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

时间:2017-08-02 05:11:14

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

当我打印(inp_shape)时,我得到(288,512,3)。但是我仍然得到错误“ValueError:检查输入时出错:预期conv2d_1_input有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 个答案:

答案 0 :(得分:1)

添加此行

X_training= tf.reshape(X_training,[-1,288, 512, 3])
在将X_training提供给model.fit

之前