Five commonly used random matrix construction methods in pytorch: rand, randn, randn_like, randint, randperm

1 torch.rand: Construct a uniformly distributed tensor

torch.rand is a function used to generate a uniform random distribution tensor from a uniform distribution on the interval [0,1) Randomly extract a random number to generate a tensor, and its calling method is as follows:

torch.rand(sizes, out=None) ➡️ Tensor

parameter:

  • sizes: used to define the shape of the output tensor

Sample code:

import torch

# 生成一个每个元素服从0-1均匀分布的4行3列随机张量
random_tensor = torch.rand(4, 3)
print('tensor:', random_tensor)
print('type:', random_tensor.type())
print('shape:', random_tensor.shape)

Running the code shows:

tensor: tensor([[0.4349, 0.8567, 0.7321],
        [0.4057, 0.0222, 0.3444],
        [0.9679, 0.0980, 0.8152],
        [0.1998, 0.7888, 0.5478]])
type: torch.FloatTensor
shape: torch.Size([4, 3])

2 torch.randn: Construct standard normal distribution tensor

torch.randn() is a function used to generate normal random distribution tensor. A random number is randomly selected from the standard normal distribution to generate a tensor. Amount, its calling method is as follows:

torch.randn(sizes, out=None) ➡️ Tensor

parameter:

  • sizes: used to define the shape of the output tensor

Sample code:

import torch

# 生成一个每个元素均为标准正态分布的4行3列随机张量
random_tensor = torch.randn(4, 3)
print('tensor:', random_tensor)
print('type:', random_tensor.type())
print('shape:', random_tensor.shape)

Running the code shows:

tensor: tensor([[ 0.7776,  0.6305,  0.1961],
        [ 0.1831, -0.4187,  0.1245],
        [ 0.3092, -1.0463, -0.6656],
        [-1.0098,  1.3861, -0.2600]])
type: torch.FloatTensor
shape: torch.Size([4, 3])

3 torch.randn_like: Construct a normal distribution tensor with the same shape as the input

torch.randn_like() is used to generate a tensor of the same size as the input tensor, filled with random values ​​from a normal distribution with mean 0 and variance 1. The calling method is as follows:

torch.randn_like(input_tensor, dtype=None, layout=None, device=None, requires_grad=False) ➡️ Tensor

parameter:

  • input_tensor (required) – The input tensor whose size will be used to generate the output tensor.

  • dtype (optional) - The required data type for the output tensor. Defaults to None, which means the data type of the input tensor will be used.

  • layout (optional) – The desired memory layout for the output tensor. Defaults to None, which means the memory layout of the input tensor will be used.

  • device (optional) – The device required for the output tensor. Defaults to None, which means the device of the input tensor will be used.

  • requires_grad (optional) - Whether the output tensor should have its gradient calculated during backpropagation. Default is False.

Sample code:

import torch

# 生成一个每个元素均为标准正态分布的4行3列随机张量
tensor_x = torch.randn(4, 3)
tensor_y = torch.randn_like(tensor_x)

print('tensor_x:', tensor_x)
print('type:', tensor_x.type())
print('shape:', tensor_x.shape)

print('tensor_y:', tensor_y)
print('type:', tensor_y.type())
print('shape:', tensor_y.shape)

Running the code shows:

tensor_x: tensor([[ 5.5292e-01,  6.5111e-01, -6.0329e-04],
        [ 1.0402e+00, -7.4630e-01,  7.5701e-01],
        [ 8.8160e-02, -1.2581e+00, -1.8089e-01],
        [-4.2769e-01, -8.5043e-01, -5.8388e-01]])
type: torch.FloatTensor
shape: torch.Size([4, 3])
tensor_y: tensor([[ 0.2308,  0.3297, -0.6633],
        [ 1.7389,  0.6372, -1.1069],
        [-0.2415, -0.8585,  0.3343],
        [-1.2581, -0.5001,  0.0317]])
type: torch.FloatTensor
shape: torch.Size([4, 3])

4 torch.randint: Construct interval distribution tensor

torch.randint() is a function used to generate arbitrary interval distribution tensor. A random number is randomly selected from the standard normal distribution to generate a tensor. , its calling method is as follows:

torch.randint(low=0, high, sizes, out=None) ➡️ Tensor

parameter:

  • low~high: the range of random numbers

  • sizes: used to define the shape of the output tensor

Sample code:

import torch

# 生成一个每个元素均为[1-10]均匀分布的4行3列随机张量
tensor_int = torch.randint(1, 10, (4, 3))
print('tensor_int:', tensor_int)
print('type:', tensor_int.type())
print('shape:', tensor_int.shape)

Running the code shows:

tensor_int: tensor([[1, 7, 1],
        [3, 8, 7],
        [5, 2, 1],
        [5, 3, 6]])
type: torch.LongTensor
shape: torch.Size([4, 3])

5 torch.randperm: Randomly sort tensors according to the generated random sequence number

torch.randint() is a function used to randomly sort tensor numbers according to the generated random sequence. Its calling format is as follows Show:

torch.randperm(n, out=None, dtype=torch.int64) ➡️ LongTensor

parameter:

  • n: an integer, which can be understood as the dimension of a tensor in a certain direction

  • dtype: Returned data type (torch.int64)

Sample code:

import torch

# 生成一个0~3的随机整数排序
idx = torch.randperm(4)

# 生成一个4行3列的张量
tensor_4 = torch.Tensor(4, 3)

# 为了方便对比,首先输出tensor_4的结果
print("原始张量\n", tensor_4)

# 下面输出随机生成的行序号
print("\n生成的随机序号\n", idx)

# 下面的指令实现了在行的方向上,对tensor_4进行随机排序,并输出结果
print("\n随机排序后的张量\n", tensor_4[idx])

Running the code shows:

原始张量
 tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])

生成的随机序号
 tensor([3, 0, 2, 1])

随机排序后的张量
 tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])

おすすめ

転載: blog.csdn.net/lsb2002/article/details/134886317