LSTM网络,用于使用初始状态进行每日天气预报

时间:2019-03-30 23:14:58

标签: python tensorflow machine-learning lstm

对于两个天气值的预测,我有一个数据集,该数据集包含一个分辨率为15分钟的一年数据。因此可以获得每天 96 个时间点和总共 365 * 96 个时间的数据。

每当我在LSTM网络中输入一天的数据进行训练时,我也想给出输出状态 h 的真实测量数据。

# tensor for setting output state h
init_state = tf.reshape(init_state, [1, 2])
init_state = tf.nn.rnn_cell.LSTMStateTuple(tf.constant([[0, 0]], tf.float32), init_state)

# tensor placeholder for input data with 2 inputs
train_placeholder = tf.placeholder(tf.float32, [1, 96, 2])

# hidden size equals 2, because LSTM output and output state h have 2 values
cell = tf.contrib.rnn.LSTMCell(2)
rnn_outputs_ts, state = tf.nn.dynamic_rnn(cell, train_placeholder,
                                          dtype=tf.float32, initial_state=init_state) 

培训每天循环进行:

# daily training of the weather data
for step in len(train_data):
    sess.run(train_lstm, {train_placeholder: train_data[step].reshape(1, 96, 2),
                          init_state: init_data[step].reshape(1, 2) })

如果如上所述,我在每次迭代中都设置了输出状态h,则与默认情况下始终将输出状态h设置为零相比,可以获得更好的结果。

由于输出状态 h 包含两个值,因此我输入了实际测量值,因此LSTM单元的隐藏大小也限制为2。 但是,在没有手动设置输出状态 h 的情况下,我注意到,如果LSTM单元的隐藏大小大于2,并且输出变平为2,则可能会有更好的结果。 MultiRNNCell也是如此,由于输出状态 h ,我也不使用它。

一方面,我想要一个更大的LSTM网络,但另一方面,我也想使用实际的测量数据来设置输出状态 h

在我的情况下,您将如何处理?

1 个答案:

答案 0 :(得分:1)

我认为您需要另一层-网络通常具有一个输入层,多个中间层和一个进行最终分类的输出层。在您的情况下,可以在LSTM层之后添加具有2个神经元的Dense()层。

此外,不建议使用LSTMCell(https://www.tensorflow.org/api_docs/python/tf/nn/rnn_cell/LSTMCell),并将在tensorflow 2.0中将其删除。我建议使用Keras(围绕tensorflow的包装器)来设置您的网络-它提供了许多便利功能并有助于分析模型。检查此处以获取有关如何设置LSTM模型的说明:https://adventuresinmachinelearning.com/keras-lstm-tutorial/