Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras LSTM multiclass classification

I have this code that works for binary classification. I have tested it for keras imdb dataset.

    model = Sequential()
    model.add(Embedding(5000, 32, input_length=500))
    model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
    model.add(Dense(1, activation='sigmoid'))
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])        
    print(model.summary())
    model.fit(X_train, y_train, epochs=3, batch_size=64)
    # Final evaluation of the model
    scores = model.evaluate(X_test, y_test, verbose=0)

I need the above code to be converted for multi-class classification where there are 7 categories in total. What I understand after reading few articles to convert above code I have to change

model.add(Dense(7, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])  

Obviously changing just above two lines doesn't work. What else do I have to change to make the code work for multiclass classification. Also I think I have to change the classes to one hot encoding but don't know how in keras.

like image 222
user1670773 Avatar asked Sep 27 '17 08:09

user1670773


People also ask

Is LSTM good for classification?

To train a deep neural network to classify sequence data, you can use an LSTM network. An LSTM network enables you to input sequence data into a network, and make predictions based on the individual time steps of the sequence data.

Can LSTM be used for text classification?

Text classification using LSTMYou can use the full code for making the model on a similar data set. Before processing the model we created a similar pad sequence of the data so that it can be put to the model with the same length. In the modelling, we are making a sequential model.

Which Optimizer is best for multiclass classification?

Multiclass Classification Neural Network using Adam Optimizer.


1 Answers

Yes, you need one hot target, you can use to_categorical to encode your target or a short way:

model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

here is the full code:

from keras.models import Sequential
from keras.layers import *

model = Sequential()
model.add(Embedding(5000, 32, input_length=500))
model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(7, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

model.summary()

Summary

Using TensorFlow backend.
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding_1 (Embedding)      (None, 500, 32)           160000    
_________________________________________________________________
lstm_1 (LSTM)                (None, 100)               53200     
_________________________________________________________________
dense_1 (Dense)              (None, 7)                 707       
=================================================================
Total params: 213,907
Trainable params: 213,907
Non-trainable params: 0
_________________________________________________________________
like image 134
大宝剑 Avatar answered Sep 30 '22 00:09

大宝剑