【总结】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

你可能感兴趣的:(Note,python,深度学习,torch,pytorch,torchvision)