【总结】PyTorch多分类log_softmax、softmax的中文手册

【总结】PyTorch多分类log_softmax、softmax的区别与联系

log_softmax、softmax在F和nn中存在,在此主要介绍nn

1.softmax

torch.nn.functional.softmax (Python function, in torch.nn.functional)
torch.nn.Softmax (Python class, in torch.nn)

在这里,我们主要介绍 torch.nn.Softmax

torch.nn.Softmax(dim=None)

在这里插入图片描述

Applies the Softmax function to an n-dimensional input Tensor rescaling them so that the elements of the n-dimensional output Tensor lie in the range [0,1] and sum to 1.

返回一个元素范围在0到1的、和为1的tensor

dim (python:int) – A dimension along which Softmax will be computed (so every slice along dim will sum to 1).

选一个softmax计算的维度(这个维度上的和为1)

2.log_softmax

torch.nn.LogSoftmax(dim=None)

在这里插入图片描述

Applies the function to an n-dimensional input Tensor. The LogSoftmax formulation can be simplified as:

简单来说,log_softmax只是对softmax进行了一个log,

但是这里有一个重要笔记:

This module doesn’t work directly with NLLLoss, which expects the Log to be computed between the Softmax and itself. Use LogSoftmax instead (it’s faster and has better numerical properties).

大概就是说,log_softmax不能直接与NLLLoss工作,后者会在softmax和自己之间进行log计算。

建议使用log_softmax替代(他更快并且在数字特征上表现得更好)

3.差距

似乎logsoftmax表现得更好。有兴趣可以参考下面这个讨论:
https://discuss.pytorch.org/t/logsoftmax-vs-softmax

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值