pythontensorflowkeraslstmlstm-stateful

TensorFlow different input and output shapes for stateful LSTM model


I would like to create a 'Sequential' model (a Time Series model as you might have guessed), that takes 20 days of past data with a feature size of 2, and predict 1 day into the future with the same feature size of 2.

I found out you need to specify the batch size for a stateful LSTM model, so if I specify a batch size of 32 for example, the final output shape of the model is (32, 2), which I think means the model is predicting 32 days into the future rathen than 1.

How would I go on fixing it?

Also, asking before I arrive to the problem; if I specify a batch size of 32 for example, but I want to predict on an input of shape (1, 20, 2), would the model predict correctly or what, since I changed to batch size from 32 to 1. Thank you.


Solution

  • You don't need to specify batch_size. But you should feed 3-d tensor:

    import tensorflow as tf
    from tensorflow.keras.layers import Input, LSTM, Dense
    from tensorflow.keras import Model, Sequential
    features = 2
    dim = 128
    new_model = Sequential([
      LSTM(dim, stateful=True, return_sequences = True),
      Dense(2)
    ])
    
    number_of_sequences = 1000
    sequence_length = 20
    input = tf.random.uniform([number_of_sequences, sequence_length, features], dtype=tf.float32)
    output = new_model(input) # shape is (number_of_sequences, sequence_length, features)
    predicted = output[:,-1] # shape is (number_of_sequences, 1, features)
    

    Shape of (32, 2) means that your sequence length is 32.

    Batch size is a parameter of training (how many sequences should be feeded to the model before backpropagating error - see stochastic graient descent method). It doesn't affect your data (which shoud be 3-d - (number of sequences, length of sequence, feature)).

    If you need to predict only one sequence - just feed tensor of shape (1, 20, 2) to the model.