tf.keras.losses.SparseCategoricalCrossentropy() 稀疏交叉熵 损失函数 示例

import numpy as np 
import tensorflow as tf
y_true = np.array([1, 2])  # 整数分类
y_pred = np.array([[0.05, 0.95, 0], [0.1, 0.8, 0.1]])  # 预测概率
delta = 1e-7
y_true_one_hot = tf.keras.utils.to_categorical( # 独热编码
    y_true,  # 类数组,类值要转换为矩阵(从 0 到 分类数-1 的整数)
    num_classes=3,  # 分类数
    dtype="float32" # 输出数据类型,默认为float32
)
print(y_true_one_hot)
[[0. 1. 0.]
 [0. 0. 1.]]

计算公式

l o s s = 1 n ∑ i t r u e i × ( − l o g e p r e d i + l e − 7 ) loss = \frac{1}{n}\sum_{i}^{}true_{i}\times \left ( -log_{e}^{pred_{i}+le^{-7}}\right ) loss=n1itruei×(logepredi+le7)

n:样本数

true : y_true

pred : y_pred

计算过程

− { [ ( 0 × l o g e 0.5 + l e − 7 ) + ( 1 × l o g e 0.95 + l e − 7 ) + ( 0 × l o g e 0.0 + l e − 7 ) ] + [ ( 0 × l o g e 0.1 + l e − 7 ) + ( 0 × l o g e 0.8 + l e − 7 ) + ( 1 × l o g e 0.1 + l e − 7 ) ] } ÷ 2 = 1.176939193635535 -\left \{[(0 \times log_{e}^{0.5+le^{-7}})+(1 \times log_{e}^{0.95+le^{-7}})+(0 \times log_{e}^{0.0+le^{-7}})]+[(0 \times log_{e}^{0.1+le^{-7}})+(0 \times log_{e}^{0.8+le^{-7}})+(1 \times log_{e}^{0.1+le^{-7}})]\right \}\div 2 = 1.176939193635535 {[(0×loge0.5+le7)+(1×loge0.95+le7)+(0×loge0.0+le7)]+[(0×loge0.1+le7)+(0×loge0.8+le7)+(1×loge0.1+le7)]}÷2=1.176939193635535

tf.keras.losses.SparseCategoricalCrossentropy 实现

scce = tf.keras.losses.SparseCategoricalCrossentropy()
print(scce(y_true, y_pred).numpy())
1.1769392490386963

numpy 实现

y_pred_delta = y_pred+delta  # 添加一个微小值可以防止负无限大(np.log(0))的发生。
print(y_pred_delta)
[[5.0e-02 9.5e-01 1.0e-11]
 [1.0e-01 8.0e-01 1.0e-01]]
y_pred_log = np.log(y_pred_delta)  # log表示以e为底数的自然对数
print(y_pred_log)
[[ -2.99573227  -0.05129329 -25.32843602]
 [ -2.30258509  -0.22314355  -2.30258509]]
print(y_true_one_hot*y_pred_log)
[[-0.         -0.05129329 -0.        ]
 [-0.         -0.         -2.30258509]]
loss = -np.sum(y_true_one_hot*y_pred_log)/2
print(loss)
1.1769392490386963
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
boston_housing module: Boston housing price regression dataset. cifar10 module: CIFAR10 small images classification dataset. cifar100 module: CIFAR100 small images classification dataset. fashion_mnist module: Fashion-MNIST dataset. imdb module: IMDB sentiment classification dataset. mnist module: MNIST handwritten digits dataset. reuters module: Reuters topic classification dataset. import tensorflow as tf from tensorflow import keras fashion_mnist = keras.datasets.fashion_mnist (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() mnist = keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() cifar100 = keras.datasets.cifar100 (x_train, y_train), (x_test, y_test) = cifar100.load_data() cifar10 = keras.datasets.cifar10 (x_train, y_train), (x_test, y_test) = cifar10.load_data() imdb = keras.datasets.imdb (x_train, y_train), (x_test, y_test) = imdb.load_data() # word_index is a dictionary mapping words to an integer index word_index = imdb.get_word_index() # We reverse it, mapping integer indices to words reverse_word_index = dict([(value, key) for (key, value) in word_index.items()]) # We decode the review; note that our indices were offset by 3 # because 0, 1 and 2 are reserved indices for "padding", "start of sequence", and "unknown". decoded_review = ' '.join([reverse_word_index.get(i - 3, '?') for i in x_train[0]]) print(decoded_review) boston_housing = keras.datasets.boston_housing (x_train, y_train), (x_test, y_test) = boston_housing.load_data() reuters= keras.datasets.reuters (x_train, y_train), (x_test, y_test) = reuters.load_data() tf.keras.datasets.reuters.get_word_index( path='reuters_word_index.json' )

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

夏华东的博客

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

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

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

打赏作者

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

抵扣说明:

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

余额充值