【keras】有关loss function的定义-返回的是`矩阵`还是`标量`

对于:

mean_absolute_error

源码中:

def mean_absolute_error(y_true, y_pred):
    return K.mean(K.abs(y_pred - y_true), axis=-1)

注意⚠️:axis=-1是默认的。
查看K.mean以及K.abs:

help(K.mean)
Help on function mean in module keras.backend.tensorflow_backend:

mean(x, axis=None, keepdims=False)
    Mean of a tensor, alongside the specified axis.
    
    # Arguments
        x: A tensor or variable.
        axis: An integer or list of integers in [-rank(x), rank(x)),
            the axes to compute the mean. If `None` (default), computes
            the mean over all dimensions.
        keepdims: A boolean, whether to keep the dimensions or not.
            If `keepdims` is `False`, the rank of the tensor is reduced
            by 1 for each entry in `axis`. If `keepdims` is `True`,
            the reduced dimensions are retained with length 1.
    
    # Returns
        A tensor with the mean of elements of `x`.

返回的是一个tensor。

如果两幅图像求mean_absolute_error

则得到的仍然是一个tensor。
举例:

x1.shape, x2.shape

((1, 256, 320, 1), (1, 256, 320, 1))

def mean_absolute_error(y_true, y_pred):
    return K.mean(K.abs(y_pred - y_true), axis=-1)
mean_absolute_error(x1, x2)

<tf.Tensor ‘Mean_2:0’ shape=(1, 256, 320) dtype=float64>
得到的为tensor,而不是一个scalar。

但是

在实际的网络训练过程中,其实只是显示一个标量而已

像下面对于自定义的smooth L1 loss:
定义1:

def l1_smooth_loss(y_true, y_pred):
x = K.abs(y_true - y_pred)
x = tf.where(tf.less(x, 1.0), 0.5 * x ** 2, x - 0.5)
return K.sum(x)

定义2:

def l1_smooth_loss(y_true, y_pred):
x = K.abs(y_true - y_pred)
x = tf.where(tf.less(x, 1.0), 0.5 * x ** 2, x - 0.5)
return K.sum(x, axis=-1)

在训练过程中打印log时显示的是不一样的。
https://github.com/keras-team/keras/blob/master/keras/engine/training_utils.py
函数中weighted_masked_objective(fn):

···
	score_array = fn(y_true, y_pred)
···
	return K.mean(score_array)
···

最后返回的是整体的均值!!!

参考:https://github.com/keras-team/keras/issues/7047

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值