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


    torch.norm参数定义

    torch版本1.6

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

    input

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

    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)
                =====  ============================  ==========================
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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 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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如果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``
    
    • 1
    • 2
    • 3

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

    out

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

    输出为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.
    
    • 1
    • 2
    • 3

    指定输出的数据类型

    示例

    >>> 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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    C++初阶:C++入门
    计算机专业毕业设计项目推荐07-科研成果管理系统(JavaSpringBoot+Vue+Mysql)
    修改element-ui源码给el-dialog添加全屏功能
    【WLAN】Android 13 WiFi Display 介绍和常规问题分析
    系统设计 - 我们如何通俗的理解那些技术的运行原理 - 第二部分:CI CD、设计模式、数据库
    c++ 中string、char*、char a[]
    阿里云大数据实战记录9:MaxCompute RAM 用户与授权
    竞赛选题 基于深度学习的目标检测算法
    Go语言入门心法(九): 引入三方依赖
    昇腾Ascend 随记 —— 昇腾 AI 的基本架构
  • 原文地址:https://blog.csdn.net/qq_36287702/article/details/126330404