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

Some torch commonly used functions

1 torch.mean()

Generating a specified size, the normal sampling point, the data type is a 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) the output of each element of the input mean, no parameter is specified is the arithmetic mean of all the elements specified parameters can be calculated for each row or each column arithmetic mean

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, calculating the average, the output and input of each row have the same dimensions: two dimensions (4,1)

Dim not set, the default is the average calculated for each row, an embedded torch.squeeze (), the compression value of dimension 1 (4)

3 torch.squeeze()

torch.squeeze (input, dim = None, out = None)
Output: input 1 to remove all dimensions (2,1) = (2) (present in the form of a row vector)

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])

Guess you like

Origin blog.csdn.net/sinat_40624829/article/details/91127373