'How do I train a Keras LSTM model on a sequence where every time step is labelled?
I'm trying to train a Keras model on some time sequence data with 5 features, I only have one sequence but it's quite long an every time step is labelled with a binary classification. What I've got so far is like this:
# Set up the model
model = Sequential()
model.add(LSTM(16, input_dim=5, activation="relu"))
model.add(Dense(8, activation="relu"))
model.add(Dense(4, activation="relu"))
model.add(Dense(1, activation="sigmoid"))
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
# X.shape == (1, 1000, 5)
# y.shape == (1000, 1)
# Fit the model
model.fit(X, y, epochs=10)
It's a very basic setup, I'm just trying to get the hang of the method I would use to do this in practice.
X contains the time series inputs (a sequence of 1000 time steps, 5 features each)
y contains a list of labels, one for each time step in X
Currently when I run this code I get the error:
ValueError: Data cardinality is ambiguous:
x sizes: 1
y sizes: 252
Solution 1:[1]
This is maybe a duplicate of this question.
In any case, you should reshape your data, as the error says, the first dimensions of $X$ and $y$ are different.
You can redefine your $y$ as
y = y.reshape(1,-1)
Hence you are sure now that the first dimensions of both $X$ and $y$ are the same.
NOTE: this should be the batch dimension, so you can also reshape both $X$ and $y$ to have an empty dimension there.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Oscar |
