torch.mean¶
- torch.mean(input, *, dtype=None) Tensor ¶
注意
如果 input 张量为空,
torch.mean()
返回nan
。此行为与 NumPy 一致,并遵循空集的平均值未定义的定义。返回
input
张量中所有元素的平均值。输入必须是浮点型或复数型。- 参数
input (Tensor) – 输入张量,浮点型或复数型 dtype
- 关键字参数
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, 可选) – 输出张量。
另请参见
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]])