評價此頁

torch.std#

torch.std(input, dim=None, *, correction=1, keepdim=False, out=None) Tensor#

計算指定維度 dim 上資料的標準差。dim 可以是單個維度、維度列表,或者 None (表示計算所有維度)。

標準差(σ\sigma)計算公式如下:

σ=1max(0, NδN)i=0N1(xixˉ)2\sigma = \sqrt{\frac{1}{\max(0,~N - \delta N)}\sum_{i=0}^{N-1}(x_i-\bar{x})^2}

其中 xx 是樣本集,xˉ\bar{x} 是樣本均值,NN 是樣本數量,δN\delta Ncorrection

如果 keepdimTrue,則輸出張量的大小與 input 相同,只有在 dim 維度上大小為 1。否則,dim 將被擠壓(參見 torch.squeeze()),導致輸出張量維度減少 1(或 len(dim))個。

引數
  • input (Tensor) – 輸入張量。

  • dim (inttuple of ints, optional) – 要規約的維度或維度。如果為 None,則規約所有維度。

關鍵字引數
  • correction (int) –

    樣本大小與樣本自由度之間的差值。預設為 貝塞爾校正,即 correction=1

    已在 2.0 版本更改: 以前此引數稱為 unbiased,是一個布林值,True 對應 correction=1,而 False 對應 correction=0

  • keepdim (bool, optional) – 輸出張量是否保留 dim。預設為 False

  • out (Tensor, optional) – 輸出張量。

示例

>>> a = torch.tensor(
...     [[ 0.2035,  1.2959,  1.8101, -0.4644],
...      [ 1.5027, -0.3270,  0.5905,  0.6538],
...      [-1.5745,  1.3330, -0.5596, -0.6548],
...      [ 0.1264, -0.5080,  1.6420,  0.1992]]
... )  # fmt: skip
>>> torch.std(a, dim=1, keepdim=True)
tensor([[1.0311],
        [0.7477],
        [1.2204],
        [0.9087]])