Search code examples
keraslstmmany-to-onerecurrent-neural-network

Many to one LSTM, multiclass classification


I am trying to train an LSTM-RNN with 64 hidden units. My data is the following:

input: numpy array with dimensions (170000, 50, 500) -> (examples, time steps, number of features)
output: numpy array with dimensions (170000, 10)

The output is a categorical variable with 10 classes (e.g., class 1 corresponds to the vector [1,0,0,0,0,0,0,0,0,0])

So far i have tried this code but an error appears stating that the Dense layer should have a 3D input.

model = Sequential()
model.add(LSTM(64, input_shape=(50, 500), return_sequences=True,dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(units = 10,activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.fit(input, output, epochs=1, batch_size=64)

The only thing that seems to work is changing the output so it has the following form: (170000,50,10), which has basically all zeros except for the 50th timestep.

Is this a correct approach? If so, is there something more efficient? I am concerned about the fact that expanding the outputs' shape might make the code less efficient.


Solution

  • All you have to do is change return_sequences=True to return_sequences=False. Also if each item can only fit in one class you need to change your activation function in the output layer to activation='softmax'