【问题标题】:Reshaping layer in Keras在 Keras 中重塑图层
【发布时间】: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


【解决方案1】:

发生这种情况是因为您使用的是'sparse_categorical_crossentropy'

“稀疏”意味着系统不会期望整个数组,而只是热点的坐标。与其期待一个常规的 (None, 2, 125) 张量,它只会期待 (None, 2, 1) 指示 125 个类中的哪一个是正确的。

要解决此问题,您要么开始使用稀疏的y_train,要么将损失替换为'categorical_crossentropy'

我相信可以使用sparse_y_train = numpy.argmax(y_train, axis=-1) 获得稀疏的y_train。如果这个模型没有给你带来内存问题,你就不需要去稀疏。

【讨论】:

    猜你喜欢
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 2019-02-10
    相关资源
    最近更新 更多