0%

implement of cross entropy loss

首先来看看交叉熵函数公式是怎么样的

此方法多用于分类问题,也就是每个种类概率为多少,最后得出一个向量vector
可以看看这篇文章的解释
https://d2l.ai/chapter_linear-networks/softmax-regression.html

The component corresponding to particular instance’s category is set to 1 and all other components are set to 0. In our case, a label $y$ would be a three-dimensional vector, with $(1, 0, 0)$ corresponding to “cat”, $(0, 1, 0)$ to “chicken”, $(0, 0, 1)$ and to “dog”

大致意思是用向量来表示每个物种
如果我们测出了一个图片的分类概率是 $\hat{y} = (0.2,0.3,0.5)$,而实际上这个物种是一个狗,也就是 $y= (0,0,1)$,那么预测值与实际值的损失是多少呢?
那么就要用到上面的cross-entropy,交叉熵函数了
把上面的向量带入公式,可以得到
那么观察可以发现,除了真实项,其它都为0,那么设计代码的时候可以直接预测值里面那个与实际项对应的数求对数

1
2
3
4
5
def cross_entropy(y_hat, y):
return - np.log(y_hat[range(len(y_hat)), y])
y_hat = np.array([[0.1, 0.3, 0.6], [0.3, 0.2, 0.5],[0.2,0.7,0.1],[0.8,0.1,0.1]])
y=(1,2,0,1)
cross_entropy(y_hat, y)

y代表每一组样本中,实际值,比如1代表$(0,1,0)$,2代表$(0,0,1)$
显示结果就是
array([1.2039728, 0.6931472, 1.609438 , 2.3025851])
可以用计算器算一下-log(0.3),-log(0.5),就是上面的结果