tf.keras.losses.sparse_categorical_crossentropy和tf.keras.losses.SparseCategoricalCrossentropy

1 tf.keras.losses.sparse_categorical_crossentropy

是一个函数。

返回每个样本的损失。

等价于:

tf.keras.backend.sparse_categorical_crossentropy

2  tf.keras.losses.SparseCategoricalCrossentropy

是一个类。

可以返回每个样本的损失,可以返回所有样本的累加损失,可以返回样本的平均损失。

参数 reduction:

reduction=tf.keras.losses.Reduction.NONE时,返回每个样本的损失。
reduction=tf.keras.losses.Reduction.SUM时,返回所有样本的累加损失。
reduction=tf.keras.losses.Reduction.SUM_OVER_BATCH_SIZE时,返回平均损失。
reduction=tf.keras.losses.Reduction.AUTO时,几乎和reduction=tf.keras.losses.Reduction.SUM_OVER_BATCH_SIZE效果一样。

文档:

* `AUTO`: Indicates that the reduction option will be determined by the usage
   context. For almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When
   used with `tf.distribute.Strategy`, outside of built-in training loops such
   as `tf.keras` `compile` and `fit`, we expect reduction value to be
   `SUM` or `NONE`. Using `AUTO` in that case will raise an error.

3 程序验证

import tensorflow as tf
import random
import numpy as np

seed = 42
random.seed(seed)
np.random.seed(seed)
tf.random.set_seed(42)
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
print(gpus)
for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu, True)

strategy = tf.distribute.get_strategy()
with strategy.scope():
    labels = tf.constant(np.random.randint(0, 2, (3,)))
    print(labels)  # tf.Tensor([0 1 0], shape=(3,), dtype=int64)
    print(labels.shape)  # (3,)
    pred = tf.constant(np.random.randn(3, 10))
    # print(pred)
    print(pred.shape)  # (3, 10)

    # tf.Tensor([2.1810578  2.85535032 2.57512316], shape=(3,), dtype=float64)
    print(tf.keras.backend.sparse_categorical_crossentropy(labels, pred, from_logits=True))

    loss_obj = tf.keras.losses.SparseCategoricalCrossentropy(reduction=tf.keras.losses.Reduction.SUM_OVER_BATCH_SIZE,
                                                             from_logits=True)
    # tf.Tensor(2.537177085876465, shape=(), dtype=float64)
    print(loss_obj(labels, pred))

    loss_obj = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
    # tf.Tensor(2.537177085876465, shape=(), dtype=float64)
    print(loss_obj(labels, pred))

    loss_obj = tf.keras.losses.SparseCategoricalCrossentropy(reduction=tf.keras.losses.Reduction.NONE, from_logits=True)
    # tf.Tensor([2.18105769 2.85535026 2.57512307], shape=(3,), dtype=float64)
    print(loss_obj(labels, pred))

    loss_obj = tf.keras.losses.SparseCategoricalCrossentropy(reduction=tf.keras.losses.Reduction.SUM, from_logits=True)
    # tf.Tensor(7.6115312576293945, shape=(), dtype=float64)
    print(loss_obj(labels, pred))

    loss_obj = tf.keras.losses.SparseCategoricalCrossentropy(reduction=tf.keras.losses.Reduction.AUTO, from_logits=True)
    # tf.Tensor(2.537177085876465, shape=(), dtype=float64)
    print(loss_obj(labels, pred))

    # tf.Tensor([2.1810578  2.85535032 2.57512316], shape=(3,), dtype=float64)
    print(tf.keras.losses.sparse_categorical_crossentropy(labels, pred, from_logits=True))



 

4 其他

tf.keras.losses.binary_crossentropy返回平均损失。
tf.keras.backend.binary_crossentropy返回每个样本的损失。

 

 

 

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忧郁的常凯申

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值