keras版本:2.0.8
在某些Keras度量函数和损失函数中,使用axis = -1作为参数。
例如:
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)
y_pred的形状:(4,256,256,2)
所以,binary_accuracy(y_true,y_pred)应该返回一个shape =(4,256,256)而不是标量张量的张量。
但是当使用binary_accuracy作为度量函数时:
model.compile(optimizer=adam, loss=keras.losses.binary_crossentropy, metrics=[binary_accuracy])
日志仍然将binary_accuracy打印为标量,这让我很困惑。
keras是否在返回binary_accuracy函数方面做了一些特别的事情?
Epoch 11/300
0s - 损失:0.4158 - binary_accuracy:0.9308 - val_loss:0.4671 - val_binary_accuracy:0.7767
答案 0 :(得分:1)
以下是training.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)
# 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)
度量函数由score_array = fn(y_true, y_pred)
调用(它是一个嵌套函数,fn
在外部函数中定义)。此数组在最后一行return K.mean(score_array)
中取平均值。这就是为什么你会看到标量指标而不是张量的原因。中间的线条只是为了在必要时引入面具和重量。