Say I want to predict the final size of a flower depending on the raining and temperature during 200 days. The flowers are in differents towns, so each flower has its own conditions of rain and temperature.

I have :

  • 500 flowers

For each flower :
- 1 time serie with 200 points corresponding to raining volume each day
- 1 time seris with 200 points corresponding to temperature on each day

  • known target : final length for each flower (500 data)

what should be the input dimensions of my LTSM network ?


1 Answer 1


What you're looking for is covered in the keras LSTM examples:

from keras.layers import LSTM, Dense
import numpy as np

data_dim = 16
timesteps = 8
num_classes = 10

# expected input data shape: (batch_size, timesteps, data_dim)
model = Sequential()
model.add(LSTM(32, return_sequences=True,
               input_shape=(timesteps, data_dim)))  # returns a sequence of vectors of dimension 32
model.add(LSTM(32, return_sequences=True))  # returns a sequence of vectors of dimension 32
model.add(LSTM(32))  # return a single vector of dimension 32
model.add(Dense(10, activation='softmax'))


# Generate dummy training data
x_train = np.random.random((1000, timesteps, data_dim))
y_train = np.random.random((1000, num_classes))

# Generate dummy validation data
x_val = np.random.random((100, timesteps, data_dim))
y_val = np.random.random((100, num_classes))

model.fit(x_train, y_train,
          batch_size=64, epochs=5,
          validation_data=(x_val, y_val))

Source: https://keras.io/getting-started/sequential-model-guide/#examples

In your case, timesteps = 200, data_dim = 2. It sounds like you have a continuous outcome and don't have classes, so you probably want to switch out the last layer to a Dense(1) with a linear activation or something, and switch out your loss and metrics appropriately (probably mean_squared_error and mae or something).

Also please note the architecture is purely an example, you may not need stacked LSTMs (or even LSTMs) for your example, you may want dropouts, you may want more dense layers, etc. This is just an example to get you started,

  • 1
    $\begingroup$ Many Thanks @Andy for pointing me towards the proper part of the doc. For all others who have difficulties understanding shape parameters and usage with LTSM, I recommand specifically the example selected by Andy because it includes examples for x_train and y_train, which helps for understanding shape requirements. I validated your answer but cannot publicly upvote it since I am to new in this community. $\endgroup$ Apr 27, 2019 at 14:35
  • $\begingroup$ update : I have now been able to upvote your answer @Andy $\endgroup$ Jun 11, 2020 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.