深入浅出Pytorch函数——torch.nn.Softmax

这篇具有很好参考价值的文章主要介绍了深入浅出Pytorch函数——torch.nn.Softmax。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

分类目录:《深入浅出Pytorch函数》总目录
相关文章:
· 机器学习中的数学——激活函数:Softmax函数
· 深入浅出Pytorch函数——torch.softmax/torch.nn.functional.softmax
· 深入浅出Pytorch函数——torch.nn.Softmax


将Softmax函数应用于 n n n维输入张量,重新缩放它们,使得 n n n维输出张量的元素位于 [ 0 , 1 ] [0,1] [0,1]的范围内,且总和为1。当输入张量是稀疏张量时,未指定的值被视为-inf

语法
torch.nn.Softmax(dim=None)
参数
  • dim:[int] Softmax函数将沿着dim轴计算,即沿dim的每个切片的和为1
返回值

与输入张量具有相同尺寸和形状的张量,且其元素值在 [ 0 , 1 ] [0,1] [01]范围内。文章来源地址https://www.toymoban.com/news/detail-611966.html

实例
>>> 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]])
函数实现
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)

到了这里,关于深入浅出Pytorch函数——torch.nn.Softmax的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 深入浅出Pytorch函数——torch.nn.init.normal_

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.nn.init.calculate_gain · 深入浅出Pytorch函数——torch.nn.init.uniform_ · 深入浅出Pytorch函数——torch.nn.init.normal_ · 深入浅出Pytorch函数——torch.nn.init.constant_ · 深入浅出Pytorch函数——torch.nn.init.ones_ ·

    2024年02月12日
    浏览(34)
  • 深入浅出Pytorch函数——torch.nn.init.zeros_

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.nn.init.calculate_gain · 深入浅出Pytorch函数——torch.nn.init.uniform_ · 深入浅出Pytorch函数——torch.nn.init.normal_ · 深入浅出Pytorch函数——torch.nn.init.constant_ · 深入浅出Pytorch函数——torch.nn.init.ones_ ·

    2024年02月12日
    浏览(28)
  • 深入浅出Pytorch函数——torch.nn.init.dirac_

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.nn.init.calculate_gain · 深入浅出Pytorch函数——torch.nn.init.uniform_ · 深入浅出Pytorch函数——torch.nn.init.normal_ · 深入浅出Pytorch函数——torch.nn.init.constant_ · 深入浅出Pytorch函数——torch.nn.init.ones_ ·

    2024年02月12日
    浏览(26)
  • 深入浅出Pytorch函数——torch.nn.init.uniform_

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.nn.init.calculate_gain · 深入浅出Pytorch函数——torch.nn.init.uniform_ · 深入浅出Pytorch函数——torch.nn.init.normal_ · 深入浅出Pytorch函数——torch.nn.init.constant_ · 深入浅出Pytorch函数——torch.nn.init.ones_ ·

    2024年02月12日
    浏览(28)
  • 深入浅出Pytorch函数——torch.nn.init.eye_

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.nn.init.calculate_gain · 深入浅出Pytorch函数——torch.nn.init.uniform_ · 深入浅出Pytorch函数——torch.nn.init.normal_ · 深入浅出Pytorch函数——torch.nn.init.constant_ · 深入浅出Pytorch函数——torch.nn.init.ones_ ·

    2024年02月11日
    浏览(32)
  • 深入浅出Pytorch函数——torch.nn.init.calculate_gain

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.nn.init.calculate_gain · 深入浅出Pytorch函数——torch.nn.init.uniform_ · 深入浅出Pytorch函数——torch.nn.init.normal_ · 深入浅出Pytorch函数——torch.nn.init.constant_ · 深入浅出Pytorch函数——torch.nn.init.ones_ ·

    2024年02月11日
    浏览(26)
  • 深入浅出Pytorch函数——torch.nn.init.xavier_uniform_

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.nn.init.calculate_gain · 深入浅出Pytorch函数——torch.nn.init.uniform_ · 深入浅出Pytorch函数——torch.nn.init.normal_ · 深入浅出Pytorch函数——torch.nn.init.constant_ · 深入浅出Pytorch函数——torch.nn.init.ones_ ·

    2024年02月11日
    浏览(25)
  • 深入浅出Pytorch函数——torch.nn.init.kaiming_uniform_

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.nn.init.calculate_gain · 深入浅出Pytorch函数——torch.nn.init.uniform_ · 深入浅出Pytorch函数——torch.nn.init.normal_ · 深入浅出Pytorch函数——torch.nn.init.constant_ · 深入浅出Pytorch函数——torch.nn.init.ones_ ·

    2024年02月12日
    浏览(36)
  • 深入浅出Pytorch函数——torch.nn.init.xavier_normal_

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.nn.init.calculate_gain · 深入浅出Pytorch函数——torch.nn.init.uniform_ · 深入浅出Pytorch函数——torch.nn.init.normal_ · 深入浅出Pytorch函数——torch.nn.init.constant_ · 深入浅出Pytorch函数——torch.nn.init.ones_ ·

    2024年02月12日
    浏览(29)
  • 深入浅出Pytorch函数——torch.nn.init.trunc_normal_

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.nn.init.calculate_gain · 深入浅出Pytorch函数——torch.nn.init.uniform_ · 深入浅出Pytorch函数——torch.nn.init.normal_ · 深入浅出Pytorch函数——torch.nn.init.constant_ · 深入浅出Pytorch函数——torch.nn.init.ones_ ·

    2024年02月11日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包