繁体   English   中英

层 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)

我一直在做一个项目,使用时间序列数据和天气数据来估计交通流量。 我正在为我的时间序列使用 30 个值的 window,并且我正在使用 20 个与天气相关的功能。 我已经使用功能 API 来实现这个,但我一直收到同样的错误,我不知道如何解决。 我查看了其他类似的线程,例如conv1d_1 层的输入 0 与该层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:[None, 200] ,但没有帮助。

这是我的 model,

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 中输入的形状是如何工作的。 如果您能指出正确的方向,我们将不胜感激,但我现在需要的是修复我的 model。

问题

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

解决方案

您没有指定“步骤”参数。
如果“步骤”为 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)    

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


series_input = 输入(形状 = (series_input_train.shape[1],1,)


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM