Reputation: 53
I'm facing some doubts trying to implement LSTM with multiple input sequences (multivariate) under Tensorflow.
I defined the LSTM in this way:
def LSTM(x):
x = tf.reshape(x, [-1, input_length])
x = tf.split(x, input_length, 1)
rnn_cell = rnn.MultiRNNCell([rnn.BasicLSTMCell(n_hidden), rnn.BasicLSTMCell(n_hidden)])
outputs, states = rnn.static_rnn(rnn_cell, x, dtype=tf.float32)
return tf.matmul(outputs[-1], weights['out']) + biases['out']
and the data tensors are defined in this way:
# tf Graph input
X = tf.placeholder("float", [None, input_length, 1])
Y = tf.placeholder("float", [None, n_classes])
which is ok for a one dimensional input with a known length. I will give you an easy example: You have the rain rate measured every second, so you have a time series of size N. You want to forecast the rain rate 30 minutes in advance so you split the data into segments every 30 minutes. So you would feed the LSTM with an input of 30(minutes)*60(one per second) measurements, and the output would be the rain rate after 30 minutes from the last given input (one measurement).
Up to here, the problem is solved with this simple model, but what should I do to add another input? That would be, instead of using only the rain rate to forecast itself after 30 minutes, also feed the LSTM with, for example, the humidity rate and the wind speed every second. That would be an LSTM with 3 sequences as inputs, and each of those inputs would contain 30 minutes * 60 measurements.
I need to add a "number_of_sequences" variable but I'm not sure how to reorganize the tensors. What should I change from my code? I'm a bit lost with the three dimensions of the X placeholder, maybe something like this?
X = tf.placeholder("float", [None, input_length, number_of_sequences])
And also with the code to transform the normal database into a sequence, maybe this?:
x = tf.reshape(x, [-1, input_length])
x = tf.split(x, input_length, number_of_sequences)
Thank you in advance.
Upvotes: 4
Views: 3172
Reputation: 1829
I edited your code so that you can get the result that you need,
input_length = 30*60
number_of_sequences =3
X = tf.placeholder("float", [None, input_length, number_of_sequences])
x = tf.reshape(X, [-1, input_length*number_of_sequences])
x = tf.split(x, input_length, 1)
now x is a list of length 30*60 and an element of a list is in the shape of [batch_size 3]. Now x is in the shape that is required by the tf static_rnn method.
Hope this helps.
Upvotes: 1