【问题标题】:Keras Conv1d input shape problem, Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2Keras Conv1d输入形状问题,conv1d层的输入0与层不兼容::预期min_ndim=3,发现ndim=2
【发布时间】:2021-03-18 12:25:11
【问题描述】:

我正在尝试使用 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)

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

【问题讨论】:

  • 如果你使用带注释的代码,它不会做太多......在你的最后一行是答案。
  • 这只是一个复制/过去的错误。我试过data_x = data_x.reshape(-1,10,1)。并与:python data_x = data_x.reshape(-1,10,1) inputs = Input(10, 1,1) x = Conv1D(64, 2, input_shape=(10,1,1))(inputs)
  • 完全使用帖子中的代码,但在CNN之前添加data_x = data_x.reshape(-1,10,1)
  • 正如我之前所说,我尝试使用我在原始帖子中输入的确切代码并添加data_x = data_x.reshape(-1, 10, 1),结果data_x 的形状为(406, 10, 1)。这会导致完全相同的错误:ValueError: Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (1, 10)
  • 对,我的错..

标签: python tensorflow machine-learning keras


【解决方案1】:

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

工作示例代码

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)

输出

(1, 9, 64)

【讨论】:

    猜你喜欢
    • 2022-07-13
    • 2021-06-17
    • 2019-07-13
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 2020-10-10
    • 2019-03-13
    • 2020-10-05
    相关资源
    最近更新 更多