torch.mean¶
- torch.mean(input, *, dtype=None) Tensor ¶
注意
如果输入张量是空的,则
torch.mean()
返回nan
。此行为与 NumPy 一致,遵循空集求平均未定义的定义。返回
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)
)。- 参数:
input (Tensor) – 输入张量。
dim (int 或 int 的元组) – 要减少的维度或维度。
keepdim(布尔值)- 输出张量是否保留
dim
。
- 关键字参数:
dtype(
torch.dtype
,可选)- 返回张量的期望数据类型。如果指定,则在执行操作之前将输入张量转换为dtype
。这有助于防止数据类型溢出。默认:None。输出(张量,可选)- 输出张量。
参见
计算非 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]])