《Deep Learning with Python》5.3节代码错误

190 阅读1分钟

按照书中代码书写后一直出错。提醒:Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 8192 but received input with shape [20, 4, 4, 512] image.png 大概意思是:输入0层的顺序是不兼容层:预计1的输入轴形状有价值8192但是收到输入形状(20 4 512)输入0层的顺序是不兼容层:预计1的输入轴形状有价值8192但是收到输入形状(20,4,4,512)

其原因在于,Dense网络层是一维数据映射到一维数据空间中的,多维数据是不能直接进行Dense运算的。 在上一节中采用Flatten()来将多维数据压入一维。

源代码 image.png 改为 image.png

添加一行代码即可

model.add(layers.Flatten())

2021/9/15