层sequential_43 的输入0 与层不兼容:预期min_ndim=5,发现ndim=4。收到完整形状:(无、32、32、100000)

时间:2021-03-01 14:27:42

标签: python tensorflow keras deep-learning conv-neural-network

我的错误:

Input 0 of layer sequential_43 is incompatible with the layer: 
: expected min_ndim=5, found ndim=4. Full shape received: (None, 32, 32, 100000)

我输入的形状:

samples.shape(32,32,32,100000)

labels.shape(100000,)

我现在尝试运行的代码如下:

model = keras.models.Sequential()
layers = tf.keras.layers

model.add(layers.Conv3D(filters=5, kernel_size=(4,4,4), strides=2, activation='relu', input_shape=(8,32,32,32,1)))
model.add(layers.Conv3D(filters=5, kernel_size=(4,4,4), strides=1, activation='relu'))
model.add(layers.Conv3D(filters=5, kernel_size=(4,4,4), strides=1, activation='relu'))
model.add(layers.Conv3D(filters=5, kernel_size=(4,4,4), strides=1, activation='relu'))
model.add(layers.Conv3D(filters=5, kernel_size=(4,4,4), strides=2, activation='relu'))


model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(1, activation='relu'))

model.compile(optimizer=Adam(learning_rate=0.0001),loss='mape',metrics=['accuracy'])
model.fit(x=samples,y=labels,validation_split=0.1,epochs=1,shuffle=True,verbose=2)

我看到的每个地方的语法都是 (batchsize,dim1,dim2,dim3,dim4)。我将 batchsize 设为 8,将数据设为 32x32x32 立方体,将颜色设为 1 维。即使我从 input_shape 中删除批量大小并将其添加到 model.fit 作为 batch_size=8 它也会给出相同的错误。有谁知道为什么?

1 个答案:

答案 0 :(得分:0)

如您的问题所述,维度的顺序是 (batchsize,dim1,dim2,dim3,dim4),因此您需要重塑 samples 数组以匹配该顺序。

您可以转置您的数组以获取样本数作为第一维,并将其扩展以将通道维度(或颜色,如果我重复使用您的术语)为 1。

>>> samples.shape
TensorShape([32, 32, 32, 100000])
>>> samples = tf.expand_dims(tf.transpose(samples,[3,0,1,2]), axis=-1)
>>> samples.shape
TensorShape([100000, 32, 32, 32, 1])