【问题标题】:Noob question: Keras shapes are incompatible菜鸟问题:Keras 形状不兼容
【发布时间】:2020-12-30 13:05:15
【问题描述】:

我是数据科学专业的学生,​​我正在努力研究深度学习。我的模型如下:

Model: "sequential_32"

Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_26 (Conv2D)           (None, 27, 27, 32)        288       
_________________________________________________________________
max_pooling2d_12 (MaxPooling (None, 13, 13, 32)        0         
_________________________________________________________________
dense_13 (Dense)             (None, 13, 13, 128)       4224      
_________________________________________________________________
dropout_6 (Dropout)          (None, 13, 13, 128)       0         
_________________________________________________________________
dense_14 (Dense)             (None, 13, 13, 10)        1290      
=================================================================
Total params: 5,802
Trainable params: 5,802
Non-trainable params: 0
_________________________________________________________________

现在我正在尝试使用以下代码将一些数据放入其中:

print(x_train.shape)
print(x_test.shape)
result = model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))

输出如下:

(60000, 28, 28, 1)
(10000, 28, 28, 1)
# some error codes followed by:
ValueError: Shapes (32, 10) and (32, 13, 13, 11) are incompatible

我觉得这确实是一个很容易修复的错误,但我就是看不到它。欢迎任何帮助和/或解释!

【问题讨论】:

  • 从上面的模型中,我猜你在max pooling 之后缺少Flatten 层。因此,您的模型输出似乎是(13, 13, 10),而不是大小为10 的单个softmax。您得到的形状错误可能是 y 值形状与模型输出形状不匹配。

标签: python-3.x keras tensorflow2.0 keras-layer


【解决方案1】:

是的,Thymen 的回答帮助了我。谢谢你。模型现在看起来像这样:

Model: "sequential_35"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_29 (Conv2D)           (None, 27, 27, 32)        160       
_________________________________________________________________
max_pooling2d_15 (MaxPooling (None, 13, 13, 32)        0         
_________________________________________________________________
flatten (Flatten)            (None, 5408)              0         
_________________________________________________________________
dense_17 (Dense)             (None, 128)               692352    
_________________________________________________________________
dropout_8 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_18 (Dense)             (None, 10)                1290      
=================================================================
Total params: 693,802
Trainable params: 693,802
Non-trainable params: 0
_________________________________________________________________

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多