torch.rand() 和 torch.randn() 有什么区别?

torch.randn(*sizes, out=None)

randn是随机生成服从正态分布的数据,返回值为张量。

参数:

  • sizes (int...) - 整数序列,定义了输出张量的形状
  • out (Tensor, optinal) - 结果张量

torch.rand(*sizes, out=None)

rand是随机生成服从均匀分布的数据,返回值为张量。

参数:

  • sizes (int...) - 整数序列,定义了输出张量的形状
  • out (Tensor, optinal) - 结果张量

两者主要区别在于数据服从的数据分布不同rand是均匀分布,randn是正态分布,n的意思是normal distribution。

例子:

import torch

x = torch.randn(5, 5)
print(x)
x = torch.rand(5, 5)
print(x)

结果:

tensor([[-1.2067e+00, -1.7496e-01, -8.8117e-01,  1.6632e-01,  6.3906e-01],
        [ 3.7858e-02,  2.8564e-02, -3.1674e-02, -2.3180e+00,  1.5624e+00],
        [ 1.8508e+00, -2.8605e-01, -1.0858e+00, -1.6341e-02, -9.7166e-01],
        [-1.2331e+00,  6.5460e-01, -3.2398e-01, -6.5933e-01, -8.2312e-01],
        [-9.6841e-02, -1.1673e+00, -1.8217e-03, -1.4767e+00, -2.0411e-02]])
tensor([[0.5935, 0.0334, 0.3593, 0.0768, 0.8266],
        [0.1705, 0.0965, 0.9235, 0.5899, 0.5111],
        [0.0445, 0.2402, 0.0333, 0.9654, 0.4318],
        [0.1791, 0.1183, 0.1795, 0.4961, 0.5653],
        [0.7089, 0.5844, 0.2965, 0.2817, 0.1237]])

 

你可能感兴趣的:(pytorch,pytorch)