tf.keras.losses.binary_crossentropy()

介绍

二分类交叉熵函数

注意: 从源代码可知,对 二分类交叉熵 又做一次 平均值

源代码网址:https://github.com/tensorflow/tensorflow/blob/r2.0/tensorflow/python/keras/losses.py#L981-L994

源代码如下:

from tensorflow.keras import backend as K

@keras_export('keras.metrics.binary_crossentropy',
              'keras.losses.binary_crossentropy')
def binary_crossentropy(y_true, y_pred, from_logits=False, label_smoothing=0):  # pylint: disable=missing-docstring
  y_pred = ops.convert_to_tensor(y_pred)
  y_true = math_ops.cast(y_true, y_pred.dtype)
  label_smoothing = ops.convert_to_tensor(label_smoothing, dtype=K.floatx())

  def _smooth_labels():
    return y_true * (1.0 - label_smoothing) + 0.5 * label_smoothing

  y_true = smart_cond.smart_cond(label_smoothing,
                                 _smooth_labels, lambda: y_true)
  return K.mean(
      K.binary_crossentropy(y_true, y_pred, from_logits=from_logits), axis=-1)

在返回时调用K.mean( K.binary_crossentropy() ),

二分类交叉熵 是通过调用K.binary_crossentropy()实现的,关于该函数详见:
https://www.malaoshi.top/show_1EF5aAhO2wh7.html

对上面结果又做一次平均值K.mean( ),关于该函数详见:
https://www.malaoshi.top/show_1EF5aAJyDs39.html

常用别名

  • tf.losses.binary_crossentropy

语法

tf.keras.losses.binary_crossentropy(
    y_true,
    y_pred,
    from_logits=False,
    label_smoothing=0
)
参数
  • y_true:真实目标值,与y_pred形状相同
  • y_pred:预测值
  • from_logits=False:默认值为False,y_pred是否为 logits 张量。默认情况下,认为 y_pred 是概率分布
  • label_smoothing=0

原文出处:/show_1EF5aAgq0yB5.html