问题描述
我想在keras model.fit中使用class_weight参数来处理不平衡的训练数据.通过查看一些文档,我了解到我们可以通过这样的字典:
I'd like to use class_weight argument in keras model.fit to handle the imbalanced training data. By looking at some documents, I understood we can pass a dictionary like this:
class_weight = {0 : 1,
1: 1,
2: 5}
(在此示例中,class-2在损失函数中将受到更高的惩罚.)
(In this example, class-2 will get higher penalty in the loss function.)
问题是我的网络输出具有一键编码,即class-0 =(1、0、0),class-1 =(0、1、0)和class-3 =(0、0, 1).
The problem is that my network's output has one-hot encoding i.e. class-0 = (1, 0, 0), class-1 = (0, 1, 0), and class-3 = (0, 0, 1).
我们如何将class_weight用于一键编码输出?
How can we use the class_weight for one-hot encoded output?
通过查看 Keras中的某些代码,看起来_feed_output_names
包含一个输出类列表,但在我的情况下,model.output_names
/model._feed_output_names
返回['dense_1']
By looking at some codes in Keras, it looks like _feed_output_names
contain a list of output classes, but in my case, model.output_names
/model._feed_output_names
returns ['dense_1']
推荐答案
我想我们可以改用sample_weights
.实际上,在Keras内部,class_weights
会转换为sample_weights
.
I guess we can use sample_weights
instead. Inside Keras, actually, class_weights
are converted to sample_weights
.
这篇关于Keras:一键编码的类权重(class_weight)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!