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

问题描述 投票:0回答:1

当我

print(inp_shape)
时,我得到
(288, 512, 3)
。不过我还是明白了

ValueError: Error when checking input: expected conv2d_1_input to have 4 
dimensions, but got array with shape (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')
python tensorflow neural-network keras conv-neural-network
1个回答
2
投票

添加此行

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

X_training
model.fit

之前
© www.soinside.com 2019 - 2024. All rights reserved.