Home > Back-end >  LSTM with multiple input features and multiple outputs
LSTM with multiple input features and multiple outputs

Time:11-04

Given 30 timestamps with each having 3 features, I want to predict one single output containing 4 different quantities.

I have an X_train and y_train of shape (72600, 30, 3) and (72600, 4) respectively.

where for X_train,

  • 72600 represents the number of samples
  • 30 represents the number of timestamps considered
  • 3 represents the number of features for each timestamp

e.g. X_train[0] will look something like this :

[
    [1,2,3],
    [4,5,6],
    ... such 30 rows 
]

and in y_train, 4 represents the number of outputs to be predicted.

I tried the following code,

model = Sequential()
model.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], X_train.shape[2])))
model.add(Dropout(0.2))
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))
model.add(Dense(units = 4))

The output which I get from this model after passing a single sample of size (1, 30, 3) is of shape: (1, 30, 4) but I just want an output of shape (1, 4).

So how can I do that?

CodePudding user response:

In your last LSTM layer, you will have to set the return_sequences parameter to False in order to get an 1D output:

import tensorflow as tf

model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(units = 50, return_sequences = True, input_shape = (30, 3)))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.LSTM(units = 50, return_sequences = True))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.LSTM(units = 50))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(units = 4))

model(tf.random.normal((1, 30, 3)))
<tf.Tensor: shape=(1, 4), dtype=float32, numpy=
array([[-1.3130311e-03,  1.0584719e-02, -6.3279571e-05, -2.3087783e-02]],
      dtype=float32)>

So, instead of returning a sequence given a sequence, your last LSTM layer returns the output state of only the last LSTM cell.

  • Related