解析torch.norm函数

官方文档:https://pytorch.org/docs/stable/generated/torch.norm.html?highlight=norm#torch.norm

函数:

norm(input, p='fro', dim=None, keepdim=False, out=None, dtype=None)
    Returns the matrix norm or vector norm of a given tensor.

参数:

  • input (Tensor) – 输入张量
  • p (int, float, inf, -inf, ‘fro’, ‘nuc’, optional) – 范数顺序.
    Default: 'fro',此时p为Frobenius范数计算中的幂指数值
 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 (int, 2-tuple of ints, 2-list of ints, optional):进行范数计算的维度
dim可以进行范数计算的情况
intvector norm
2-tuple of intsmatrix norm
Noneinput是二维tensor,matrix norm可以进行范数计算
input是一维tensor,vector norm可以进行范数计算
input是超过二维的tensor,vector norm将被应用到最后的dim

dim_example
Example:

import torch
from torch import tensor

a = tensor([[[1, 2, 3, 4], [1, 2, 3, 4]],
            [[0, 2, 3, 4], [0, 2, 3, 4]]], dtype=torch.float32)
print(a.shape)
a0 = torch.norm(a, p=2, dim=0)       # 对0 dim求vector 2-norm
a1 = torch.norm(a, p=2, dim=1)       # 对1 dim求vector 2-norm
a2 = torch.norm(a, p=2, dim=2)       # 对2 dim求vector 2-norm
a3 = torch.norm(a, p=2, dim=(0, 1))  # 对0,1 dim求matrix 2-norm
a4 = torch.norm(a, p=2, dim=None)    # input是超过二维的tensor,vector norm将被应用到最后的dim
print(a0)
print(a1)
print(a2)
print(a3)
print(a4)

Out:

torch.Size([2, 2, 4])
tensor([[1.0000, 2.8284, 4.2426, 5.6569],
        [1.0000, 2.8284, 4.2426, 5.6569]])
tensor([[1.4142, 2.8284, 4.2426, 5.6569],
        [0.0000, 2.8284, 4.2426, 5.6569]])
tensor([[5.4772, 5.4772],
        [5.3852, 5.3852]])
tensor([1.4142, 4.0000, 6.0000, 8.0000])
tensor(10.8628)
  • keepdim(bool, optional)– 是否保持输入时的维度. Default: False
b0 = torch.norm(a, p=2, dim=0, keepdim=True)
b1 = torch.norm(a, p=2, dim=0, keepdim=False)   # 就是刚才的a0
print(b0.shape)
print(b1.shape)

Out:

torch.Size([1, 2, 4])
torch.Size([2, 4])
  • out (Tensor, optional) – 结果张量.
  • dtype (:class:torch.dtype, optional): 返回的tensor的数据类型. Default: None.
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值