Keras 如何处理多标签分类? [英] How does Keras handle multilabel classification?

查看:30
本文介绍了Keras 如何处理多标签分类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何解释 Keras 在以下情况下的默认行为:

I am unsure how to interpret the default behavior of Keras in the following situation:

我的 Y (ground truth) 是使用 scikit-learn 的 MultilabelBinarizer() 设置的.

My Y (ground truth) was set up using scikit-learn's MultilabelBinarizer().

因此,举一个随机的例子,我的 y 列的一行是这样编码的:[0,0,0,1,0,1,0,0,0,0,1].

Therefore, to give a random example, one row of my y column is one-hot encoded as such: [0,0,0,1,0,1,0,0,0,0,1].

所以我有 11 个可以预测的类,并且不止一个类是真实的;因此问题的多标签性质.这个特定样本有三个标签.

So I have 11 classes that could be predicted, and more than one can be true; hence the multilabel nature of the problem. There are three labels for this particular sample.

我像处理非多标签问题一样训练模型(一切照旧),但没有出现任何错误.

I train the model as I would for a non multilabel problem (business as usual) and I get no errors.

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD

model = Sequential()
model.add(Dense(5000, activation='relu', input_dim=X_train.shape[1]))
model.add(Dropout(0.1))
model.add(Dense(600, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(y_train.shape[1], activation='softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy',])

model.fit(X_train, y_train,epochs=5,batch_size=2000)

score = model.evaluate(X_test, y_test, batch_size=2000)
score

当 Keras 遇到我的 y_train 并看到它是多"one-hot 编码时,它会做什么,这意味着 y_train 的每一行中存在多个一个"?基本上,Keras 会自动执行多标签分类吗?对评分指标的解释有何不同?

What does Keras do when it encounters my y_train and sees that it is "multi" one-hot encoded, meaning there is more than one 'one' present in each row of y_train? Basically, does Keras automatically perform multilabel classification? Any differences in the interpretation of the scoring metrics?

推荐答案

简而言之

不要使用softmax.

使用 sigmoid 激活输出层.

使用 binary_crossentropy 作为损失函数.

Use binary_crossentropy for loss function.

使用 predict 进行评估.

softmax 中,当增加一个标签的分数时,所有其他标签的分数都会降低(这是一个概率分布).当您有多个标签时,您不希望这样.

In softmax when increasing score for one label, all others are lowered (it's a probability distribution). You don't want that when you have multiple labels.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation
from tensorflow.keras.optimizers import SGD

model = Sequential()
model.add(Dense(5000, activation='relu', input_dim=X_train.shape[1]))
model.add(Dropout(0.1))
model.add(Dense(600, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(y_train.shape[1], activation='sigmoid'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='binary_crossentropy',
              optimizer=sgd)

model.fit(X_train, y_train, epochs=5, batch_size=2000)

preds = model.predict(X_test)
preds[preds>=0.5] = 1
preds[preds<0.5] = 0
# score = compare preds and y_test

这篇关于Keras 如何处理多标签分类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆