tensorflow – 为什么在Keras度量函数中使用axis = -1?

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 – loss: 0.4158 – binary_accuracy: 0.9308 – val_loss: 0.4671 –
val_binary_accuracy: 0.7767

最佳答案 这是你在寻找的内容,在
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在外部函数中定义).此数组在最后一行返回K.mean(score_array)中取平均值.这就是为什么你会看到标量指标而不是张量.中间的线条只是为了在必要时引入面具和重量.

点赞