Module: tf.keras.losses 理解

本文主要整理记录对tf2.0中损失函数模块的理解。tf中实现的所有损失都是python中的class

损失函数理解

    • BinaryCrossentropy
      • 参数解释
    • CategoricalCrossentropy
      • 参数解释
    • SparseCategoricalCrossentropy
      • 参数解释

BinaryCrossentropy

tf.keras.losses.BinaryCrossentropy:

tf.keras.losses.BinaryCrossentropy(
    from_logits=False, label_smoothing=0, reduction=losses_utils.ReductionV2.AUTO,
    name='binary_crossentropy'
)

当处理二分类问题时使用该交叉熵损失。对于每一个样本的预测值都应该是浮点型的数值(floating)
下面给的例子中,y_predy_true都有batch_size的维度

>>>y_true = [[0., 1.], [0., 0.]]
>>>y_pred = [[0.6, 0.4], [0.4, 0.6]]
>>># Using 'auto'/'sum_over_batch_size' reduction type.
>>>bce = tf.keras.losses.BinaryCrossentropy()
>>>bce(y_true, y_pred).numpy()
0.815

这里使用默认的reduction方式

 >>># Using 'sum' reduction type.
 >>>bce = tf.keras.losses.BinaryCrossentropy(
     reduction=tf.keras.losses.Reduction.SUM)
 >>>bce(y_true, y_pred).numpy()
 1.630

这里指定使用SUM的reduction方式;注意对比默认的方式和这种方式的区别。

>>># Using 'none' reduction type.
>>>bce = tf.keras.losses.BinaryCrossentropy(
    reduction=tf.keras.losses.Reduction.NONE)
>>>bce(y_true, y_pred).numpy()
array([0.916, 0.714], dtype=float32)

注意这种方式和之前两种方式的区别。

tf.keras API一起使用的语法:

model.compile(optimizer='sgd', loss=tf.keras.losses.BinaryCrossentropy())

参数解释

  1. from_logits: 是否要把y_pred转变成由logit value组成的张量(为什么要转变成logit呢?后续在研究)。默认情况把y_pred中的每个值理解成概率(即取值范围为 [ 0 , 1 ] [0,1] [0,1])。注意,某些情况下from_logits=True会数值稳定些(除了这点,其余的和True有什么区别呢)。
  2. label_smoothing: 通常情况使用默认值,这里不细讲,有需求再看。
  3. reduction: 了解这个参数的含义就够了,通常情况使用默认值。

CategoricalCrossentropy

tf.keras.losses.CategoricalCrossentropy:

tf.keras.losses.CategoricalCrossentropy(
    from_logits=False, label_smoothing=0, reduction=losses_utils.ReductionV2.AUTO,
    name='categorical_crossentropy'
)

当涉及到两个或者更多个标签类别的时候使用该损失函数。标签应该以one_hot的形式被提供。如果想将标签表示为整数类型,请使用SparseCategoricalCrossentropy损失函数。
在下面举的例子中,每个样本的预测值都应该包含# classes个浮点型数值。y_predy_true的形状都是[batch_size, num_classes]

>>>y_true = [[0, 1, 0], [0, 0, 1]]
>>>y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
>>># Using 'auto'/'sum_over_batch_size' reduction type.
>>>cce = tf.keras.losses.CategoricalCrossentropy()
>>>cce(y_true, y_pred).numpy()
1.177
>>># Using 'sum' reduction type.
>>>cce = tf.keras.losses.CategoricalCrossentropy(
    reduction=tf.keras.losses.Reduction.SUM)
>>>cce(y_true, y_pred).numpy()
2.354

配合keras API的语法:

model.compile(optimizer='sgd', loss=tf.keras.losses.CategoricalCrossentropy())

参数解释

  1. from_logits: 是否要把y_pred转变成由logit value组成的张量(为什么要转变成logit呢?后续在研究)。默认情况把y_pred中的每个值理解成概率(即取值范围为 [ 0 , 1 ] [0,1] [0,1]),即我们期望y_pred表示一个概率分布。注意,某些情况下from_logits=True会数值稳定些(除了这点,其余的和True有什么区别呢)。
  2. label_smoothing: 通常情况使用默认值,这里不细讲,有需求再看。
  3. reduction: 了解这个参数的含义就够了,通常情况使用默认值。

SparseCategoricalCrossentropy

tf.keras.losses.SparseCategoricalCrossentropy(
    from_logits=False, reduction=losses_utils.ReductionV2.AUTO,
    name='sparse_categorical_crossentropy'
)

当涉及到两个或者多个标签类别的时候使用该损失函数。标签应该以整数的形式呈现,如果想将标签以one_hot的形式呈现,使用CategoricalCrossentropy损失函数。对于每个预测值,应该有# classes个浮点型数值。下面举的例子中,y_true的形状是[batch_size]y_pred的形状是[batch_size, num_classes]

>>>y_true = [1, 2]#注意这里和CategoricalCrossentropy的区别,可以说是最重要的区别了
>>>y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]#这里和CategoricalCrossentropy没区别
>>># Using 'auto'/'sum_over_batch_size' reduction type.
>>>scce = tf.keras.losses.SparseCategoricalCrossentropy()
>>>scce(y_true, y_pred).numpy()
1.177

这个损失函数和CategoricalCrossentropy的最大区别在于y_true的表达形式

参数解释

和CategoricalCrossentropy的参数含义完全相同。

其余损失函数后续再整理。

你可能感兴趣的:(tensorflow,tensorflow,深度学习)