Keras LSTM multiclass classification

前端 未结 1 1008
夕颜
夕颜 2021-01-18 09:18

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

    model = Sequential()
    model.add(Embedding(5000, 32, i         


        
相关标签:
1条回答
  • 2021-01-18 10:04

    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
    _________________________________________________________________
    
    0 讨论(0)
提交回复
热议问题