評價此頁

Softmax#

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

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

將其重新縮放,使得 n 維輸出 Tensor 的元素位於 [0,1] 範圍內,並且總和為 1。

Softmax 定義為

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

當輸入 Tensor 是稀疏張量時,未指定的值將被視為 -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]#

執行前向傳播。

返回型別

張量