gpt4 book ai didi

python - Keras LSTM 多类分类

转载 作者:太空狗 更新时间:2023-10-30 02:38:37 25 4
gpt4 key购买 nike

我有这段适用于二进制分类的代码。我已经针对 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
_________________________________________________________________

关于python - Keras LSTM 多类分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46443566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com