根据keras doc,
Conv2D输出形状
如果data_format
=’channels_first’,则具有形状:(样本,过滤器,new_rows,new_cols)的4D张量;如果data_format
=’channels_last’,则具有形状:(样本,new_rows,new_cols,过滤器)的4D张量。由于填充,行和列的值可能已更改。
由于使用
channels_last,因此层输出的形状为:
# shape=(100, 100, 100, 3)x = Conv2D(32, (3, 3), activation='relu')(input_layer)# shape=(100, row, col, 32)x = Flatten()(x)# shape=(100, row*col*32)x = Dense(256, activation='relu')(x)# shape=(100, 256)x = Dense(10, activation='softmax')(x)# shape=(100, 10)
错误说明(已编辑,感谢@Marcin)
使用
Dense图层将4D张量(shape =(100,row,col,256))链接到2D张量(shape
=(100,256))仍将形成4D张量(shape =(100,row,col,256) ),这不是您想要的。
# shape=(100, 100, 100, 3)x = Conv2D(32, (3, 3), activation='relu')(input_layer)# shape=(100, row, col, 32)x = Dense(256, activation='relu')(x)# shape=(100, row, col, 256)x = Dense(10, activation='softmax')(x)# shape=(100, row, col, 10)
当输出4D张量与目标2D张量不匹配时,将发生错误。
这就是为什么需要
Flatten一层将其从4D平坦到2D的原因。
参考
Conv2D
密集



