I have for each day sensor timeseries data. I just ask myself how to train with that a LSTM eg. for classification? Since I would like to have the LSTM train on all examples and not just one?

I just see examples where LSTM are trained on one timeseries


2 Answers 2


Firstly, LSTM is perfect for time series as a sequence model. You can do classification with LSTM. Let's say you have 3 classes in your labels. You want the probabilities of each class as predictions. What you can do is to have a usual neural network combined with the LSTM at the end. The last layer will have 3 nodes/neuron which you will use 'softmax' activation function for the classification task. Here I share my Keras code for my multclassification task as an example:

nn = Sequential()
nn.add(LSTM(120, batch_input_shape=(64,100,18), return_sequences=True))
nn.add(Dense(100, activation='relu'))
nn.add(Dense(40, activation='relu'))
nn.add(Dense(10, activation='relu'))
nn.add(Dense(3, activation = 'softmax'))

opt = Adam(lr = 0.001, decay = 1e-5)
nn.compile(loss='categorical_crossentropy', optimizer= 'adam')

We have 2 LSTM layers, and 4 layers of DNN, yet you could just use the last layer of DNN to get an output. If you want binary classification, just change the last layer with:

nn.add(Dense(1, activation = 'sigmoid'))

And you will have if the probability of the class 1 occurring at the output. If you do not know Dropout, do not get scared, it is some strategy for dealing with overfitting.

If you have any further questions, I will be around. Please do not hesitate to ask.


Do you mean you mave multiple sensors? Ir one single sensor that record one measurement every day?

In short, you will need to simply match the dimensions of your data to the LSTM. To do that, you design your LSTM to consume tensors of the correct shape.

Let's make the following assumptions:

  1. You have 2 sensors
  2. Each sensor records one measurement per hour (so 24 per day)
  3. You want to consider blocks of 5 consecutive hours to predict the 6th hour

Your LSTM shape would have the following input_shape shape:

(5, 2)

You can pass any number of these sample blocks as a batch, using a batch_size parameter.

So it summarises to this format:

(batch_size, num_time_steps, num_features)

This is a brief introduction that hopefully generalises the idea for you. I would suggest looking at more examples to start getting a feeling for the required dimensions and the terminology that people generally use.


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.