【问题标题】:Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 30)层 conv1d 的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2。收到的完整形状:(无,30)
【发布时间】:2021-06-17 11:05:16
【问题描述】:

我一直在做一个项目,使用时间序列数据和天气数据来估计交通流量。我正在为我的时间序列使用 30 个值的窗口,并且我正在使用 20 个与天气相关的功能。我已经使用功能 API 来实现这一点,但我一直收到同样的错误,我不知道如何解决它。我查看了其他类似的线程,例如Input 0 of layer conv1d_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 200],但没有帮助。

这是我的模型,

series_input = Input(shape = (series_input_train.shape[1], ), name = 'series_input')
x = Conv1D(filters=32, kernel_size=5, strides=1, padding="causal", activation="relu")(series_input)
x = LSTM(32, return_sequences = True)(x)
x = LSTM(32, return_sequences = True)(x)
x = Dense(1, activation = 'relu')(x)
series_output = Lambda(lambda w: w * 200)(x)

weather_input = Input(shape = (weather_input_train.shape[1], ), name = 'weather_input')
x = Dense(32, activation = 'relu')(weather_input)
x = Dense(32, activation = 'relu')(x)
weather_output = Dense(1, activation = 'relu')(x)

concatenate = concatenate([series_output, weather_output], axis=1, name = 'concatenate')

output = Dense(1, name = 'output')(concatenate)

model = Model([series_input, weather_input], output)

series_input_trainweather_input_train 的形状分别是 (34970, 30) 和 (34970, 20)。

我一直收到的错误是这个,

ValueError: Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 30)

我做错了什么?

老实说,我一直难以弄清楚输入的形状在 TensorFlow 中是如何工作的。如果您能指出我正确的方向,我们将不胜感激,但我现在需要的是修复我的模型。

【问题讨论】:

    标签: python tensorflow time-series lstm convolution


    【解决方案1】:

    问题

    3+D 张量,形状:batch_shape + (steps, input_dim) https://keras.io/api/layers/convolution_layers/convolution1d/

    解决方案

    您没有指定“步骤”参数。
    如果“steps”为1,则使用以下代码:

    import numpy as np
    series_input_train = np.expand_dims(series_input_train, axis=1)
    weather_input_train = np.expand_dims(weather_input_train, axis=1)    
    

    【讨论】:

    • 那么我将向输入层的形状参数传递什么?我现在收到此错误,conv1d 层的输入 0 与该层不兼容::预期 min_ndim=3,发现 ndim=2。收到的完整形状:(None, 30) with this layer, series_input = Input(shape = (series_input_train.shape[1], ), name = 'series_input')
    • 在上述网站中,输入是128个长度的向量,有10个时间步长,batch size是4。input_shape = (4, 10, 128)
    • 我的输入不是向量,它们只是 30 个值的序列。
    【解决方案2】:

    正如 Tao-Lung 所说,卷积层的第一个连接需要 3 位形式。 序列上的 1D 卷积需要 3D 输入。换句话说,对于批次的每个元素,对于每个时间步,一个向量。 如果您希望步骤是单一的,您可以按如下方式解决您的问题:


    series_input = Input(shape = (series_input_train.shape[1],1,)


    x = Conv1D(filters=32, kernel_size=5, strides=1, padding="causal", activation="relu",input_shape=[None,series_input])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-13
      • 2022-12-10
      • 2021-08-10
      • 2021-09-23
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      相关资源
      最近更新 更多