torch.mean#
- torch.mean(input, *, dtype=None) Tensor#
注意
如果 input 張量為空,
torch.mean()返回nan。此行為與 NumPy 一致,並且遵循空集上的平均值未定義的定義。返回
input張量中所有元素的平均值。輸入必須是浮點型或複數型。- 引數
input (Tensor) – 輸入張量,可以是浮點型或複數型
- 關鍵字引數
dtype (
torch.dtype, 可選) – 返回張量的期望資料型別。如果指定,則在執行操作之前將輸入張量轉換為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, 可選) – 返回張量的期望資料型別。如果指定,則在執行操作之前將輸入張量轉換為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]])