[pytorch]关于KL散度的计算

可以直接调用torch.nn.functional.kl_div进行计算,具体的用法为:

torch.nn.KLDivLoss(size_average=None, reduce=None, reduction='mean', log_target=False)

输入变量的含义:

  • reduction (str, optional) – 制定输出的加权方式. Default: “mean”,也就是平均.“batchmean”是求各个样本的平均,等于sum/batchsize。还可以选’sum’,将batch中样本的平均损失相加。
  • log_target (bool, optional) – 指定target是否为log空间。默认值:False.如果是True,则输入的Target应当位于log空间。

举例说明:

target在softmax空间
import torch.nn as nn
>>> import torch.nn.functional as F
>>> kl_loss = nn.KLDivLoss(reduction="batchmean")
 # input should be a distribution in the log space
>>> input = F.log_softmax(torch.randn(3, 5, requires_grad=True), dim=1)
 # Sample a batch of distributions. Usually this would come from the dataset
>>> target = F.softmax(torch.rand(3, 5), dim=1)
>>> output = kl_loss(input, target)

target在log空间
>>> kl_loss = nn.KLDivLoss(reduction="batchmean", log_target=True)
>>> log_target = F.log_softmax(torch.rand(3, 5), dim=1)
>>> output = kl_loss(input, log_target)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值