Keras:一键编码的类权重(class_weight) [英] Keras: class weights (class_weight) for one-hot encoding

查看:1359
本文介绍了Keras:一键编码的类权重(class_weight)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在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']

相关:如何设置班级权重在Keras中出现班级不平衡的情况?

推荐答案

我想我们可以改用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.

sample_weight:与x长度相同的可选数组,包含 应用于每个样本的模型损失的权重.如果是 时间数据,您可以传递带有形状的2D数组(样本, sequence_length),以对每个时间步长施加不同的权重 每个样本.在这种情况下,您应确保指定 在compile()中为sample_weight_mode ="temporal".

sample_weight: optional array of the same length as x, containing weights to apply to the model's loss for each sample. In the case of temporal data, you can pass a 2D array with shape (samples, sequence_length), to apply a different weight to every timestep of every sample. In this case you should make sure to specify sample_weight_mode="temporal" in compile().

https://github.com/fchollet /keras/blob/d89afdfd82e6e27b850d910890f4a4059ddea331/keras/engine/training.py#L1392

这篇关于Keras:一键编码的类权重(class_weight)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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