这是我的CNN
并获得
model = cnn_model()print(model.call(train_data [0]))ValueError:输入 conv2d_6层的0与该层不兼容:预期ndim = 4, 发现ndim = 3。收到的完整图形:[28、28、1]
形状为(28,28,1)
怎么了?
input_shape = (28,28.1)
class cnn_model(tf.keras.Model):
def __init__(self):
super(cnn_model,self).__init__()
self.conv1 = layers.Conv2D(32,(3,3),activation='relu',input_shape= input_shape)
self.maxpool = layers.MaxPool2D((2,2))
self.conv2 = layers.Conv2D(64,(3,3),activation ='relu')
self.conv3 = layers.Conv2D(64,(3,3),activation='relu')
self.flatten = layers.Flatten()
self.dense64 = layers.Dense(64,activation='relu')
self.dense10 = layers.Dense(10,activation='relu')
def call(self,inputs):
x = self.conv1(inputs)
x = self.maxpool(x)
x = self.conv2(x)
x = self.maxpool(x)
x = self.conv3(x)
x = self.flatten(x)
x = self.dense64(x)
x = self.dense10(x)
return x
答案 0 :(得分:0)
您的input_shape
参数看起来不错,所以我猜train_data[0]
没有足够的尺寸!可能train_data.shape
就像(N,H,W,C)这样的东西已经准备好进入模型,但是train_data[0].shape
就像(H,W,C)一样出来了,其尺寸比预期。如果要向模型提供单个样本,则可能必须使用numpy的expand_dims将train_data[0]
重塑为(1,H,W,C)。
答案 1 :(得分:0)
从您的代码片段中,
input_shape = (28,28.1)
是否有错别字-.
而不是,
?您打算将其编写如下吗?
input_shape = (28, 28, 1)