# Input shape for simpler time series in LSTM+CNN

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()
# 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.

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

• 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?
– rnso
Dec 19 '18 at 12:18
• 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) Dec 19 '18 at 12:55
• I want only one output per row. How can I get that?
– rnso
Dec 19 '18 at 13:50
• Will input shape of (100, 1, 1000) and output of (100,7) work?
– rnso
Dec 19 '18 at 16:10
• 100x1000 will give you exactly what you want for input, and output should be (100x1000, 7) Dec 19 '18 at 18:35