2
$\begingroup$

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?

$\endgroup$
0
$\begingroup$

This sort of answers my question.

$\endgroup$
  • $\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 '18 at 6:15
0
$\begingroup$

'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: https://stackoverflow.com/questions/43034960/many-to-one-and-many-to-many-lstm-examples-in-keras/43047615#43047615.

'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.

$\endgroup$
  • $\begingroup$ Is y_train supposed to have the same shape as x_train ? $\endgroup$ – Leevo Jul 9 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$ – from keras import michael Jul 10 at 20:37

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.