ValueError:检查输入时出错:预期conv2d_1_input有4个维度,但数组具有形状(120,1)

时间:2021-09-26 09:49:50

When I print(inp_shape) I get (288, 512, 3). However I still get the error "ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (120, 1)". I don't understand where teh shape (120, 1) comes from.

当我打印(inp_shape)时,我得到(288,512,3)。然而,我仍然会得到错误的“ValueError:检查输入时的错误:预期的conv2d_1_输入有4个维度,但有形状的数组(120,1)”。我不知道这个形状(120,1)从何而来。

    dropout_prob = 0.2
    activation_function = 'relu'
    loss_function = 'categorical_crossentropy'
    verbose_level = 1
    convolutional_batches = 32
    convolutional_epochs = 3
    inp_shape = X_training.shape[1:]
    num_classes = 2
    opt = SGD()
    opt2 = 'adam'

    y_train_cat = np_utils.to_categorical(y_training, num_classes) 
    y_test_cat = np_utils.to_categorical(y_testing, num_classes)

    model = Sequential()
    model.add(Conv2D(filters=16, kernel_size=(3, 3), input_shape=inp_shape))
    model.add(Conv2D(filters=32, kernel_size=(3, 3)))
    #model.add(MaxPooling2D(pool_size = (2,2)))
    #model.add(Dropout(rate=dropout_prob))
    model.add(Flatten())
    model.add(Dense(128,activation=activation_function))
    #model.add(Dropout(rate=dropout_prob))
    model.add(Dense(64,activation=activation_function))
    #model.add(Dropout(rate=dropout_prob))
    model.add(Dense(32,activation=activation_function))
    model.add(Dense(num_classes,activation='softmax'))
    model.summary()
    model.compile(loss=loss_function, optimizer=opt, metrics=['accuracy'])
    history = model.fit(X_training, y_train_cat, batch_size=convolutional_batches, epochs = convolutional_epochs, verbose = verbose_level, validation_data=(X_testing, y_test_cat))
    model.save('../models/neural_net.h5')

1 个解决方案

#1


0  

add this line

添加这一行

X_training= tf.reshape(X_training.shape[1:],[-1,288, 512, 3])

before feeding X_training to the model.fit

在给模型提供X_training之前。

#1


0  

add this line

添加这一行

X_training= tf.reshape(X_training.shape[1:],[-1,288, 512, 3])

before feeding X_training to the model.fit

在给模型提供X_training之前。