Following is code from https://machinelearningmastery.com/how-to-develop-rnn-models-for-human-activity-recognition-time-series-classification/ which uses LSTM and CNN with TimeDistributed for human activity timeseries:

def evaluate_model(trainX, trainy, testX, testy):
    # define model
    verbose, epochs, batch_size = 0, 25, 64
    n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
    # reshape data into time steps of sub-sequences
    n_steps, n_length = 4, 32
    trainX = trainX.reshape((trainX.shape[0], n_steps, n_length, n_features))
    testX = testX.reshape((testX.shape[0], n_steps, n_length, n_features))
    # define model
    model = Sequential()
    model.add(TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu'), input_shape=(None,n_length,n_features)))
    model.add(TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu')))
    model.add(Dense(100, activation='relu'))
    model.add(Dense(n_outputs, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    # fit network
    model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
    # evaluate model
    _, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
    return accuracy

However, I want to modify above code for a simple time series data (1000 rows and 100 columns), where each row is a time series of 100 values (shown as columns). The output is a class and there are 7 types of classes (hence n_output=7). It is a "sequence classification" problem.

What change should I make in above function for this? Do I need n_steps and n_length (since my data is simpler than human activity recognition data)? For simple CNN1D, I have to reshape as follows:

trainX = trainX.reshape(nrows, ncols, 1)
# then use (ncols, 1) as input shape

How should I reshape here?

I tried by removing n_steps and n_length and putting input_shape as (100, 1) (after reshaping data to (1000,100,1)) but it did not work.


1 Answer 1


For LSTM in tensorflow the tensor has three inputs. So, let's assume we have: [samples, time steps, features]. This means that you have n number of samples, and each sample is divided in m time steps. All of the samples have the same number and the same features.

So in your case n_steps, n_length = 4, 32 means that n_steps are going to be taken in the data with 32 samples, meaning each 4samples are fed into the LSTM at one single point of time. Or 8 different subsets are going to be fed into the LSTM. LSTM usually takes subsets(not single row-samples!)

According to your reshape (1000,100,1) you say that you have 1000 samples(imagine them as subsets of the dataset) and each of these 1000 subsets you divide into 100 time steps. This means that in each of the 1000 subsets, you must have at least 100 samples or a number divisible by 100, otherwise, it won't work. The third parameter "1" is referring to the number of features in your dataset/subset. When you work with accelerometer data I don't believe that you work with only one feature. My guessing is that there you need to have at least 3 features(x,y, and z) or maybe more.

Please check their documentation site: https://www.tensorflow.org/tutorials/sequences/recurrent

  • $\begingroup$ I am not using "accelerometer data". Consider that I am working on music audio signals, each of which is a time series of 1000 values (frequency every millisec). I have hundred such series, each of which is labelled one of 7 types of musical instruments. How do I proceed? $\endgroup$
    – rnso
    Dec 19, 2018 at 12:18
  • $\begingroup$ If I got you correct, then you would need to have samples = 100 (because you have hundred such series), and the 1000values per one series(if you want to go with a time step of 1ms) and only 1 feature with the Hz measure of the frequency of the audio. Then the input shape would be (100, 1000, 1) where 1 is just the frequency measure. The output shape should be with (100x1000(or whatever time step you choose), 7) because the LSTM makes the overall predictions you have on each time step(usually it is not only one row). So input (100, 1000, 1) and output(100x1000, 7) $\endgroup$ Dec 19, 2018 at 12:55
  • $\begingroup$ I want only one output per row. How can I get that? $\endgroup$
    – rnso
    Dec 19, 2018 at 13:50
  • $\begingroup$ Will input shape of (100, 1, 1000) and output of (100,7) work? $\endgroup$
    – rnso
    Dec 19, 2018 at 16:10
  • $\begingroup$ 100x1000 will give you exactly what you want for input, and output should be (100x1000, 7) $\endgroup$ Dec 19, 2018 at 18:35

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.