文章目录
基础理论
交叉熵
参考:https://zhuanlan.zhihu.com/p/35709485
常用于分类问题,但是也可以用于回归问题
tensorFlow中的损失函数
BinaryCrosstropy
计算真实标签和预测标签之间的交叉熵损失。
当只有两个标签类别(假定为0和1)时,请使用此交叉熵损失。对于每个示例,每个预测应该有一个浮点值。
tf.keras.losses.BinaryCrossentropy(
from_logits=False, label_smoothing=0, reduction=losses_utils.ReductionV2.AUTO,
name='binary_crossentropy'
)
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()
CategoricalCrossentropy
计算标签和预测之间的交叉熵损失。
当有两个或多个标签类别时,请使用此交叉熵损失函数。我们希望标签以one_hot表示形式提供。如果要以整数形式提供标签,请使用SparseCategoricalCrossentropy损失。
tf.keras.losses.CategoricalCrossentropy(
from_logits=False, label_smoothing=0, reduction=losses_utils.ReductionV2.AUTO,
name='categorical_crossentropy'
)
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