目录

  • torch.norm参数定义
    • input
    • p
      • dim是matrix norm
      • dim是vector norm
    • dim
    • Keepdim
    • out
    • dtype
  • 示例

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’时,是求核范数,核范数是矩阵奇异值的和。核范数的具体定义为


例子来源:https://zhuanlan.zhihu.com/p/104402273

p=other时,当作vec norm计算,p为int的形式,则是如下形式:

详细解释:https://zhuanlan.zhihu.com/p/260162240

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)

【深度学习框架-torch】torch.norm函数详解用法相关推荐

  1. pytorch之torch.nn.Conv2d()函数详解

    文章目录 一.官方文档介绍 二.torch.nn.Conv2d()函数详解 参数详解 参数dilation--扩张卷积(也叫空洞卷积) 参数groups--分组卷积 三.代码实例 一.官方文档介绍 官 ...

  2. Keras深度学习实战(4)——深度学习中常用激活函数和损失函数详解

    Keras深度学习实战(4)--深度学习中常用激活函数和损失函数详解 常用激活函数 Sigmoid 激活函数 Tanh 激活函数 ReLU 激活函数 线性激活函数 Softmax 激活函数 损失函数 ...

  3. 深度学习(十四)——Softmax详解, 目标检测, RCNN

    https://antkillerfarm.github.io/ Softmax详解 首先给出Softmax function的定义: yc=ζ(z)c=ezc∑Cd=1ezd for c=1,-,C ...

  4. 【论文解读】深度学习目标检测的开山鼻祖 |R-CNN详解 | 两阶段目标检测代表

    目录 前言 目标检测近年里程碑 深度学习目标检测 1 R-CNN简介 1.1 何为R-CNN? 1.2 摘要 1.2.1 论文综述 1.2.2 优点汇总 1.2.3 缺点汇总 2. RCNN网络结构解 ...

  5. [学习C++ ]C++ STL 全排列函数详解(排列组合与匹配算法)--1

    一.概念 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列.当m=n时所有的排列情况叫全排列.如果这组数有n个,那么全排列数为n!个. 比如a ...

  6. PyTorch中torch.norm函数详解

    torch.norm() 是 PyTorch 中的一个函数,用于计算输入张量沿指定维度的范数.具体而言,当给定一个输入张量 x 和一个整数 p 时,torch.norm(x, p) 将返回输入张量 x ...

  7. one hot编码及torch.Tensor.scatter_()函数详解

    one hot 编码 One Hot编码,又称为一位有效编码,主要是采用N位状态寄存器来对N个状态进行编码,每个状态都由他独立的寄存器位,并且在任意时候只有一位有效. One Hot编码是分类变量作为 ...

  8. 【深度学习】ResNet——CNN经典网络模型详解(pytorch实现)

    建议大家可以实践下,代码都很详细,有不清楚的地方评论区见~ 1.前言 ResNet(Residual Neural Network)由微软研究院的Kaiming He等四名华人提出,通过使用ResNe ...

  9. 深度学习-目标检测YOLOV3 YOLOv4原理详解

    2.YoloV3核心基础内容 2.1 网络结构可视化 Yolov3是目标检测Yolo系列非常非常经典的算法,不过很多同学拿到Yolov3或者Yolov4的cfg文件时,并不知道如何直观的可视化查看网络 ...

  10. 机器学习、深度学习中常用的优化算法详解——梯度下降法、牛顿法、共轭梯度法

    一.梯度下降法 1.总述: 在机器学习中,基于基本的梯度下降法发展了三种梯度下降方法,分别为随机梯度下降法,批量梯度下降法以及小批量梯度下降法. (1)批量梯度下降法(Batch Gradient D ...

最新文章

  1. python使用函数的目的_在Python 3.x中经常看到定义函数有一个单独的 * 参数?定义这样参数的目的是?怎样对其取值呢?...
  2. python就业方向及工资-Python的5大就业方向,薪资诱人前景好!
  3. python读取excel日期内容读出来是数字-Python xlrd读取excel日期类型的2种方法
  4. 常见宽带错误解决方法
  5. keycode对应主要键的关系
  6. druid.properties文件的配置
  7. Spring Cloud文档阅读笔记-初识Spring Cloud(对Spring Cloud初步了解)
  8. 解析ThreadPoolExecutor类是如何保证线程池正确运行的
  9. 肌电信号 聚类 Matlab
  10. h5前端兼容性问题及解决方法集合
  11. c# 判断时间是否在 某一时间段内
  12. 湖北省武汉市社会保险参保缴费查询
  13. [leetcode] 935 Knight Dialer 骑士拨号器
  14. 键盘钢琴html代码,键盘钢琴KeyboardPiano(源代码)
  15. 词霸天下---141词根 【-habit- = -hibit- 拥有,占据,居住 】
  16. [数学建模] 微分方程--捕鱼业的持续发展
  17. zookeeper 监控
  18. eclipse突然报错:An internal error occurred during: “Compute launch button tooltip“.并且运行main方法时Run As后面是空
  19. ChatGPT 以及相关开源项目体验
  20. Hyperledger Fabric 1.0 快速搭建 -------- 多机部署 Fabric CA节点服务

热门文章

  1. VLAN技术详解三(VLAN三种访问模式)
  2. 【pyqt5学习】——pyqt5中.qrc资源文件的创建与编写
  3. Multisim14.1中/英文版软件下载和安装教程|兼容WIN10
  4. 乾颐堂现任明教教主(2014年课程)TCPIP协议详解卷一 第一节课笔记
  5. 整理了一份嵌入式相关开源项目、库、资料
  6. QT Icon设置异常
  7. WhiteSmoke无限试用
  8. CATIA软件有限元分析功能详解及使用教程
  9. 打印机出现另存为xps_打印机打印文件显示另存为xps格式的文件 - 卡饭网
  10. GB28181协议之设备录像查询