torch.renorm
抱怨
看了很多的别人的分析,然后全都在做2范数的分析和实验。真是怪哦,你们都不看文档的吗?都没发现自己2范数时验证的想法在文档里明显对应不上吗?,我真是麻了,我还去分析你们的博客和代码,虽然也不难,但是浪费了时间还气…
正题
关于torch.renorm(x,p,dim, maxnorm)的理解:
return x_subvector * maxnorm / p_norm(x_subvector)
这样能理解吧,没空,简略一点先。
Python 3.9.16 (main, Mar 8 2023, 14:00:05) [GCC 11.2.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. >>> import torch >>> a = torch.ones(3,3) >>> a.fill_(2) tensor([[2., 2., 2.], [2., 2., 2.], [2., 2., 2.]]) >>> a tensor([[2., 2., 2.], [2., 2., 2.], [2., 2., 2.]]) >>> a = torch.ones(3,3) >>> a[1].fill_(2) tensor([2., 2., 2.]) >>> a[2].fill_(3) tensor([3., 3., 3.]) >>> a tensor([[1., 1., 1.], [2., 2., 2.], [3., 3., 3.]]) >>> torch.renorm(x,1,0,5) Traceback (most recent call last): File "", line 1, in NameError: name 'x' is not defined >>> torch.renorm(a,1,0,5) tensor([[1.0000, 1.0000, 1.0000], [1.6667, 1.6667, 1.6667], [1.6667, 1.6667, 1.6667]]) >>> a tensor([[1., 1., 1.], [2., 2., 2.], [3., 3., 3.]]) >>> 2/6/5 0.06666666666666667 >>> 2/6*5 1.6666666666666665 >>> 10/6 1.6666666666666667 >>> ### 二范数的验证 >>> torch.renorm(a,2,0,5) tensor([[1.0000, 1.0000, 1.0000], [2.0000, 2.0000, 2.0000], [2.8868, 2.8868, 2.8868]]) >>> 3*5/((3*3*3)**0.5) 2.8867513459481287
ps
当然,我也没空去做3范数到n范数的验证,说不定我也是错的。不想管啦。