注意:参数列表里的 * 星号,标志着位置参数的就此终结,之后的那些参数,都只能以关键字形式来指定
可能你到这里就信誓旦旦去调用这个函数了,但是实际结果是:
import torch
torch.normal(means=torch.arange(1,11),std=torch.arange(1,0,-0.1))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_21072/3185136372.py in
1 import torch
----> 2 torch.normal(means=torch.arange(1,11),std=torch.arange(1,0,-0.1))
TypeError: normal() received an invalid combination of arguments - got (means=Tensor, std=Tensor, ), but expected one of:
* (Tensor mean, Tensor std, *, torch.Generator generator, Tensor out)
* (Tensor mean, float std, *, torch.Generator generator, Tensor out)
* (float mean, Tensor std, *, torch.Generator generator, Tensor out)
* (float mean, float std, tuple of ints size, *, torch.Generator generator, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)
然而 。。。
torch.normal(mean=torch.arange(1,11),std=torch.arange(1,0,-0.1))
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_21072/2194249099.py in
----> 1 torch.normal(mean=torch.arange(1,11),std=torch.arange(1,0,-0.1))
RuntimeError: "normal_kernel_cpu" not implemented for 'Long'
是不是很无奈,其实这是pytorch版本的问题,我们把arrange中的数字都改成浮点数就行了
torch.normal(mean=torch.arange(1.,11.),std=torch.arange(1.,0.,-0.1))
# ([ 0.9897, // 从均值为1,标准差为1的正太分布中随机选取的
# 2.0167, // 从均值为2,标准差为0.9的正太分布中随机选取的
# 3.4270, //以此类推
# 3.9813,
# 5.7261,
# 6.1477,
# 7.3065,
# 8.3148,
# 8.6105,
# 10.0650])
tensor([ 0.9897, 2.0167, 3.4270, 3.9813, 5.7261, 6.1477, 7.3065, 8.3148,
8.6105, 10.0650])
其他部分和第一个函数一样
torch.normal(2.,torch.arange(1.0,0,-0.1))
tensor([1.3067, 3.1319, 3.0808, 1.4510, 2.7221, 2.3876, 2.6084, 1.8700, 1.8914,
2.0556])
(Tensor mean, float std, *, torch.Generator generator, Tensor out):共享同一个标准差
(float mean, float std, tuple of ints size, *, torch.Generator generator, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad):共享同一个均值和同一个标准差,并且需要额外指定输出张量的形状