交
叉
熵
损
失
函
数
−
C
r
o
s
s
E
n
t
r
o
p
y
L
o
s
s
交叉熵损失函数-CrossEntropyLoss
交叉熵损失函数−CrossEntropyLoss
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200905171544471.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMzc1MzE4,size_16,color_FFFFFF,t_70#pic_center)
Numpy实现
a =np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]],)
b =np.array([[.9, .05, .05], [.05, .89, .06], [.05, .01, .94]])
np.average(-np.sum(a*np.log(b),axis=1))
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200905190338110.png#pic_center)
tf2.0实现CategoricalCrossentropy
import tensorflow as tf
cce = tf.keras.losses.CategoricalCrossentropy()
loss = cce(
[[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]],
[[.9, .05, .05], [.05, .89, .06], [.05, .01, .94]])
print('Loss: ', loss.numpy())
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200905190318455.png#pic_center)
tf2.0实现SparseCategoricalCrossentropy
在这里插入代码片