我有这个代码适用于二进制分类.我已经为keras imdb数据集测试了它.
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)
我需要将上面的代码转换为多类别分类,总共有7个类别.在阅读了几篇文章以转换上面的代码之后,我理解了什么,我必须改变
model.add(Dense(7, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
显然,改变两行以上是行不通的.我还需要做些什么来使代码适用于多类分类.另外我认为我必须将类更改为一个热编码但不知道如何在keras中.
是的,您需要一个热门目标,您可以使用它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 # ================================================================= 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 _________________________________________________________________