【问题标题】:What is the purpose of the dim parameter in torch.nn.Softmaxtorch.nn.Softmax 中的 dim 参数的用途是什么
【发布时间】:2020-04-21 18:22:49
【问题描述】:

我不明白在 torch.nn.Softmax 中 dim 参数适用于什么。有一个警告告诉我使用它,我将它设置为 1,但我不明白我在设置什么。它在公式中的使用位置:

Softmax(xi​)=exp(xi)/∑j​exp(xj​)​

这里没有暗淡,那么它适用于什么?

【问题讨论】:

    标签: pytorch softmax


    【解决方案1】:

    torch.nn.Softmax 上的 Pytorch documentation 指出: dim (int) -- 将计算 Softmax 的维度(因此沿 dim 的每个切片的总和为 1)。

    例如,如果您有一个二维矩阵,您可以选择是将 softmax 应用于行还是列:

    import torch 
    import numpy as np
    
    softmax0 = torch.nn.Softmax(dim=0) # Applies along columns
    softmax1 = torch.nn.Softmax(dim=1) # Applies along rows 
    
    v = np.array([[1,2,3],
                  [4,5,6]])
    v =  torch.from_numpy(v).float()
    
    softmax0(v)
    # Returns
    #[[0.0474, 0.0474, 0.0474],
    # [0.9526, 0.9526, 0.9526]])
    
    
    softmax1(v)
    # Returns
    #[[0.0900, 0.2447, 0.6652],
    # [0.0900, 0.2447, 0.6652]]
    

    注意 softmax0 的列如何加到 1,而 softmax1 的行如何加到 1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 2018-12-08
      • 2023-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多