参考来源:
Pytorch 文档:TORCH.MEAN
Pytorch 文档:TORCH.TENSOR.MEAN
Tensor.mean()
Tensor.mean(dim=None, keepdim=False, *, dtype=None) → Tensor
见 torch.mean()。
torch.mean()
torch.mean(input, *, dtype=None) → Tensor
返回张量中所有元素的平均值。
参数:
input (Tensor):输入张量。
关键字参数:
dtype (torch.dtype, optional):返回张量的所需数据类型。 如果指定,则在执行操作之前,输入张量将会被转换。 这对于防止数据类型溢出很有用。 默认值:None。
a = torch.randn(1, 3)print(a)print(torch.mean(a))"""output:tensor([[ 0.2294, -0.5481, 1.3288]])tensor(0.3367)"""
torch.mean(input, dim, keepdim=False, *, dtype=None, out=None) → Tensor
返回给定维度 dim 中输入张量的每一行的平均值。 如果 dim 是维度列表,则缩减所有维度。
如果 keepdim 为 True,则输出张量的大小与输入的大小相同,除了维度为 1 的维度以外。否则,dim 会被压缩(参见 torch.squeeze()),导致输出张量的维度少 1(或 len(dim))。
a = torch.randn(4, 4)print(a)print(torch.mean(a, 1))print(torch.mean(a, 1, True))"""output: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]])tensor([-0.0163, -0.5085, -0.4599, 0.1807])tensor([[-0.0163],[-0.5085],[-0.4599],[ 0.1807]])"""
