简体   繁体   English

层“conv2d”的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(32、28、28)

[英]Input 0 of layer "conv2d" is incompatible with the layer: expected min_ndim=4, found ndim=3. Full shape received: (32, 28, 28)

I wrote a CNN function for fashion minst dataset, whenever I try to call it in the main, I receive this error: Input 0 of layer "conv2d" is incompatible with the layer: expected min_ndim=4, found ndim=3.我为 fashion minst 数据集写了一个 CNN function,每当我尝试在 main 中调用它时,我都会收到此错误:层“conv2d”的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=3。 Full shape received: (32, 28, 28).已收到完整形状:(32, 28, 28)。

Here's how did I call it:我是这样称呼它的:

cnn_model = create_CNN(28, 28, 3, 10)

Here's the CNN code:这是 CNN 代码:

def create_CNN(height, weight, channels, classes):
  model = Sequential()
  model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(height, weight, channels)))
  model.add(MaxPooling2D((2, 2)))
  model.add(Conv2D(64, (3, 3), activation='relu'))
  model.add(MaxPooling2D((2, 2)))
  model.add(Flatten())
  model.add(Dense(64, activation='relu'))
  model.add(Dense(classes))

  model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
  
  return model
def create_CNN(classes:int=10, input_shape:tuple=(28, 28, 3)):
  model = Sequential()
  model.add(Conv2D(32, (3, 3), activation='relu', input_shape=input_shape))
  model.add(MaxPooling2D((2, 2)))
  model.add(Conv2D(64, (3, 3), activation='relu'))
  model.add(MaxPooling2D((2, 2)))
  model.add(Flatten())
  model.add(Dense(64, activation='relu'))
  model.add(Dense(classes, activation='softmax'))

  model.compile(optimizer='adam', loss='Sparse_categorical_crossentropy', metrics=['accuracy'])
  
  return model

and also reshape the value, if your data will store in x variable then,并且重塑价值,如果你的数据将存储在x变量中,那么,

x = data  # your Mnist Fashion Data
print(x.ndim, x.shape)# No. Of Dimensions 3 and Shape is (32, 28, 38)
x = data.reshape((32, 28, 28, 3))  # Reshaping the Data
print(x.ndim, x.shape) # After Reshaping, it No. Of Dimensiona 4 and shape is (32, 28, 28, 3)

I Hope, It will Work Properly.我希望,它会正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:层“conv2d”的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(28、28、1) - ValueError: Input 0 of layer "conv2d" is incompatible with the layer: expected min_ndim=4, found ndim=3. Full shape received: (28, 28, 1) ValueError:conv2d 层的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(2240、70、3) - ValueError: Input 0 of layer conv2d is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (2240, 70, 3) ValueError:顺序层的输入 0 与层不兼容::预计 min_ndim=4,发现 ndim=3。 收到完整形状:[8, 28, 28] - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28] 层 conv2d_6 的输入 0 与层不兼容:预期 ndim=4,发现 ndim=3。 收到的完整形状:[28, 28, 1] - Input 0 of layer conv2d_6 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [28, 28, 1] ValueError:conv2d 层的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(无、400、50) - ValueError: Input 0 of layer conv2d is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (None, 400, 50) ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[None, 32, 32] - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 32, 32] ValueError:layersequence_4 的输入 0 与 layer 不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(32、224、3) - ValueError: Input 0 of layer sequential_4 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (32, 224, 3) ValueError:层 conv2d 的输入 0 与层不兼容:预期 ndim=4,发现 ndim=3。 收到的完整形状:[无,30,30] - ValueError: Input 0 of layer conv2d is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 30, 30] ValueError:conv2d_46 层的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(无,64,64) - ValueError: Input 0 of layer conv2d_46 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (None, 64, 64) “密集”层的输入 0 与该层不兼容:预期 min_ndim=2,发现 ndim=1。 完整形状收到:(32,) - Input 0 of layer "dense" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (32,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM