【问题标题】:LSTM RNN to predict multiple time-steps and multiple features simultaneouslyLSTM RNN 同时预测多个时间步长和多个特征
【发布时间】:2020-04-11 07:31:16
【问题描述】:

我有一个来自 4 个温度传感器的数据集,用于测量建筑物内/周围的不同位置:

我正在训练一个模型,该模型接受形状 (96, 4) 的输入,4 个传感器的 96 个时间步长。据此,我想为每个传感器预测 48 个点,形状 (48, 4)。

到目前为止,我已经完成了一个只能预测一个传感器的实现。我主要关注this section from the TensorFlow tutorials

我的火车 X 是形状 (6681, 96, 4),火车 Y 是形状 (6681, 48),因为我已将其限制为仅一个传感器。如果我只是在训练时将训练 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].,因为我的模型并不期望这种形状。

我遇到困难的地方是我的 LSTM 层的输入/输出形状。我只是不知道如何以 (BATCH_SIZE, 48, 4) 的形状结束。

这是我目前的图层设置:

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)

一些想法:

  • 我是否只是遗漏了什么或忘记为要使用的输出特征/尺寸设置参数?
  • 是否需要训练单独的 RNN 来预测每个传感器?

谢谢! :)

【问题讨论】:

    标签: python tensorflow keras lstm recurrent-neural-network


    【解决方案1】:

    我最终通过使用 Dense 层实现了这一点,该层具有我想要的时间步数乘以我预测的特征数。然后,我将其重新塑造成我想要的输出形状。

    我不确定这是否是执行此操作的最佳方法,但它可以正常工作。

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

    【讨论】:

    • 添加AveragePooling1D(pool_size=(2))怎么样?
    • 不要将Dropout 层用于 LSTM。他们有自己的recurrent_dropout 论点。 RNN 通常不能很好地处理 dropout。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2018-03-09
    • 2022-01-14
    • 2022-01-24
    • 1970-01-01
    相关资源
    最近更新 更多