Keras:损失函数为什么必须为每个批处理项目返回一个标量,而不是一个标量? [英] Keras: Why do loss functions have to return one scalar per batch item rather than just one scalar?

查看:106
本文介绍了Keras:损失函数为什么必须为每个批处理项目返回一个标量,而不是一个标量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Keras中编写了一个自定义损失函数,并跳了以下内容:

I'm writing a custom loss function in Keras and just tripped over the following:

为什么Keras损失函数必须为每个批处理项目返回一个标量,而不仅仅是一个标量?

Why do Keras loss functions have to return one scalar per batch item rather than just one scalar?

我关心整个批次的累积损失,而不是每件物品的损失,不是吗?

I care about the cumulative loss for the whole batch, not about the loss per item, don't I?

推荐答案

我想我已经明白了:fit()有一个参数sample_weight,您可以使用该参数为批次中的不同样品分配不同的权重.为了使它起作用,您需要损失函数以返回每批物料的损失.

I think I figured it out: fit() has an argument sample_weight with which you can assign different weights to different samples in the batch. In order for this to work you need the loss function to return the loss per batch item.

这篇关于Keras:损失函数为什么必须为每个批处理项目返回一个标量,而不是一个标量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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