[英]Tensorflow keras timeseries prediction with X and y having different shapes
我正在尝试使用 tensorflow 和 keras 进行时间序列预测,其中X
和y
具有不同的维度:
X.shape = (5000, 12)
y.shape = (5000, 3, 12)
当我执行以下操作时
n_input = 7
generator = TimeseriesGenerator(X, y, length=n_input, batch_size=1)
for i in range(5):
x_, y_ = generator[i]
print(x_.shape)
print(y_.shape)
我得到了 output
(1, 7, 12)
(1, 3, 12)
(1, 7, 12)
(1, 3, 12)
...
这是因为我的数据是气象数据,我有 5000 天,对于数组X
中的训练,我使用 7 天的滑动 window,每天包含 12 个特征(气压、温度、湿度 ao)。 在目标数组y
中,我将 windows 滑动 3 天,试图预测接下来的 3 天到每个 window 的 7 天。
但是当我尝试安装 model 时,由于X
和y
arrays 的形状不匹配而出现错误:
model = Sequential()
model.add(LSTM(4, input_shape=(None, 12)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
history = model.fit_generator(generator, epochs=3).history
ValueError: A target array with shape (1, 3, 12) was passed for an output of shape (None, 1) while using as loss `mean_squared_error`. This loss expects targets to have the same shape as the output.
那么有没有办法针对尺寸不匹配调整架构? 或者有没有办法重塑X
和y
以使它们与这种架构一起工作? 我尝试了后期将X
重塑为(5000, 7, 12)
,但这也产生了维度错误。 Tnx
你的发电机是正确的......这是你的网络不起作用。
你没有正确处理维度。 你正在处理序列,所以你需要在你的 LSTM 单元中强加return_sequences=True
。 您的输入有 7 个时间步,而您的 output 有 3 个时间步,您必须从 7 传递到 3(您可以通过池化等方式进行)。
下面是一个虚拟示例。 我不使用池操作,而只是使用 select 作为序列的一部分,以获得 3 个时间步长的 output
X = np.random.uniform(0,1, (5000, 12))
y = np.random.uniform(0,1, (5000, 3, 12))
n_input = 7
generator = tf.keras.preprocessing.sequence.TimeseriesGenerator(X, y, length=n_input, batch_size=32)
model = Sequential()
model.add(LSTM(4, return_sequences=True, input_shape=(n_input, 12)))
model.add(Lambda(lambda x: x[:,-3:,:]))
model.add(Dense(12))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()
model.fit(generator, epochs=2)
这里是一个池化操作的例子
model = Sequential()
model.add(LSTM(4, return_sequences=True, input_shape=(n_input, 12)))
model.add(MaxPool1D(2)) # also AvgPool1D is ok
model.add(Dense(12))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()
model.fit(generator, epochs=2)
这里有一个 return_sequences=False 和重复向量的例子
model = Sequential()
model.add(LSTM(4, return_sequences=False, input_shape=(n_input, 12)))
model.add(RepeatVector(3))
model.add(Dense(12))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()
model.fit(generator, epochs=2)
您的最终(完全连接)层的形状是 (None, 1),输出的形状是 (None, 3, 12)。 Output 形状的数据和网络必须匹配。
我将使用功能 API并创建 3 个单独的密集层并将它们连接起来。 像这样:
inp = tf.keras.Input(shape=(7, 12))
x = tf.keras.layers.LSTM(4)(inp)
y1 = tf.keras.layers.Dense(12)(x)
y2 = tf.keras.layers.Dense(12)(x)
y3 = tf.keras.layers.Dense(12)(x)
y1 = tf.keras.backend.expand_dims(y1, axis=1)
y2 = tf.keras.backend.expand_dims(y2, axis=1)
y3 = tf.keras.backend.expand_dims(y3, axis=1)
output = tf.keras.layers.Concatenate(axis=1)([y1, y2, y3])
mdl = tf.keras.Model(inp, output)
mdl.summary()
返回: Model 总结
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.