[英]Keras Conv1d input shape problem, Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2
I am trying to use a conv1d to predict time series, but I have trouble with the conv1d input shape.我正在尝试使用 conv1d 来预测时间序列,但是 conv1d 输入形状有问题。 My data 406 samples of 10 values in temporal order.
我的数据按时间顺序包含 10 个值的 406 个样本。 The goal is to predict sample N+1 using sample N as input.
目标是使用样本 N 作为输入来预测样本 N+1。
Here is a sample of my code:这是我的代码示例:
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)
But I get this error 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)
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)
。
I don't know what I am missing, I even tried to do data_x = data_x.reshape(-1,10,1)
but with the same results.我不知道我错过了什么,我什至尝试做
data_x = data_x.reshape(-1,10,1)
但结果相同。
Use tf.expand_dims(x, axis=0)
instead of reshape.使用
tf.expand_dims(x, axis=0)
而不是重塑。
Working sample code工作示例代码
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 Output
(1, 9, 64)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.