*Memos:
- My post explains linalg.norm().
- My post explains linalg.matrix_norm().
linalg.vector_norm() can get the 0D or more D tensor of the one or more elements computed with vector norm from the 0D or more D tensor of zero or more elements as shown below:
*Memos:
-
linalg.vector_norm()
can be used with torch but not with a tensor. - The 1st argument with
torch
isinput
orx
(Required-Type:tensor
offloat
orcomplex
): *Memos:- It must be the 0D or more D tensor of zero or more elements.
- A
complex
tensor returns afloat
tensor even ifdtype=torch.complex64
is set tolinalg.vector_norm()
which is a bug.
- The 2nd argument with
torch
isord
(Optional-Default:2
-Type:int
orfloat
). *It sets a norm. - The 3rd argument with
torch
isdim
(Optional-Default:None
-Type:int
ortuple
orlist
ofint
). - The 4th argument with
torch
iskeepdim
(Optional-Default:False
-Type:bool
). *My post explainskeepdim
argument. - There is
dtype
argument withtorch
(Optional-Default:None
-Type:dtype): *Memos:- If it's
None
, it's inferred frominput
orx
. -
dtype=
must be used. -
My post explains
dtype
argument.
- If it's
- There is
out
argument withtorch
(Optional-Default:None
-Type:tensor
): *Memos:-
out=
must be used. -
My post explains
out
argument.
-
- If
dim
isNone
, theinput
(x
) tensor is flattened to a 1D tensor. -
ord
supports the following norms: *Memos:-
inf
can betorch.inf
,float('inf')
, etc: - There are also L3 norm(
ord=3
), L4 norm(ord=4
) etc.
-
ord |
Vector norm |
---|---|
inf |
max(abs(x)) |
-inf |
min(abs(x)) |
0 |
sum(x != 0) (L0 norm) |
1 |
sum(abs(x)^{ord})^{(1/ord)} (L1 norm) |
-1 |
Same as above |
2 (Default) |
Same as above(L2 norm) |
-2 |
Same as above |
Other int or float
|
Same as above |
import torch
from torch import linalg
my_tensor = torch.tensor([-3., -2., -1., 0., 1., 2., 3., 4.])
linalg.vector_norm(input=my_tensor) # L2 norm
# tensor(6.6332)
linalg.vector_norm(input=my_tensor, ord=0) # L0 norm
# tensor(7.)
linalg.vector_norm(input=my_tensor, ord=1) # L1 norm
# tensor(16.)
linalg.vector_norm(input=my_tensor, ord=-1)
# tensor(0.)
linalg.vector_norm(input=my_tensor, ord=2) # L2 norm
# tensor(6.6332)
linalg.vector_norm(input=my_tensor, ord=-2)
# tensor(0.)
linalg.vector_norm(input=my_tensor, ord=torch.inf)
# tensor(4.)
linalg.vector_norm(input=my_tensor, ord=-torch.inf)
# tensor(0.)
my_tensor = torch.tensor([[-3., -2., -1., 0.],
[1., 2., 3., 4.]])
linalg.vector_norm(input=my_tensor) # L2 norm
# tensor(6.6332)
linalg.vector_norm(input=my_tensor, ord=0) # L0 norm
# tensor(7.)
linalg.vector_norm(input=my_tensor, ord=1) # L1 norm
# tensor(16.)
linalg.vector_norm(input=my_tensor, ord=-1)
# tensor(0.)
linalg.vector_norm(input=my_tensor, ord=2) # L2 norm
# tensor(6.6332)
linalg.vector_norm(input=my_tensor, ord=-2)
# tensor(0.)
linalg.vector_norm(input=my_tensor, ord=torch.inf)
# tensor(4.)
linalg.vector_norm(input=my_tensor, ord=-torch.inf)
# tensor(0.)
my_tensor = torch.tensor([[[-3., -2.], [-1., 0.]],
[[1., 2.], [3., 4.]]])
linalg.vector_norm(input=my_tensor) # L2 norm
# tensor(6.6332)
linalg.vector_norm(input=my_tensor, ord=0) # L0 norm
# tensor(7.)
linalg.vector_norm(input=my_tensor, ord=0, dim=2) # L0 norm
linalg.vector_norm(input=my_tensor, ord=0, dim=-1) # L0 norm
# tensor([[2., 1.],
# [2., 2.]])
linalg.vector_norm(input=my_tensor, ord=1) # L1 norm
# tensor(16.)
linalg.vector_norm(input=my_tensor, ord=1, dim=2) # L1 norm
linalg.vector_norm(input=my_tensor, ord=1, dim=-1) # L1 norm
# tensor([[5., 1.],
# [3., 7.]])
linalg.vector_norm(input=my_tensor, ord=-1)
# tensor(0.)
linalg.vector_norm(input=my_tensor, ord=-1, dim=2)
linalg.vector_norm(input=my_tensor, ord=-1, dim=-1)
# tensor([[1.2000, 0.0000],
# [0.6667, 1.7143]])
linalg.vector_norm(input=my_tensor, ord=2) # L2 norm
# tensor(6.6332)
linalg.vector_norm(input=my_tensor, ord=2, dim=2) # L2 norm
linalg.vector_norm(input=my_tensor, ord=2, dim=-1) # L2 norm
# tensor([[3.6056, 1.0000],
# [2.2361, 5.0000]])
linalg.vector_norm(input=my_tensor, ord=-2)
# tensor(0.)
linalg.vector_norm(input=my_tensor, ord=-2, dim=2)
linalg.vector_norm(input=my_tensor, ord=-2, dim=-1)
# tensor([[1.6641, 0.0000],
# [0.8944, 2.4000]])
linalg.vector_norm(input=my_tensor, ord=torch.inf)
# tensor(4.)
linalg.vector_norm(input=my_tensor, ord=torch.inf, dim=2)
linalg.vector_norm(input=my_tensor, ord=torch.inf, dim=-1)
# tensor([[3., 1.],
# [2., 4.]])
linalg.vector_norm(input=my_tensor, ord=-torch.inf)
# tensor(0.)
linalg.vector_norm(input=my_tensor, ord=-torch.inf, dim=2)
linalg.vector_norm(input=my_tensor, ord=-torch.inf, dim=-1)
# tensor([[2., 0.],
# [1., 3.]])
my_tensor = torch.tensor([[[-3.+0.j, -2.+0.j], [-1.+0.j, 0.+0.j]],
[[1.+0.j, 2.+0.j], [3.+0.j, 4.+0.j]]])
linalg.vector_norm(input=my_tensor, dtype=torch.complex64) # L2 norm
# tensor(6.6332)
Top comments (0)