torch.mean¶
- torch.mean(input, *, dtype=None) Tensor ¶
注意
如果 input 張量是空的,
torch.mean()
會回傳nan
。此行為與 NumPy 一致,並遵循空集合的平均值未定義的定義。回傳
input
張量中所有元素的平均值。輸入必須是浮點數或複數。- 參數
input (Tensor) – 輸入張量,dtype 為浮點數或複數
- 關鍵字參數
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
回傳給定維度
dim
中input
張量的每一列的平均值。 如果dim
是維度的列表,則在所有維度上進行縮減。如果
keepdim
為True
,則輸出張量的大小與input
相同,除了維度dim
(或多個維度) 大小為 1 之外。 否則,dim
將被壓縮(請參閱torch.squeeze()
),導致輸出張量少 1 個(或len(dim)
)維度。- 參數
- 關鍵字參數
dtype (
torch.dtype
, optional) – 回傳張量所需的資料類型。如果指定,輸入張量會在執行操作前轉換為dtype
。這有助於防止資料類型溢位。預設值:None。out (Tensor, optional) – 輸出張量。
另請參閱
torch.nanmean()
計算 non-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]])