对于:
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。
则得到的仍然是一个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