'Keras LSTM Multiclass Classification for time series

I am trying to classify my input time-series data in 10 response classes. So I have 10 classes in my response feature.

My input data has 40 features and response(y_train) has 1 feature with 10 classes.

train input shape (4320, 43), train_y shape (4320,)

My LSTM Network looks like following

model = Sequential()
model.add(LSTM(25, dropout=0.2, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(train_X, train_y, epochs=10, batch_size=36,  validation_split =0.05)

And I get an error

Error when checking target: expected dense_21 to have shape (10,) but got array with shape (1,)

I think it is happening because I have 1 feature in my train_y, where the dense output layer is expecting 10 features. How to run my multiclass time series classification with categorical_entropy loss function?

Also, as soon as I change loss function to sparse_categorical_entropy, it runs smooth.

model = Sequential()
model.add(LSTM(25, dropout=0.2, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(10, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(train_X, train_y, epochs=10, batch_size=36,  validation_split =0.05)

Please help me to understand the reason behind it. Also, which loss function shall I use for multiclass classification time series?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source