是的,你需要一个热门目标,你可以使用to_categorical来编码你的目标或一个简短的方法:
model.compile(loss =’sparse_categorical_crossentropy’,optimizer =’adam’,metrics = [‘accuracy’])
这是完整的代码:
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()
摘要
Using TensorFlow backend.
_________________________________________________________________
Layer (type) Output Shape Param #
=======================