[英]LSTM RNN to predict multiple time-steps and multiple features simultaneously
I have a dataset from 4 temperature sensors measuring different places in/around a building:我有一个来自 4 个温度传感器的数据集,用于测量建筑物内/周围的不同位置:
I'm training a model that takes inputs of shape (96, 4), 96 time steps for the 4 sensors.我正在训练一个模型,该模型采用形状 (96, 4)、4 个传感器的 96 个时间步长的输入。 From this I want to predict 48 points into the future for each of those sensors, shape (48, 4).
由此我想为每个传感器预测未来 48 个点,形状 (48, 4)。
So far I've got an implementation working to predict one sensor only.到目前为止,我有一个实现只能预测一个传感器。 I mostly followed this section from the TensorFlow tutorials .
我主要遵循TensorFlow 教程中的这一部分。
My train X is shape (6681, 96, 4), train Y is shape (6681, 48) as I have restricted this to one sensor only.我的火车 X 的形状为 (6681, 96, 4),火车 Y 的形状为 (6681, 48),因为我仅将其限制为一个传感器。 If I just change train Y to (6681, 48, 4) when training I of course get
ValueError: Dimensions must be equal, but are 48 and 4 for 'loss/dense_loss/sub' (op: 'Sub') with input shapes: [?,48], [?,48,4].
如果我只是在训练时将火车 Y 更改为 (6681, 48, 4) 我当然会得到
ValueError: Dimensions must be equal, but are 48 and 4 for 'loss/dense_loss/sub' (op: 'Sub') with input shapes: [?,48], [?,48,4].
as my model is not expecting this shape.因为我的模型不期望这种形状。
Where I'm getting stuck is with my LSTM layer's input/output shapes.我陷入困境的地方是我的 LSTM 层的输入/输出形状。 I just can't figure out how to finish with a shape of (BATCH_SIZE, 48, 4).
我只是不知道如何以 (BATCH_SIZE, 48, 4) 的形状结束。
Here's my layer setup at the moment:这是我目前的图层设置:
tf.keras.backend.clear_session()
print("Input shape", x_train_multi.shape[-2:])
multi_step_model = tf.keras.models.Sequential()
multi_step_model.add(tf.keras.layers.LSTM(32,
return_sequences=True,
input_shape=x_train_multi.shape[-2:]))
multi_step_model.add(tf.keras.layers.Dropout(rate=0.5)) # Dropout layer after each LSTM to reduce overfitting.
multi_step_model.add(tf.keras.layers.LSTM(16, activation='relu'))
multi_step_model.add(tf.keras.layers.Dropout(rate=0.5))
# The argument to Dense shapes the results to give the number of time steps we want.
# But how do I make it keep 4 features as well?!?
multi_step_model.add(tf.keras.layers.Dense(future_target / STEP))
multi_step_model.compile(optimizer=tf.keras.optimizers.RMSprop(clipvalue=1.0), loss='mae')
# Shape of predictions
for x, y in val_data_multi.take(1):
print ("Prediction shape", multi_step_model.predict(x).shape)
Some thoughts:一些想法:
Thanks!谢谢! :)
:)
I managed to achieve this in the end by using a Dense layer with the number of time steps I desired multiplied by the number of features I was predicting.我最终通过使用 Dense 层实现了这一点,其中我想要的时间步数乘以我预测的特征数。 Then after that, I reshaped this into the output shape I desired.
然后,我将其重塑为我想要的输出形状。
I'm not sure if this is the optimal method for doing this, but it works okay.我不确定这是否是执行此操作的最佳方法,但它可以正常工作。
#Experimental code for predicting multiple sensors
import tensorflow.keras.layers as tfl
tf.keras.backend.clear_session()
print("Input shape", x_train_multi.shape[-2:])
# Input shape (96, 4)
multi_step_model = tf.keras.Sequential()
multi_step_model.add(tf.keras.layers.LSTM(32, return_sequences=True, input_shape=x_train_multi.shape[-2:]))
multi_step_model.add(tf.keras.layers.Dropout(rate=0.5))
multi_step_model.add(tf.keras.layers.LSTM(16, return_sequences=False, activation='relu'))
multi_step_model.add(tf.keras.layers.Dropout(rate=0.5))
print("After LSTMs", multi_step_model.output_shape)
# After LSTMs (None, 16)
multi_step_model.add(tf.keras.layers.Dense((future_target / STEP) * 4))
print("After Dense Layer", multi_step_model.output_shape)
# After Dense Layer (None, 192)
multi_step_model.add(tf.keras.layers.Reshape((int(future_target / STEP), 4)))
print("After Reshape", multi_step_model.output_shape)
# After Reshape (None, 48, 4)
multi_step_model.compile(optimizer=tf.keras.optimizers.RMSprop(clipvalue=1.0), loss='mae')
# Shape of predictions
for x, y in val_data_multi.take(1):
print ("Prediction shape", multi_step_model.predict(x).shape)
# Prediction shape (512, 48, 4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.