官方文档: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 | 可以进行范数计算的情况 |
---|
int | vector norm | 2-tuple of ints | matrix norm | None | input是二维tensor,matrix norm可以进行范数计算 | | input是一维tensor,vector norm可以进行范数计算 | | input是超过二维的tensor,vector norm将被应用到最后的dim |
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)
a1 = torch.norm(a, p=2, dim=1)
a2 = torch.norm(a, p=2, dim=2)
a3 = torch.norm(a, p=2, dim=(0, 1))
a4 = torch.norm(a, p=2, dim=None)
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)
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 .
|