tensor.dist、norm 范数运算

tensor.dist、norm 范数运算_第1张图片

0 范数:当前向量中的元素之和

1 范数:对应向量元素只差的绝对值之和

2 范数: 元素的平方和再开方

p 范数: 元素的绝对值的P次方之和再开P次方

核范数:矩阵的奇异值(在参考参考别的资料)

tensor.dist、norm 范数运算_第2张图片
通过dist来计算向量与向量之间的距离度量
norm 是针对一个向量或者一个矩阵去计算范数 而 dist是用来衡量两个向量或者矩阵之间的距离

import torch

a = torch.rand(2, 1)
b = torch.rand(2, 1)
print(a, '\n', b)
print(torch.dist(a, b, p=1))  # 一范数
print(torch.dist(a, b, p=2))  # 二范数
print(torch.dist(a, b, p=3))  # 三范数

# a 的二范数
print(torch.norm(a))
# a 的一范数
print(torch.norm(a, p=1))
# a 的核范数
print(torch.norm(a, p='fro'))

执行结果:


tensor([[0.7361],
        [0.1522]]) 
 tensor([[0.0269],
        [0.1849]])
tensor(0.7419)
tensor(0.7099)
tensor(0.7092)
tensor(0.7516)
tensor(0.8882)
tensor(0.7516)

L1范数

tensor.dist、norm 范数运算_第3张图片

L2范数

tensor.dist、norm 范数运算_第4张图片

你可能感兴趣的:(pytorch,python,math,pytorch,python,深度学习)