評價此頁

Softmax#

class torch.nn.modules.activation.Softmax(dim=None)[source]#

將 Softmax 函式應用於 n 維輸入張量。

重新縮放它們,使 n 維輸出張量中的元素位於 [0,1] 範圍內並求和為 1。

Softmax 定義為

Softmax(xi)=exp(xi)jexp(xj)\text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}

當輸入張量是稀疏張量時,未指定的預設值被視為 -inf

形狀
  • 輸入: ()(*),其中 * 表示任意數量的附加維度

  • 輸出: ()(*),與輸入形狀相同

返回

一個與輸入具有相同維度和形狀的張量,其值在 [0, 1] 範圍內

引數

dim (int) – 計算 Softmax 的一個維度(因此沿 dim 的每個切片都將求和為 1)。

返回型別

注意

此模組不能直接與 NLLLoss 配合使用,因為 NLLLoss 期望在 Softmax 和自身之間計算 Log。請改用 LogSoftmax(它更快,並且具有更好的數值特性)。

示例

>>> m = nn.Softmax(dim=1)
>>> input = torch.randn(2, 3)
>>> output = m(input)
extra_repr()[source]#

返回模組的額外表示。

返回型別

str

forward(input)[source]#

執行前向傳播。

返回型別

張量