torch.renorm的理解--已验证1范数和2范数的输出结果

文章讨论了torch.renorm函数在PyTorch中的使用,特别是对1范数和2范数的调整,指出了一些常见误解,并通过示例解释了函数如何规范化张量的子向量。作者提醒读者注意文档中关于范数计算的细节,并提供了验证不同范数的代码示例。
摘要由CSDN通过智能技术生成

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范数的验证,说不定我也是错的。不想管啦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值