Keras均方误差损失层 [英] Keras mean squared error loss layer

查看:358
本文介绍了Keras均方误差损失层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在实现自定义损失层,在此过程中,我偶然发现了Objectives.py文件[1]中均方误差的实现.我知道我对这种损失计算的了解有所遗漏,因为我一直认为平均值是针对每个微型批处理(张量的轴0)中的每个输出分别在样本中完成的,但看来平均值实际上是在最后一个轴上完成,这在单个向量中将意味着它在输出上完成.在我的自定义损失层上工作时,我偶然发现了这一点,因为它需要折算一些输出的损失,而在特定位置的训练输出是特定值.无论如何,我对均方误差的理解不正确吗?为什么Keras会使用最后一个轴,从而将1xn的输出矢量转换为1x1的输出矢量?

I am currently implementing a custom loss layer and in the process, I stumbled upon the implementation of mean squared error in the objectives.py file [1]. I know I'm missing something in my understanding of this loss calculation because I always thought that the average was done separately across the samples for each output in each mini-batch (axis 0 of the tensor) but it appears that the average is actually being done across the last axis, which in a single vector, would mean it's being done across the outputs. I found this by accident while working on my custom loss layer because it requires discounting the loss of a few of the outputs it a training output in a specific place is a specific value. Anyways, is my understanding of the mean squared error incorrect? Why would Keras be using the last axis and thus turning a a 1xn output vector into a 1x1 output vector?

谢谢.

[1] https://github.com/fchollet/keras/blob/master/keras/objectives.py#L7

推荐答案

MSE丢失的相关代码如下:

The code in question for the MSE loss is this:

def mean_squared_error(y_true, y_pred):
    return K.mean(K.square(y_pred - y_true), axis=-1)

首先将y_pred和y_true相减,然后将结果传递给K.square,按预期方式,返回其参数的平方,然后将结果提供给K.mean,计算平均值.

Here first y_pred and y_true are subtracted, then that result is passed to K.square, which as expected, returns the square of its parameter, and then that result is given to K.mean, which computes the mean.

因此,代码显然正在执行其应做的事情.关于为什么要操作最后一个轴,这与类无关,这只是一个约定.请注意,通常,MSE定义中没有类.

So the code clearly is doing what its supposed to do. About why the last axis is operated upon, this has nothing to do with classes, it is just a convention. Note that in general, there are no classes in the MSE definition.

这篇关于Keras均方误差损失层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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