torch.norm()函数的用法

举报
悲恋花丶无心之人 发表于 2021/02/03 02:33:08 2021/02/03
【摘要】 目录 一、函数定义 二、代码示例 三、整体代码 一、函数定义 公式:                                                                       意思就是inputs的一共N维的话对这N个数据求p范数,当然这个还是太抽象了,接下来还是看具体的代码~ p指的是求p范数的p值,函数默认p=2,那么就...

目录

一、函数定义

二、代码示例

三、整体代码


一、函数定义

公式:

                                                                     ||x||_{p} = \sqrt[p]{x_{1}^{p} + x_{2}^{p} + \ldots + x_{N}^{p}}

意思就是inputs的一共N维的话对这N个数据p范数,当然这个还是太抽象了,接下来还是看具体的代码~

p指的是求p范数的p值,函数默认p=2,那么就是求2范数


  
  1. def norm(self, input, p=2): # real signature unknown; restored from __doc__
  2. """
  3. .. function:: norm(input, p=2) -> Tensor
  4. Returns the p-norm of the :attr:`input` tensor.
  5. .. math::
  6. ||x||_{p} = \sqrt[p]{x_{1}^{p} + x_{2}^{p} + \ldots + x_{N}^{p}}
  7. Args:
  8. input (Tensor): the input tensor
  9. p (float, optional): the exponent value in the norm formulation
  10. Example::
  11. >>> a = torch.randn(1, 3)
  12. >>> a
  13. tensor([[-0.5192, -1.0782, -1.0448]])
  14. >>> torch.norm(a, 3)
  15. tensor(1.3633)
  16. .. function:: norm(input, p, dim, keepdim=False, out=None) -> Tensor
  17. Returns the p-norm of each row of the :attr:`input` tensor in the given
  18. dimension :attr:`dim`.
  19. If :attr:`keepdim` is ``True``, the output tensor is of the same size as
  20. :attr:`input` except in the dimension :attr:`dim` where it is of size 1.
  21. Otherwise, :attr:`dim` is squeezed (see :func:`torch.squeeze`), resulting
  22. in the output tensor having 1 fewer dimension than :attr:`input`.
  23. Args:
  24. input (Tensor): the input tensor
  25. p (float): the exponent value in the norm formulation
  26. dim (int): the dimension to reduce
  27. keepdim (bool): whether the output tensor has :attr:`dim` retained or not
  28. out (Tensor, optional): the output tensor
  29. Example::
  30. >>> a = torch.randn(4, 2)
  31. >>> a
  32. tensor([[ 2.1983, 0.4141],
  33. [ 0.8734, 1.9710],
  34. [-0.7778, 0.7938],
  35. [-0.1342, 0.7347]])
  36. >>> torch.norm(a, 2, 1)
  37. tensor([ 2.2369, 2.1558, 1.1113, 0.7469])
  38. >>> torch.norm(a, 0, 1, True)
  39. tensor([[ 2.],
  40. [ 2.],
  41. [ 2.],
  42. [ 2.]])
  43. """
  44. pass

二、代码示例

输入代码


  
  1. import torch
  2. rectangle_height = 3
  3. rectangle_width = 4
  4. inputs = torch.randn(rectangle_height, rectangle_width)
  5. for i in range(rectangle_height):
  6. for j in range(rectangle_width):
  7. inputs[i][j] = (i + 1) * (j + 1)
  8. print(inputs)

得到一个3×4矩阵,如下


  
  1. tensor([[ 1., 2., 3., 4.],
  2. [ 2., 4., 6., 8.],
  3. [ 3., 6., 9., 12.]])

接着我们分别对其分别求2范数


  
  1. inputs1 = torch.norm(inputs, p=2, dim=1, keepdim=True)
  2. print(inputs1)
  3. inputs2 = torch.norm(inputs, p=2, dim=0, keepdim=True)
  4. print(inputs2)

结果分别为


  
  1. tensor([[ 5.4772],
  2. [10.9545],
  3. [16.4317]])
  4. tensor([[ 3.7417, 7.4833, 11.2250, 14.9666]])

怎么来的?

inputs1:(p = 2,dim = 1)每行每一列数据进行2范数运算

5.4772 = \sqrt[2]{1^{2} + 2^{2} + 3^{2} + 4^{2}}

10.9545 = \sqrt[2]{2^{2} + 4^{2} + 6^{2} + 8^{2}}

15.4317 = \sqrt[2]{3^{2} + 6^{2} + 9^{2} + 12^{2}}

inputs2:(p = 2,dim = 0)每列每一行数据进行2范数运算

3.7417= \sqrt[2]{1^{2} + 2^{2} + 3^{2}}

7.4833 = \sqrt[2]{2^{2} + 4^{2} + 6^{2}}

11.2250 = \sqrt[2]{3^{2} + 6^{2} +9^{2}}

14.9666 = \sqrt[2]{4^{2} + 8^{2} + 12^{2}}


关注keepdim = False这个参数


  
  1. inputs3 = inputs.norm(p=2, dim=1, keepdim=False)
  2. print(inputs3)

inputs3

tensor([ 5.4772, 10.9545, 16.4317])
 

输出inputs1inputs3shape


  
  1. print(inputs1.shape)
  2. print(inputs3.shape)

  
  1. torch.Size([3, 1])
  2. torch.Size([3])

可以看到inputs3少了一维,其实就是dim=1(求范数)那一维(列)少了,因为从4列变成1列,就是3行中求每一行的2范数,就剩1列了,不保持这一维不会对数据产生影响

或者也可以这么理解,就是数据每个数据有没有用[]扩起来。

keepdim = True[]扩起来;

keepdim = False不用[]括起来~;


不写keepdim,则默认不保留dim的那个维度


  
  1. inputs4 = torch.norm(inputs, p=2, dim=1)
  2. print(inputs4)
tensor([ 5.4772, 10.9545, 16.4317])
 

不写dim,则计算Tensor中所有元素的2范数


  
  1. inputs5 = torch.norm(inputs, p=2)
  2. print(inputs5)
tensor(20.4939)
 

等价于这句话


  
  1. inputs6 = inputs.pow(2).sum().sqrt()
  2. print(inputs6)
tensor(20.4939)
 

20.4939 = \sqrt[2]{1^{2} + 2^{2} + 3^{2} + 4^{2} + 2^{2} + 4^{2} + 6^{2} + 8^{2} + 3^{2} + 6^{2} + 9^{2} + 12^{2}}


总之,norm操作后dim这一维变为1或者消失


三、整体代码


  
  1. """
  2. @author:nickhuang1996
  3. """
  4. import torch
  5. rectangle_height = 3
  6. rectangle_width = 4
  7. inputs = torch.randn(rectangle_height, rectangle_width)
  8. for i in range(rectangle_height):
  9. for j in range(rectangle_width):
  10. inputs[i][j] = (i + 1) * (j + 1)
  11. print(inputs)
  12. inputs1 = torch.norm(inputs, p=2, dim=1, keepdim=True)
  13. print(inputs1)
  14. inputs2 = torch.norm(inputs, p=2, dim=0, keepdim=True)
  15. print(inputs2)
  16. inputs3 = inputs.norm(p=2, dim=1, keepdim=False)
  17. print(inputs3)
  18. print(inputs1.shape)
  19. print(inputs3.shape)
  20. inputs4 = torch.norm(inputs, p=2, dim=1)
  21. print(inputs4)
  22. inputs5 = torch.norm(inputs, p=2)
  23. print(inputs5)
  24. inputs6 = inputs.pow(2).sum().sqrt()
  25. print(inputs6)

文章来源: nickhuang1996.blog.csdn.net,作者:悲恋花丶无心之人,版权归原作者所有,如需转载,请联系作者。

原文链接:nickhuang1996.blog.csdn.net/article/details/90698186

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。