当前位置: 首页 > news >正文

Pytorch torch.norm函数详解用法

torch.norm参数定义

torch版本1.6

def norm(input, p="fro", dim=None, keepdim=False, out=None, dtype=None)

input

input (Tensor): the input tensor 输入为tensor

p

 p (int, float, inf, -inf, 'fro', 'nuc', optional): the order of norm. Default: ``'fro'``The following norms can be calculated:=====  ============================  ==========================ord    matrix norm                   vector norm=====  ============================  ==========================None   Frobenius norm                2-norm'fro'  Frobenius norm                --'nuc'  nuclear norm                  --Other  as vec norm when dim is None  sum(abs(x)**ord)**(1./ord)=====  ============================  ==========================
dim是matrix norm

如果inputmatrix norm,也就是维度大于等于2维,则
P值默认为fro,Frobenius norm可认为是与计算向量的欧氏距离类似
有时候为了比较真实的矩阵和估计的矩阵值之间的误差
或者说比较真实矩阵和估计矩阵之间的相似性,我们可以采用 Frobenius 范数。

在这里插入图片描述计算矩阵的Frobenius norm (Frobenius 范数),就是矩阵A各项元素的绝对值平方的总和再开根号

p='nuc’时,是求核范数,核范数是矩阵奇异值的和。核范数的具体定义为
在这里插入图片描述
在这里插入图片描述
例子来源:机器学习 | Schatten范数 - 知乎

p=other时,当作vec norm计算,p为int的形式,则是如下形式:
在这里插入图片描述
详细解释:torch.norm()函数的用法 - 知乎

dim是vector norm

p=none时,为L2 Norm,也是属于P范数一种,pytorch调用的函数是F.normalize,pytorch官网定义如下:,

dim

dim (int, 2-tuple of ints, 2-list of ints, optional): If it is an int,vector norm will be calculated, if it is 2-tuple of ints, matrix normwill be calculated. If the value is None, matrix norm will be calculatedwhen the input tensor only has two dimensions, vector norm will becalculated when the input tensor only has one dimension. If the inputtensor has more than two dimensions, the vector norm will be applied tolast dimension.

如果dimNone, 当input的维度只有2维时使用matrix norm,当input的维度只有1维时使用vector norm,当input的维度超过2维时,只在最后一维上使用vector norm
如果dim不为None,1.dim是int类型,则使用vector norm,如果dim是2-tuple int类型,则使用matrix norm.

Keepdim

keepdim (bool, optional): whether the output tensors have :attr:`dim`retained or not. Ignored if :attr:`dim` = ``None`` and:attr:`out` = ``None``. Default: ``False``

keepdim为True,则保留dim指定的维度,如果为False,则不保留。默认为False

out

out (Tensor, optional): the output tensor. Ignored if:attr:`dim` = ``None`` and :attr:`out` = ``None``.

输出为tensor,如果dim = None and out = None.则不输出

dtype

dtype (:class:`torch.dtype`, optional): the desired data type ofreturned tensor. If specified, the input tensor is casted to:attr:'dtype' while performing the operation. Default: None.

指定输出的数据类型

示例

>>> import torch
>>> a = torch.arange(9, dtype= torch.float) - 4
>>> a
tensor([-4., -3., -2., -1.,  0.,  1.,  2.,  3.,  4.])
>>> b = a.reshape((3, 3))
>>> b
tensor([[-4., -3., -2.],[-1.,  0.,  1.],[ 2.,  3.,  4.]])
>>> torch.norm(a)
>tensor(7.7460)
>>>计算流程: math.sqrt((4*4 + 3*3 + 2*2 + 1*1 +  -4*-4 + -3*-3 + -2*-2 + -1*-1))
7.7460
>>> torch.norm(b) # 默认计算F范数
tensor(7.7460)
http://www.lryc.cn/news/233880.html

相关文章:

  • 【DevOps】Git 图文详解(二):Git 安装及配置
  • 亚马逊美国站CPC认证ASTM F963测试项目要求有哪些?
  • 通付盾Web3专题 | KYT/AML:Web3合规展业的必要条件
  • Centos8配置Zabbix5.0中文汉化
  • 元数据管理,数字化时代企业的基础建设
  • 大数据之Hive:regexp_extract函数案例
  • tsconfig.json无法写入文件“XXXX“因为它会覆盖输入文件
  • 本周Github有趣项目:draw-a-ui等
  • VBA如何快速识别Excel单元格中的文本数字
  • Mysql数据库 16.SQL语言 数据库事务
  • docker 部署Redis集群(三主三从,以及扩容、缩容)
  • JavaScript 浮点数运算的精度问题及解决
  • 基于STM32的无线传感器网络(WSN)通信方案设计与实现
  • Flink和Kafka连接时的精确一次保证
  • UE4动作游戏实例RPG Action解析三:实现效果,三连击Combo,射线检测,显示血条,火球术
  • Linux/麒麟系统上部署Vue+SpringBoot前后端分离项目
  • STM32在FreeRTOS下的us延时
  • 软件测试/人工智能丨深入人工智能软件测试:PyTorch引领新时代
  • Android 当中的 Fragment 协作解耦方式
  • 城市网吧视频智能监控方案,实现视频远程集中监控
  • C#WPF视频播放器实例
  • 【uniapp】Google Maps
  • C语言变量与常量
  • AI创作系统ChatGPT网站源码/支持DALL-E3文生图/支持最新GPT-4-Turbo模型+Prompt应用
  • 二维码智慧门牌管理系统升级,异常门牌聚合解决方案助力高效管理
  • 【XTDrone Ubuntu20.04】XTDrone+ Ubuntu20.04 + PX4安装
  • 河北大学选择ZStack Cube超融合一体机打造实训云平台
  • IDEA远程一键部署SpringBoot到Docker
  • 索引三星结构
  • rust 笔记 高级错误处理