张量流中定义的损失函数? [英] defined loss function in tensorflow?

查看:34
本文介绍了张量流中定义的损失函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,负实例远远多于正实例,所以我想给出更大权重的正实例.我的目标是:

In my project, the negative instance is far more than positive instance, so I want to give positive instance with a larger weight. my target is:

loss = 0.0
if y_label==1:loss += 100 * cross_entropy
else:loss += cross_entropy

如何在 tensorflow 中实现这一点[?]

How to realizate this in tensorflow[?]

推荐答案

losses 成为批处理中示例的损失值向量(等级 1 张量).并让 y 为对应标签的向量.然后你可以通过

Let losses to be a vector (rank-1 tensor) of loss values for the examples in your batch. And let y be the the vector of corresponding labels. You could then achieve the result you want by

weights = w_pos*y + w_neg*(1.0-y)
loss = tf.reduce_mean(weights*losses)

这里,w_posw_neg 是常量标量值(w_pos=100.0w_neg=1.0 在你的例子).然后,向量 weights 的值为 w_pos,例如标签等于 1 和 w_neg 等于 0.然后乘以 weights element-wise with losses 根据相应的标签对losses中的值进行加权,然后取平均值.

Here, w_pos and w_neg are constant scalar values (w_pos=100.0 and w_neg=1.0 in your example). The vector weights then has a value of w_pos for examples where the label equals 1 and w_neg where it equals 0. You then multiply weights element-wise with losses to weigh the values in the losses according to the corresponding labels and then take the mean.

这篇关于张量流中定义的损失函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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