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

这篇具有很好参考价值的文章主要介绍了【深度学习框架-torch】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 范数。

torch.norm,深度学习,深度学习,pytorch,人工智能,torch.norm,正则化计算矩阵的Frobenius norm (Frobenius 范数),就是矩阵A各项元素的绝对值平方的总和再开根号

p='nuc’时,是求核范数,核范数是矩阵奇异值的和。核范数的具体定义为
torch.norm,深度学习,深度学习,pytorch,人工智能,torch.norm,正则化
torch.norm,深度学习,深度学习,pytorch,人工智能,torch.norm,正则化
例子来源:https://zhuanlan.zhihu.com/p/104402273

p=other时,当作vec norm计算,p为int的形式,则是如下形式:
torch.norm,深度学习,深度学习,pytorch,人工智能,torch.norm,正则化
详细解释:https://zhuanlan.zhihu.com/p/260162240

dim是vector norm

p=none时,为L2 Norm,也是属于P范数一种,pytorch调用的函数是F.normalize,pytorch官网定义如下:torch.norm,深度学习,深度学习,pytorch,人工智能,torch.norm,正则化

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 norm
            will be calculated. If the value is None, matrix norm will be calculated
            when the input tensor only has two dimensions, vector norm will be
            calculated when the input tensor only has one dimension. If the input
            tensor has more than two dimensions, the vector norm will be applied to
            last 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 of
            returned tensor. If specified, the input tensor is casted to
            :attr:'dtype' while performing the operation. Default: None.

指定输出的数据类型文章来源地址https://www.toymoban.com/news/detail-694234.html

示例

>>> 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函数详解用法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Pytorch】梯度裁剪——torch.nn.utils.clip_grad_norm_的原理及计算过程

    众所周知,梯度裁剪是为了防止梯度爆炸。在训练FCOS算法时,因为训练过程出现了损失为NaN的情况,在github issue有很多都是这种训练过程出现loss为NaN,作者也提出要调整梯度裁剪的超参数,于是理了理梯度裁剪函数 torch.nn.utils.clip_grad_norm_ 的计算过程,方便调参。 torch.nn.u

    2024年02月12日
    浏览(38)
  • 【Pytorch】torch.max() 函数详解

    参数: input (Tensor) – 输入张量 返回输入张量所有元素中的最大值。 输出结果: 返回张量 input 在压缩指定维度 dim 时的最大值及其下标。 输出结果: 返回两张量 input 和 other_input 在对应位置上的最大值形成的新张量。 输出结果: 详解 torch.max 函数

    2024年01月23日
    浏览(38)
  • Pytorch函数——torch.gather详解

    在学习强化学习时,顺便复习复习pytorch的基本内容,遇到了 torch.gather() 函数,参考图解PyTorch中的torch.gather函数 - 知乎 (zhihu.com)进行解释。 pytorch官网对函数给出的解释: 即input是一个矩阵,根据dim的值,将index的值替换到不同的维度的 索引 ,当dim为0时,index替代i的值,成为

    2024年01月18日
    浏览(30)
  • 【Torch API】pytorch 中bincount()函数详解

    torch.bincount 是 PyTorch 中的函数,用于计算给定整数张量中每个值的出现次数。它返回一个张量,其中的每个元素表示输入张量中对应索引值出现的次数。 具体而言, torch.bincount 函数的语法如下: 其中: input 是输入的整数张量,可以是一维或多维的。 weights 是可选的权重张量

    2024年02月11日
    浏览(65)
  • 详解torch.nn.utils.clip_grad_norm_ 的使用与原理

    本文是对梯度剪裁: torch.nn.utils.clip_grad_norm_()文章的补充。所以可以先参考这篇文章 从上面文章可以看到, clip_grad_norm 最后就是对所有的梯度乘以一个 clip_coef ,而且乘的前提是 clip_coef一定是小于1的 ,所以,按照这个情况: clip_grad_norm 只解决梯度爆炸问题,不解决梯度消失

    2023年04月08日
    浏览(29)
  • 【Torch API】pytorch 中repeat_interleave函数详解

    torch. repeat_interleave ( input ,  repeats ,  dim=None ) → Tensor Repeat elements of a tensor. Parameters input  (Tensor) – the input tensor. repeats  (Tensor  or  int) – The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis. dim  (int ,  optional ) – The dimension along which to repeat values

    2024年02月11日
    浏览(26)
  • 【深度学习笔记】彻底理解torch中的tensor与numpy中array区别及用法

    刚接触深度学习的同学,很多开源项目代码中, 张量tensor 与 数组array 都有使用,不清楚两者有什么区别,以及怎么使用,如何相互转换等。博主起初也有类似的疑惑,经过查阅资料以及实践,逐渐有了深入了解,本文将记录并分享自己对两者的理解,可供参考。 提示:以下

    2023年04月08日
    浏览(60)
  • 深度学习:Pytorch安装的torch与torchvision的cuda版本冲突问题与解决历程记录

    今天不小心将conda环境中的一个pytorch环境中的torch包给搞混了,将其更新了一下,发生了一些问题: 当时运行了一下这个代码:  pip install torchvision --upgrade 导致了环境中包的混乱: 只能说欲哭无泪,当时这个 pytorch环境中我是安装的CUDA11.8的版本应该,后来安装了cpu版本的将

    2024年02月20日
    浏览(37)
  • Pytorch深度学习-----神经网络之线性层用法

    PyTorch深度学习——Anaconda和PyTorch安装 Pytorch深度学习-----数据模块Dataset类 Pytorch深度学习------TensorBoard的使用 Pytorch深度学习------Torchvision中Transforms的使用(ToTensor,Normalize,Resize ,Compose,RandomCrop) Pytorch深度学习------torchvision中dataset数据集的使用(CIFAR10) Pytorch深度学习--

    2024年02月14日
    浏览(25)
  • Pytorch学习笔记(5):torch.nn---网络层介绍(卷积层、池化层、线性层、激活函数层)

     一、卷积层—Convolution Layers  1.1 1d / 2d / 3d卷积 1.2 卷积—nn.Conv2d() nn.Conv2d 1.3 转置卷积—nn.ConvTranspose nn.ConvTranspose2d  二、池化层—Pooling Layer (1)nn.MaxPool2d (2)nn.AvgPool2d (3)nn.MaxUnpool2d  三、线性层—Linear Layer  nn.Linear  四、激活函数层—Activate Layer (1)nn.Sigmoid  (

    2024年01月20日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包