pytorch中五种常用随机矩阵构造方法:rand、randn、randn_like、randint、randperm

1 torch.rand:构造均匀分布张量

torch.rand是用于生成均匀随机分布张量的函数,从区间[0,1)的均匀分布中随机抽取一个随机数生成一个张量,其调用方法如下所示:

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

参数:

  • sizes:用于定义输出张量的形状

示例代码:

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)

运行代码显示:

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:构造标准正态分布张量

torch.randn()是用于生成正态随机分布张量的函数,从标准正态分布中随机抽取一个随机数生成一个张量,其调用方法如下所示:

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

参数:

  • sizes:用于定义输出张量的形状

示例代码:

import torch

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

运行代码显示:

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:构造与输入形状相同正态分布张量

torch.randn_like()用于生成一个与输入张量大小相同的张量,其中填充了均值为 0 方差为 1 的正态分布的随机值,其调用方法如下所示:

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

参数:

  • input_tensor(必需)- 其大小将用于生成输出张量的输入张量。

  • dtype(可选)- 输出张量所需的数据类型。默认为None,这意味着将使用输入张量的数据类型。

  • layout(可选)- 输出张量所需的内存布局。默认为None,这意味着将使用输入张量的内存布局。

  • device(可选)- 输出张量所需的设备。默认为None,这意味着将使用输入张量的设备。

  • requires_grad(可选)- 输出张量是否应该在反向传播期间计算其梯度。默认为False。

示例代码:

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)

运行代码显示:

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:构造区间分布张量

torch.randint()是用于生成任意区间分布张量的函数,从标准正态分布中随机抽取一个随机数生成一个张量,其调用方法如下所示:

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

参数:

  • low~high:随机数的区间范围

  • sizes:用于定义输出张量的形状

示例代码:

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)

运行代码显示:

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:根据生成的随机序号对张量进行随机排序

torch.randint()是用于对张量序号进行随机排序的函数,根据生成的随机序列进行随机排序,其调用格式如下所示:

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

参数:

  • n:一个整数,可以理解为张量某个方向的维度

  • dtype:返回的数据类型(torch.int64

示例代码:

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

运行代码显示:

原始张量
 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