Categoría: Catálogo General de "Funciones de Pytorch de Manera Simple"
Artículos Relacionados:
Matemáticas en Aprendizaje Automático——Función de Activación: Funciones Softmax
Funciones de Pytorch de Manera Simple—torch.softmax/torch.nn.funcional.softmax Funciones de Pytorch de Manera Simple Manera—
antorcha
Aplicar la función Softmax a nntensores de entrada n- dimensionales, reescálelos de modo quenLos elementos del tensor de salida n- dimensional están ubicados en[0, 1][0,1][ 0 ,1 ] , y la suma es 1. Cuando el tensor de entrada es un tensor disperso, se consideran valores no especificados-inf
.
gramática
torch.nn.Softmax(dim=None)
parámetro
dim
:[int
] La función Softmax sedim
calculará a lo largo del eje, es decir,dim
la suma de cada corte a lo largo del
valor de retorno
Un tensor del mismo tamaño y forma que el tensor de entrada, con valores de elementos en el rango [0, 1] [0, 1]en el rango [ 0 , 1 ] .
ejemplo
>>> m = torch.nn.Softmax(dim=1)
>>> input = torch.randn(2, 3)
>>> output = m(input)
tensor([[0.4773, 0.0833, 0.4395],
[0.0281, 0.6010, 0.3709]])
implementación de funciones
class Softmax(Module):
r"""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.
Softmax is defined as:
.. math::
\text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}
When the input Tensor is a sparse tensor then the unspecified
values are treated as ``-inf``.
Shape:
- Input: :math:`(*)` where `*` means, any number of additional
dimensions
- Output: :math:`(*)`, same shape as the input
Returns:
a Tensor of the same dimension and shape as the input with
values in the range [0, 1]
Args:
dim (int): A dimension along which Softmax will be computed (so every slice
along dim will sum to 1).
.. note::
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).
Examples::
>>> m = nn.Softmax(dim=1)
>>> input = torch.randn(2, 3)
>>> output = m(input)
"""
__constants__ = ['dim']
dim: Optional[int]
def __init__(self, dim: Optional[int] = None) -> None:
super().__init__()
self.dim = dim
def __setstate__(self, state):
super().__setstate__(state)
if not hasattr(self, 'dim'):
self.dim = None
def forward(self, input: Tensor) -> Tensor:
return F.softmax(input, self.dim, _stacklevel=5)
def extra_repr(self) -> str:
return 'dim={dim}'.format(dim=self.dim)