捷徑

torch.var

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

計算由 dim 指定維度上的變異數。dim 可以是單個維度、維度列表或 None,表示在所有維度上進行縮減。

變異數(σ2\sigma^2)的計算公式如下:

σ2=1max(0, NδN)i=0N1(xixˉ)2\sigma^2 = \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 (int or tuple of ints, optional) – 要縮減的維度。 如果為 None,則會縮減所有維度。

關鍵字參數
  • correction (int) –

    樣本大小與樣本自由度之間的差異。預設為 貝索校正 (Bessel’s correction)correction=1

    版本 2.0 更新:先前此引數稱為 unbiased,且為布林值,其中 True 對應於 correction=1,而 False 則為 correction=0

  • keepdim (bool) – 指輸出張量是否保留 dim 維度。

  • 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]])
>>> torch.var(a, dim=1, keepdim=True)
tensor([[1.0631],
        [0.5590],
        [1.4893],
        [0.8258]])

文件

存取 PyTorch 的全面開發人員文件

檢視文件

教學

取得針對初學者和進階開發人員的深入教學課程

檢視教學

資源

尋找開發資源並取得您的問題解答

檢視資源