如何在Keras中使用fit_generator()加权类? [英] How to weight classes using fit_generator() in Keras?

查看:236
本文介绍了如何在Keras中使用fit_generator()加权类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用keras来拟合CNN模型以对图像进行分类.数据集具有来自某些类别的更多图像,因此其不平衡.

I am trying to use keras to fit a CNN model to classify images. The data set has much more images form certain classes, so its unbalanced.

我在Keras中读到了关于如何权衡损失以解决这一问题的另一件事,例如: https://datascience.stackexchange.com/Questions/13490/如何为喀拉斯中的不平衡类设置权重,对此进行了很好的解释.但是,它总是解释 fit ()函数,而不是 fit_generator ()函数.

I read different thing on how to weight the loss to account for this in Keras, e.g.: https://datascience.stackexchange.com/questions/13490/how-to-set-class-weights-for-imbalanced-classes-in-keras, which is nicely explained. But, its always explaining for the fit() function, not the fit_generator() one.

实际上,在fit_generator()函数中,我们没有' class_weights '参数,但是我们有' weighted_metrics ',我不理解其描述:"weighted_metrics:在训练和测试期间要通过sample_weight或class_weight评估和加权的指标列表."

Indeed, in the fit_generator() function we dont have the 'class_weights' parameter, but instead we have 'weighted_metrics', which I dont understand its description: "weighted_metrics: List of metrics to be evaluated and weighted by sample_weight or class_weight during training and testing."

如何从"class_weights"传递到"weighted_metrics"?有人能举一个简单的例子吗?

How can I pass from 'class_weights' to 'weighted_metrics'? Would any one have a simple example?

推荐答案

根据

Class_weight :可选的字典,将类索引(整数)映射到 权重(浮点)值,用于对损失函数进行加权(在 仅培训).告诉模型多付钱"可能很有用. 注意"来自代表性不足的班级的样本.

Class_weight: Optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class.

假设您有两个类[ positive negative ],则可以通过以下方式将class_weight传递给fit_generator:

Assume you have two classes [positive and negative], you can pass class_weight to fit_generator with:

model.fit_generator(gen,class_weight=[0.7,1.3])

这篇关于如何在Keras中使用fit_generator()加权类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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