conv1d 层的输入 0 与该层不兼容::预期 min_ndim=3,发现 ndim=2。收到完整形状:(无,30)

Min*_*ewa 13 python time-series convolution lstm tensorflow

我一直在研究一个使用时间序列数据与天气数据相结合来估计交通流量的项目。我的时间序列使用了 30 个值的窗口,并且使用了 20 个与天气相关的特征。我已经使用函数式 API 来实现此目的,但我不断收到相同的错误,并且我不知道如何解决它。我查看了其他类似的线程,例如层 conv1d_1 的输入 0 与该层不兼容:预期 ndim=3,发现 ndim=2。收到的完整形状:[无,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)
Run Code Online (Sandbox Code Playgroud)

series_input_train和的形状weather_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)
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

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

小智 0

问题

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

解决方案

您没有指定“步骤”参数。
如果“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)    
Run Code Online (Sandbox Code Playgroud)