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, trainX.shape, trainy.shape # reshape data into time steps of sub-sequences n_steps, n_length = 4, 32 trainX = trainX.reshape((trainX.shape, n_steps, n_length, n_features)) testX = testX.reshape((testX.shape, 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(TimeDistributed(Dropout(0.5))) model.add(TimeDistributed(MaxPooling1D(pool_size=2))) model.add(TimeDistributed(Flatten())) model.add(LSTM(100)) model.add(Dropout(0.5)) 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.