为什么 randn 并不总是均值为 0 且方差为 1?
Why randn doesn't always have a mean of 0 and variance of 1?
对于 PyTorch.randn() 方法,文档说:
Returns a tensor filled with random numbers from a normal distribution
with mean 0
and variance 1
(also called the standard normal
distribution).
所以这是张量示例:
x = torch.randn(4,3)
tensor([[-0.6569, -0.7337, -0.0028],
[-0.3938, 0.3223, 0.0497],
[ 0.0129, -2.7546, -2.2488],
[ 1.6754, -0.1497, 1.8202]])
当我打印平均值时:
x.mean()
tensor(-0.2550)
当我打印标准偏差时:
x.std()
tensor(1.3225)
那么为什么平均值不是 0 而标准差不是 1?
奖励问题:如何生成均值为 0 的随机张量?
分布的有限样本具有完全相同的均值和完全相同的标准差,这将是一个很大的巧合。预计您生成的数字越多,样本的均值和偏差越接近分布的 "true" 均值和偏差。
我只能回答一半:我认为您误解了文档。它不应解析为 "Returns a tensor {filled with random numbers from a normal distribution} with mean 0 and variance 1",而应解析为 "Returns a tensor filled with {random numbers from a normal distribution with mean 0 and variance 1}"。 IE。返回的张量没有均值 0 或方差 1。只有从中抽取随机数的分布具有均值 0 和方差 1。
如果您真的希望给定样本的均值为零且方差为 1(而不是从具有零均值和单位方差的分布中采样),那么您可以将其转换为这样,例如:
x = torch.randn(4,3)
x -= x.mean()
x /= x.std()
这种东西很有用
对于 PyTorch.randn() 方法,文档说:
Returns a tensor filled with random numbers from a normal distribution with mean
0
and variance1
(also called the standard normal distribution).
所以这是张量示例:
x = torch.randn(4,3)
tensor([[-0.6569, -0.7337, -0.0028],
[-0.3938, 0.3223, 0.0497],
[ 0.0129, -2.7546, -2.2488],
[ 1.6754, -0.1497, 1.8202]])
当我打印平均值时:
x.mean()
tensor(-0.2550)
当我打印标准偏差时:
x.std()
tensor(1.3225)
那么为什么平均值不是 0 而标准差不是 1?
奖励问题:如何生成均值为 0 的随机张量?
分布的有限样本具有完全相同的均值和完全相同的标准差,这将是一个很大的巧合。预计您生成的数字越多,样本的均值和偏差越接近分布的 "true" 均值和偏差。
我只能回答一半:我认为您误解了文档。它不应解析为 "Returns a tensor {filled with random numbers from a normal distribution} with mean 0 and variance 1",而应解析为 "Returns a tensor filled with {random numbers from a normal distribution with mean 0 and variance 1}"。 IE。返回的张量没有均值 0 或方差 1。只有从中抽取随机数的分布具有均值 0 和方差 1。
如果您真的希望给定样本的均值为零且方差为 1(而不是从具有零均值和单位方差的分布中采样),那么您可以将其转换为这样,例如:
x = torch.randn(4,3)
x -= x.mean()
x /= x.std()
这种东西很有用