简体   繁体   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

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.

相关问题 Keras Conv1D ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2 - Keras Conv1D ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2 ValueError: 层 conv1d 的输入 0 与层不兼容: : 预期 min_ndim=3, 发现 ndim=2 - ValueError: Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2 层 conv1d 的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2。 收到的完整形状:(无,30) - Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 30) Conv1D:ValueError:层序列_1 的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2。 收到完整形状:(无,2) - Conv1D: ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 2) Keras Conv2D - ValueError:层序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3 - Keras Conv2D - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3 添加Conv1D层时出现错误“输入0与层conv1d_48不兼容:预期的ndim = 3,找到的ndim = 2” - Error 'Input 0 is incompatible with layer conv1d_48: expected ndim=3, found ndim=2' when adding Conv1D layer ValueError:层“conv2d”的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(28、28、1) - ValueError: Input 0 of layer "conv2d" is incompatible with the layer: expected min_ndim=4, found ndim=3. Full shape received: (28, 28, 1) ValueError:conv2d 层的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(2240、70、3) - ValueError: Input 0 of layer conv2d is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (2240, 70, 3) 层 conv1d_39 的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2。 收到的完整形状:(无,64) - Input 0 of layer conv1d_39 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 64) Keras CNN错误conv1d_13层的输入0与层不兼容::预期min_ndim=3,发现ndim=2 - Keras CNN error Input 0 of layer conv1d_13 is incompatible with the layer: : expected min_ndim=3, found ndim=2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM