I refer to the example given at the Keras website here:

from keras.models import Sequential
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'))

model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

# 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))

For a real-world example, what should be y_train and y_val? Should they be the same as x_train and x_val respectively, since they come from the same sequence?

Also, how should I understand data_dim and num_classes?


3 Answers 3


'y_train' and 'y_val' should be whatever it is you are trying to predict. They can be values, classes, or they can be a sequence. The form of what you are trying to predict will influence how you structure a RNN in Keras: Many to one and many to many LSTM examples in Keras.

'data_dim' is the number of features in the dataset. If you are predicting the weather, the features may be 'pressure', 'temperature, 'precipitation', and so 'data_dim' = 3.

'num_classes' is the number of classes that the outcome variable can assume. If the outcome variable is 'type of animal', and the classes the outcome variable can assume are 'dog', 'cat', 'bird', then 'num_classes' = 3.

  • $\begingroup$ Is y_train supposed to have the same shape as x_train ? $\endgroup$
    – Leevo
    Jul 9, 2019 at 16:40
  • $\begingroup$ Not generally. The row (or example) dimension should be the same but otherwise, the shapes will not typically be the same. If you are trying to predict a sequence, then that dimension can sometimes be the same. $\endgroup$ Jul 10, 2019 at 20:37

This sort of answers my question.

  • 1
    $\begingroup$ While the link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. $\endgroup$
    – tuomastik
    Sep 3, 2018 at 6:15

There is no necessity that Y_out should be of same shape as input sequence . however u must predict for each input u have given For ex lets say u have a data with 2 samples each sample is basically a sequence with 4 time steps i.e [2,3,4,5],[3,5,7,9] the above problem will have two output for each sequence ,the out put could be the next val or next series of value whatever u want the above can be think of as 1 sample with 8 time steps i.e [2,3,4,5,3,5,7,9] in this case we have 1 out put could be anything(next value or multiples value) dimension basically represent the feature of the input data in other word at a given observation in a time step what have u got classes represent the different categories Y out can belongs to.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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