不能使用卷积1D。错误:预期conv1d_1有3个维度

时间:2018-01-16 07:03:00

标签: tensorflow deep-learning keras convolution

我像这样使用卷积1D。

X_train[:, None].shape
X_train_t = X_train[:, None]
X_test_t = X_test[:, None]

K.clear_session()
model = Sequential()
model.add(Conv1D(39, 1, activation='relu', input_shape=(1,12)))

model.compile(loss='mean_squared_error', optimizer='adam' )

model.summary()

model.fit(X_train_t, y_train, epochs=200, batch_size=1, verbose=1)

y_pred = model.predict(X_test)

它显示错误

ValueError: Error when checking target: expected conv1d_1 to have 3 dimensions, but got array with shape (39, 1)

我使用此代码 print(X_train.shape)打印形状。

(39, 12)

如果我将input_shape模型更改为1,1。

model.add(Conv1D(39, 1, activation='relu', input_shape=(1,1)))

显示错误。

ValueError: Error when checking input: expected conv1d_1_input to have shape (None, 1, 1) but got array with shape (39, 1, 12)

如何使用卷积1D?

1 个答案:

答案 0 :(得分:0)

在代码中添加model.add(Flatten())

model = Sequential()
model.add(Flatten())
model.add(Conv1D(39, 1, activation='relu', input_shape=(1,12)))

model.compile(loss='mean_squared_error', optimizer='adam' )

model.summary()

model.fit(X_train_t, y_train, epochs=200, batch_size=1, verbose=1)

y_pred = model.predict(X_test)