itr*_*lli 6 numpy python-3.x keras keras-layer
我对Keras和深度学习有点陌生。我目前正在尝试复制本文,但是在编译第一个模型(没有LSTM)时,出现以下错误:
“ ValueError:检查目标时出错:预期densed_3具有形状(无,120、40)但具有形状(8、40、1)的数组”
该模型的描述是这样的:
T
是特定于设备的窗口大小)size
3,5,和7分别是stride=1
,number of filters=32
,
activation type=linear
,border mode=same
output_dim=128
,activation type=ReLU
output_dim=128
,activation type=ReLU
output_dim=T
,activation type=linear
我的代码是这样的:
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)
Run Code Online (Sandbox Code Playgroud)
其中X
和y
是numpy arrays
长度为40个值的8个序列。所以,X.shape
和y.shape
是(8, 40, 1)
。实际上是一批数据。问题是我无法理解输出的形状(None, 120, 40)
以及这些尺寸的含义。
正如您所指出的,您的形状包含batch_size
,length
和channels
: (8,40,1)
你的三个卷积,每一个,都创建一个像 一样的张量(8,40,32)
。您在 中的串联axis=1
创建了一个像(8,120,32)
, where 一样的张量120 = 3*40
。
现在,密集层仅在最后一个维度(本例中为通道)上起作用,而长度(现在为 120)保持不变。
现在,看来您确实想保留最后的长度。因此,您不需要任何展平或重塑图层。但您需要将长度保持为 40。
您可能在错误的轴上进行串联。您应该连接通道轴(2 或 -1),而不是长度轴 (1)。
所以,这应该是你的连接层:
merge_layer = layers.Concatenate()([branch_a, branch_b, branch_c])
#or layers.Concatenate(axis=-1)([branch_a, branch_b, branch_c])
Run Code Online (Sandbox Code Playgroud)
这将输出(8, 40, 96)
,并且密集层会将 96 转换为其他内容。
归档时间: |
|
查看次数: |
470 次 |
最近记录: |