快捷方式

torch.mean

torch.mean(input, *, dtype=None) Tensor

注意

如果 input 張量為空,torch.mean() 返回 nan。此行為與 NumPy 一致,並遵循空集的均值未定義的定義。

返回 input 張量中所有元素的均值。輸入必須是浮點型別或複數型別。

引數

input (Tensor) – 輸入張量,可以是浮點型別或複數型別

關鍵字引數

dtype (torch.dtype, optional) – 返回張量所需的 資料型別。如果指定,操作執行前會將輸入張量轉換為 dtype。這對於防止資料型別溢位很有用。預設值:None。

示例

>>> a = torch.randn(1, 3)
>>> a
tensor([[ 0.2294, -0.5481,  1.3288]])
>>> torch.mean(a)
tensor(0.3367)
torch.mean(input, dim, keepdim=False, *, dtype=None, out=None) Tensor

返回給定維度 diminput 張量每行的均值。如果 dim 是維度列表,則對所有維度進行歸約。

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

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

  • dim (inttuple of ints) – 待歸約的維度或維度列表。

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

關鍵字引數
  • dtype (torch.dtype, optional) – 返回張量所需的 資料型別。如果指定,操作執行前會將輸入張量轉換為 dtype。這對於防止資料型別溢位很有用。預設值:None。

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

另請參閱

torch.nanmean() 計算非 NaN 元素的均值。

示例

>>> a = torch.randn(4, 4)
>>> a
tensor([[-0.3841,  0.6320,  0.4254, -0.7384],
        [-0.9644,  1.0131, -0.6549, -1.4279],
        [-0.2951, -1.3350, -0.7694,  0.5600],
        [ 1.0842, -0.9580,  0.3623,  0.2343]])
>>> torch.mean(a, 1)
tensor([-0.0163, -0.5085, -0.4599,  0.1807])
>>> torch.mean(a, 1, True)
tensor([[-0.0163],
        [-0.5085],
        [-0.4599],
        [ 0.1807]])

文件

訪問 PyTorch 全面的開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源