gpt4 book ai didi

python-3.x - Keras 并行层的串联改变了所需的目标形状

转载 作者:行者123 更新时间:2023-12-02 00:47:05 25 4
gpt4 key购买 nike

我对 Keras 和深度学习有点陌生。我目前正在尝试复制此paper但是当我编译第一个模型(没有 LSTM)时,出现以下错误:

"ValueError: Error when checking target: expected dense_3 to have shape (None, 120, 40) but got array with shape (8, 40, 1)"

模型的描述是这样的:

  1. 输入(长度T是设备特定的窗口大小)
  2. 并行 1D 卷积,滤波器大小 3、5 和 7分别为stride=1filters=32激活类型=线性边框模式=相同
  3. 合并层,连接输出并行一维卷积
  4. 密集层,output_dim=128activation type=ReLU
  5. 密集层,output_dim=128activation type=ReLU
  6. 密集层,output_dim=Tactivation type=线性

我的代码是这样的:

from keras import layers, Input
from keras.models import Model

# the window sizes (seq_length?) are 40, 1075, 465, 72 and 1246 for the kettle, dish washer,
# fridge, microwave, oven and washing machine, respectively.

def ae_net(T):
input_layer = Input(shape= (T,))
branch_a = layers.Conv1D(32, 3, activation= 'linear', padding='same', strides=1)(input_layer)
branch_b = layers.Conv1D(32, 5, activation= 'linear', padding='same', strides=1)(input_layer)
branch_c = layers.Conv1D(32, 7, activation= 'linear', padding='same', strides=1)(input_layer)

merge_layer = layers.concatenate([branch_a, branch_b, branch_c], axis=1)

dense_1 = layers.Dense(128, activation='relu')(merge_layer)
dense_2 =layers.Dense(128, activation='relu')(dense_1)
output_dense = layers.Dense(T, activation='linear')(dense_2)
model = Model(input_layer, output_dense)
return model

model = ae_net(40)
model.compile(loss= 'mean_absolute_error', optimizer='rmsprop')
model.fit(X, y, batch_size= 8)

其中Xy是长度为40个值的8个序列的numpy数组。因此,X.shapey.shape(8, 40, 1)。它实际上是一批数据。问题是我无法理解输出的形状 (None, 120, 40) 以及这些大小意味着什么。

最佳答案

正如您所指出的,您的形状包含 batch_sizelengthchannels:(8,40,1)

您的三个卷积分别创建一个像 (8,40,32) 这样的张量。axis=1 中的串联创建了一个类似于 (8,120,32) 的张量,其中 120 = 3*40

现在,密集层仅适用于最后一个维度(本例中为 channel ),长度(现在为 120)保持不变。

解决方案

现在,看来您确实想保留最后的长度。因此,您不需要任何展平或 reshape 图层。但您需要将长度保持为 40。

您可能在错误的轴上进行串联。您应该连接 channel 轴(2 或 -1),而不是长度轴 (1)。

所以,这应该是您的连接层:

merge_layer = layers.Concatenate()([branch_a, branch_b, branch_c])
#or layers.Concatenate(axis=-1)([branch_a, branch_b, branch_c])

这将输出 (8, 40, 96),密集层会将 96 转换为其他内容。

关于python-3.x - Keras 并行层的串联改变了所需的目标形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48081052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com