Pytorch的rand、randn和normal的用法及区别

文章目录

  • 1.rand
  • 2.randn
  • 3.normal


1.rand

生成均匀分布随机数【范围在 0 到 1 之间】

X = torch.rand(size=(3, 5))    # 三行五列均匀分布矩阵
print(X)
# tensor([[0.7748, 0.2472, 0.9830, 0.1304, 0.0813],
#         [0.8575, 0.7833, 0.3998, 0.1507, 0.5604],
#         [0.4845, 0.0819, 0.5311, 0.5740, 0.1260]])

2.randn

生成标准正态分布随机数【均值为 0 标准差为 1】

X = torch.randn(size=(3, 5))	# 三行五列标准正态分布矩阵
print(X)
# tensor([[0.7748, 0.2472, 0.9830, 0.1304, 0.0813],
#         [0.8575, 0.7833, 0.3998, 0.1507, 0.5604],
#         [0.4845, 0.0819, 0.5311, 0.5740, 0.1260]])

3.normal

生成任意正态分布随机数【均值和标准差由创建者指定】

X = torch.normal(mean=5, std=20, size=(3, 5))   # 均值5标准差20三行五列正态分布矩阵
print(X)
# tensor([[ 14.6288,   1.8920, -30.1699,   0.5885,  -8.1273],
#         [  8.5448,  25.8816, -14.2568, -21.4472,  40.0083],
#         [-24.3172,   4.9765, -17.0209,  -4.4697,  17.1688]])

你可能感兴趣的:(NLP,CV,Python,pytorch,python,深度学习)