我正在使用带有TF后端的keras来构建一个简单的Conv1d
网络。数据具有以下形状:
train feature shape: (33960, 3053, 1)
train label shape: (33960, 686, 1)
我使用以下方法构建模型:
def create_conv_model():
inp = Input(shape=(3053, 1))
conv = Conv1D(filters=2, kernel_size=2)(inp)
pool = MaxPool1D(pool_size=2)(conv)
flat = Flatten()(pool)
dense = Dense(686)(flat)
model = Model(inp, dense)
model.compile(loss='mse', optimizer='adam')
return model
模型摘要:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 3053, 1) 0
_________________________________________________________________
conv1d_1 (Conv1D) (None, 3052, 2) 6
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 1526, 2) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 3052) 0
_________________________________________________________________
dense_1 (Dense) (None, 686) 2094358
=================================================================
Total params: 2,094,364
Trainable params: 2,094,364
Non-trainable params: 0
运行时
model.fit(x=train_feature,
y=train_label_categorical,
epochs=100,
batch_size=64,
validation_split=0.2,
validation_data=(test_feature,test_label_categorical),
callbacks=[tensorboard,reduce_lr,early_stopping])
我收到以下非常常见的错误:
ValueError: Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (8491, 3053)
我已经检查了有关此常见问题的几乎所有帖子,但一直找不到解决方案。我究竟做错了什么?我不明白发生了什么。形状(8491, 3053)
来自哪里?
任何帮助将不胜感激,我无法解决这个问题。
答案 0 :(得分:1)
将validation_data=(test_feature,test_label_categorical)
函数中的model.fit
更改为
validation_data=(np.expand_dims(test_feature, -1),test_label_categorical)
该模型期望形状为(8491, 3053, 1)
的验证功能,但是在上面的代码中,您正在提供形状(8491, 3053)
。