ValueError: 层 conv2d 的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=3。收到完整形状:(无、400、50)

时间:2021-06-01 00:39:54

标签: python tensorflow keras

我不断收到与输入形状相关的错误。任何帮助将不胜感激。谢谢!

def deep_model():

    model = Sequential()
    model.add(keras.layers.Conv1D(filters=50, kernel_size=9, strides=1, padding='same', 
               batch_input_shape=(None, Length, 4), activation='relu'))

    model.add(keras.layers.Conv2D(32, kernel_size =(9, 9), strides =(1, 1),
                    activation ='relu'))
    model.add(MaxPooling2D(pool_size =(2, 2), strides =(2, 2)))
    model.add(keras.layers.Conv2D(64, (9, 9), activation ='relu'))
    model.add(MaxPooling2D(pool_size =(2, 2)))
    model.add(Flatten())
    model.add(Dense(100, activation ='relu'))
    model.add(Dropout(0.3))
    model.add(Dense(3, activation ='softmax'))

    # training the model
    model.compile(loss = keras.losses.categorical_crossentropy,
                optimizer = keras.optimizers.SGD(lr = 0.01),
                metrics =['accuracy'])

    return model

x_train = x_train.reshape(-1, 400, 4)
<块引用>

raise ValueError('Input ' + str(input_index) + ' of layer ' + ValueError: 层 conv2d 的输入 0 与层不兼容:: 预期 min_ndim=4,发现 ndim=3。收到完整形状:(无,400, 50)

我也尝试重塑 x_train,但出现此错误:

<块引用>

x_train = x_train.reshape(-1, 400, 400, 4) ValueError:无法重塑 大小为 43200000 的数组转化为形状 (400,400,4)

1 个答案:

答案 0 :(得分:0)

我认为您需要输入形状具有额外的维度。 重塑失败的原因是因为 400 * 400 * 4 等于 640000 而不是 43200000

你会这样做:

x_train = x_train[np.newaxis]