I have time series data with variable sequence lengths. So something like:

date        value label
2020-01-01  2     0     # first input time series
2020-01-02  1     0     # first input time series
2020-01-03  1     0     # first input time series
2020-01-01  3     1     # second input time series
2020-01-03  1     1     # second input time series

how is it possible to create a training dataset (numpy arrays) of shape [samples, time_steps, n_features] when time_steps is not consistent?

Additional Info: The model that is going to be trained is an LSTM which is capable to handle variable input lengths.

  • $\begingroup$ What would be the needed dataset in your example ? $\endgroup$
    – manu190466
    Apr 21, 2020 at 17:28

1 Answer 1


I solved it the following way:

  1. zero padding all time series that are smaller than the longest one
  2. adding a Masking()layer with masking_value=0. which ignores all zero values before feeding the network.

The part of the model with the Masking layer looks like the following:

model = keras.Sequential()
model.add(layers.Masking(mask_value=0., input_shape=(None, 1)))

Additional Info: Implementing the Masking Layer for a Model with two separate Inputs was kind of unconvinient with a the Keras Functional API, therefore i implemented that part of the model with the Keras Sequential API and connected it to the rest of the model which is implemented with the Functional API.


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.