Pytorch(3)- torch.mean(),.randn(),.squeeze()

torch常用的一些函数

1 torch.mean()

产生大小为指定的,正态分布的采样点,数据类型是tensor

torch.randn(4)

tensor([-2.1436, 0.9966, 2.3426, -0.6366])

torch.randn(2, 3)

tensor([[ 1.5954, 2.8929, -1.0923],
[ 1.1719, -0.4709, -0.1996]])

2 torch.mean()

torch.mean(input) 输出input 各个元素的的均值,不指定任何参数就是所有元素的算术平均值,指定参数可以计算每一行或者 每一列的算术平均数

a = torch.randn(1, 3)

tensor([[ 0.2294, -0.5481, 1.3288]])

torch.mean(a)

tensor(0.3367)

a = torch.randn(4, 4)

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, True)

tensor([[-0.0163],
[-0.5085],
[-0.4599],
[ 0.1807]])

torch.mean(a, 1)

tensor([-0.0163, -0.5085, -0.4599, 0.1807])

dim=true,计算每一行的平均数,输出与输入有相同的维度:两维度(4,1)

不设置dim,默认计算每一行的平均数,内嵌了一个torch.squeeze(),将数值为1的维度压缩(4,)

3 torch.squeeze()

torch.squeeze(input, dim=None, out=None)
输出:将输入所有维度为1的去除(2,1)=(2,)(以行向量的形式存在)

x = torch.zeros(2, 1, 2, 1, 2)
x.size()

torch.Size([2, 1, 2, 1, 2])

y = torch.squeeze(x)
y.size()

torch.Size([2, 2, 2])

猜你喜欢

转载自blog.csdn.net/sinat_40624829/article/details/91127373
今日推荐