PyTorch: 交叉熵

1. 导入包

import torch
from torch.nn import functional as F

2. 熵Entropy

#熵
#uncertainty越高,信息越多;entropy越高,信息越少
#Entropy=-P(i)*log2[P(i)]的求和
print("熵示例一:")
a=torch.full([4],0.25)
print(a)#tensor([0.2500, 0.2500, 0.2500, 0.2500])
print(-(a*torch.log2(a)).sum())#tensor(2.),熵最大,所含的信息量最少

b=torch.tensor([0.1,0.1,0.1,0.7])
print(-(b*torch.log2(b)).sum())#tensor(1.3568),熵较大,所含的信息量较少

c=torch.tensor([0.001,0.001,0.001,0.997])
print(-(c*torch.log2(c)).sum())#tensor(0.0342),不确定性大,熵小,所含的信息量最多

3. 交叉熵Cross Entropy

#交叉熵
#当P=Q时,H(p,Q)=H(P)=-P(i)*log2[P(i)].sum()
#当P!=Q时,H(P,Q)=-P(i)*log2[Q(i)].sum()
#二值分类:cat和dog
  #P(dog)=1-P(cat),Q(dog)=1-Q(cat)
  #H(P,Q)=-P(i)*log2[Q(i)].sum()=-P(cat)*log2(cat)-P(dog)*log2(dog)=-[y*log2(x)+(1-y)*log2(1-x)]
#分类问题使用交叉熵:cross entropy,而不使用mse
print("示例二:")
x=torch.randn(1,784)
w=torch.randn(10,784)

logits=x@w.t()
print(logits.shape)

pred=F.softmax(logits,dim=1)#得到概率值
print(pred.shape)
pred_log=torch.log(pred)

#自动得到softmax后的结果,不用进行softmax操作
#cross_entropy=softmax+log+nll_loss
result1=F.cross_entropy(logits,torch.tensor([3]))
print(result1)#tensor(80.8452)
#不包含softmax操作
result2=F.nll_loss(pred_log,torch.tensor([3]))
print(result2)#tensor(80.8452)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值