pytorch笔记:torch.norm

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

1 参数说明

参数 参数说明
input 输入的数据
p

指定的范数

fro

默认情况,Frobenius范数

\|\mathbf{A}\|_F=\sqrt{\sum_{i=1}^m \sum_{j=1}^n\left|a_{i j}\right|^2}

nuc 求核范数(矩阵奇异值的和)
p \sqrt[p]{\left|x_1\right|^p+\left|x_2\right|^p+\ldots+\left|x_n\right|^p}

dim 指定在哪个维度(如果不指定,那么默认是在所有维度上进行计算)
keepdim 是否保持维度

2 举例

import torch

a=torch.ones(2,2,3,4)
print(a)
'''
tensor([[[[1., 1., 1., 1.],
          [1., 1., 1., 1.],
          [1., 1., 1., 1.]],

         [[1., 1., 1., 1.],
          [1., 1., 1., 1.],
          [1., 1., 1., 1.]]],


        [[[1., 1., 1., 1.],
          [1., 1., 1., 1.],
          [1., 1., 1., 1.]],

         [[1., 1., 1., 1.],
          [1., 1., 1., 1.],
          [1., 1., 1., 1.]]]])
'''
print(torch.norm(a))
#tensor(6.9282)

猜你喜欢

转载自blog.csdn.net/qq_40206371/article/details/129835965