为什么在 Keras 指标函数中使用axis=-1? [英] Why use axis=-1 in Keras metrics function?
问题描述
keras 版本:2.0.8
keras version:2.0.8
在一些 Keras 度量函数和损失函数中,使用axis=-1 作为参数.
In some Keras metric functions and loss functions, use axis=-1 as parameter.
例如:
def binary_accuracy(y_true, y_pred):
return K.mean(K.equal(y_true, K.round(y_pred)), axis=-1)
就我而言:
y_true 的形状:(4,256,256,2)
shape of y_true:(4,256,256,2)
y_pred 的形状:(4,256,256,2)
shape of y_pred:(4,256,256,2)
因此,binary_accuracy(y_true, y_pred) 应该返回一个 shape=(4,256,256) 的张量,而不是一个标量张量.
So, binary_accuracy(y_true, y_pred) should return a tensor with shape=(4,256,256) instead of a scalar tensor.
但是当使用 binary_accuracy 作为度量函数时:
But when use binary_accuracy as metric function:
model.compile(optimizer=adam, loss=keras.losses.binary_crossentropy, metrics=[binary_accuracy])
日志仍然将 binary_accuracy 打印为标量,这让我很困惑.
The log still prints binary_accuracy as scalar,which confused me a lot.
keras 是否对 binary_accuracy 函数的返回做了一些特殊处理?
Does keras do some special on the return of binary_accuracy function?
纪元 11/300
0s - 损失:0.4158 - binary_accuracy:0.9308 - val_loss:0.4671 -val_binary_accuracy: 0.7767
0s - loss: 0.4158 - binary_accuracy: 0.9308 - val_loss: 0.4671 - val_binary_accuracy: 0.7767
推荐答案
这里是您要找的内容,training_utils.py:
Here's what you're looking for, inside training_utils.py:
def weighted(y_true, y_pred, weights, mask=None):
"""Wrapper function.
# Arguments
y_true: `y_true` argument of `fn`.
y_pred: `y_pred` argument of `fn`.
weights: Weights tensor.
mask: Mask tensor.
# Returns
Scalar tensor.
"""
# score_array has ndim >= 2
score_array = fn(y_true, y_pred)
if mask is not None:
# Cast the mask to floatX to avoid float64 upcasting in Theano
mask = K.cast(mask, K.floatx())
# mask should have the same shape as score_array
score_array *= mask
# the loss per batch should be proportional
# to the number of unmasked samples.
score_array /= K.mean(mask) + K.epsilon()
# apply sample weighting
if weights is not None:
# reduce score_array to same ndim as weight array
ndim = K.ndim(score_array)
weight_ndim = K.ndim(weights)
score_array = K.mean(score_array,
axis=list(range(weight_ndim, ndim)))
score_array *= weights
score_array /= K.mean(K.cast(K.not_equal(weights, 0), K.floatx()))
return K.mean(score_array)
return weighted
度量函数由 score_array = fn(y_true, y_pred)
调用(它是一个嵌套函数,fn
在外部函数中定义).该数组在最后一行 return K.mean(score_array)
中取平均值.这就是为什么您看到的是标量指标而不是张量.中间的线只是在必要时引入掩码和权重.
The metric function is called by score_array = fn(y_true, y_pred)
(it's a nested function and fn
is defined in the outer function). This array is averaged in the last line return K.mean(score_array)
. That's why you're seeing scalar metrics instead of tensors. The lines in between are just to introduce masks and weights if necessary.
这篇关于为什么在 Keras 指标函数中使用axis=-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!