繁体   English   中英

Keras Conv1d输入形状问题,conv1d层的输入0与层不兼容::预期min_ndim=3,发现ndim=2

[英]Keras Conv1d input shape problem, Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2

我正在尝试使用 conv1d 来预测时间序列,但是 conv1d 输入形状有问题。 我的数据按时间顺序包含 10 个值的 406 个样本。 目标是使用样本 N 作为输入来预测样本 N+1。

这是我的代码示例:

print(data_x.shape)
# (406, 10)
print(data_y.shape)
# (406, 10)


inputs = Input(10, 1)
x = Conv1D(64, 2, input_shape=(10,1))(inputs)
x = Dense(64, "relu")(x)
x = Dense(64, "relu")(x)
x = Dense(10, "sigmoid")(x)

model = Model(inputs, x)
model.compile(loss='mse', metrics=['accuracy'], optimizer='adam')
history = model.fit(data_x, data_y,
                    batch_size=10, epochs=EPOCHS)

但我收到此错误ValueError: Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (1, 10) ValueError: Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (1, 10)

我不知道我错过了什么,我什至尝试做data_x = data_x.reshape(-1,10,1)但结果相同。

使用tf.expand_dims(x, axis=0)而不是重塑。

工作示例代码

import tensorflow as tf

inputs = (10, 1)
x = tf.random.normal(inputs)
inp = tf.expand_dims(x, axis=0)
x = tf.keras.layers.Conv1D(64, 2, input_shape=(10,1))(inp)

Output

(1, 9, 64)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM