简体   繁体   English

Class 权重恶化了我的 keras 分类 model

[英]Class weights worsen my keras classification model

I have a model with that is to classify some data, and it has a target output of 21. It uses the adam optimizer and categorical cross-entropy loss.我有一个 model 用于对一些数据进行分类,它的目标 output 为 21。它使用亚当优化器和分类交叉熵损失。 In an attempt to improve the model loss, I did a visualization of the class frequencies in the data set and found that the top 2 classes have a frequency of about 25,000 and 20,000 while the lowest 2 are about 4, 40. with the other classes ranging from 100, 2000. I realized this is a stark difference in values and attempted to add in-class weights which I extracted using sklearn like so:为了改善 model 损失,我对数据集中的 class 频率进行了可视化,发现前 2 个类别的频率约为 25,000 和 20,000,而最低的 2 个类别约为 4、40。范围从 100 到 2000。我意识到这是值的明显差异,并尝试添加我使用 sklearn 提取的类内权重,如下所示:

My y array is in the one-hot encoding style, something like:我的 y 数组采用 one-hot 编码样式,例如:

class1,   class2, class3, class4 ... class21
   0        0       1       0    ...    0 
   1        0       0       0    ...    0
   0        1       0       0    ...    0
from sklearn.utils.class_weight import compute_class_weight

y_int = np.argmax(y.to_numpy(), axis=1)
weights = compute_class_weight('balanced', classes=np.unique(y_int), y=y_int)
di = dict(enumerate(class_weights))

but my loss worsened and I started getting loss values in the range of 30 - 50, by the 50th epoch.但是我的损失恶化了,到第 50 个时期,我开始获得 30 - 50 范围内的损失值。 which is horrible when compared to the fact that without the class weights I was getting about 0.4.与没有 class 权重的情况相比,我得到了大约 0.4,这太可怕了。

Is there something wrong with the way I extracted the class weights?我提取 class 权重的方式有问题吗? or should I not be using class weights entirely?还是我不应该完全使用 class 权重? If not, what should I be using to account for this huge imbalance?如果不是,我应该用什么来解释这种巨大的不平衡? -Thanks -谢谢

Keras prints the weighted loss during training; Keras 打印训练期间的加权损失; you can confirm that by, eg, doubling all the class weights.您可以通过例如将所有 class 权重加倍来确认这一点。 So the larger loss for the weighted model may just suggest that the smaller classes are more difficult to classify, and now that you're focusing the loss's attention on those smaller classes you see worse scores.因此,加权 model 的较大损失可能只是表明较小的类别更难分类,现在您将损失的注意力集中在那些您看到分数更差的较小类别上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM