【发布时间】:2019-12-28 06:25:46
【问题描述】:
我正在尝试构建一个带有输出矩阵的卷积神经网络。输入形状为 (100,100,4),输出形状为 (2,125)。
这是我当前模型的摘要:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_63 (InputLayer) (None, 100, 100, 4) 0
_________________________________________________________________
conv2d_44 (Conv2D) (None, 100, 100, 25) 2525
_________________________________________________________________
max_pooling2d_38 (MaxPooling (None, 50, 50, 25) 0
_________________________________________________________________
flatten_38 (Flatten) (None, 62500) 0
_________________________________________________________________
dense_47 (Dense) (None, 10) 625010
_________________________________________________________________
dense_48 (Dense) (None, 250) 2750
_________________________________________________________________
reshape_63 (Reshape) (None, 2, 125) 0
=================================================================
Total params: 630,285
Trainable params: 630,285
Non-trainable params: 0
_________________________________________________________________
None
我认为应该没问题,但是当我尝试拟合模型时出现此错误:
ValueError: Error when checking target: expected reshape_62 to have shape (2, 1) but got array with shape (2, 125)
这是我使用的代码
batch_size = 100
input_layer = Input(shape=(xs[1],xs[2],xs[3]))
conv1 = Conv2D(filters = 25, kernel_size = 5,padding="same",activation="relu", data_format = 'channels_last')(input_layer)
pool1 = MaxPooling2D(pool_size=(2,2),padding="same")(conv1)
flat = Flatten()(pool1)
hidden1 = Dense(10, activation='relu')(flat)
output_layer = Dense(ys[1]*ys[2], activation='softmax')(hidden1)
output_reshape = Reshape((2,125))(output_layer)
model = Model(inputs=input_layer, outputs=output_reshape)
print(model.summary())
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', sample_weight_mode='temporal')
model.fit(x_train,y_train,batch_size=batch_size,epochs=3)
我一直在查找重塑层的工作原理,但仍然无法弄清楚。非常感激任何的帮助。
【问题讨论】:
-
此错误消息很可能不是您显示的模型摘要。你确定你使用了正确的变量吗?
-
@DanielMöller 你好丹尼尔!非常感谢您的回答!我认为他们应该是一样的。我已经发布了我在原始帖子中使用的代码,因为这里太长了。再次感谢!
标签: tensorflow matrix keras conv-neural-network reshape