PyTorch torch.renorm function
PyTorch torch reference manual
torch.renorm is a function in PyTorch used to renormalize tensors. It normalizes a tensor according to a specified norm, so that the norm of elements along the specified dimension does not exceed a given value.
Function definition
torch.renorm(input, p, dim, maxnorm)
Parameter Description:
input: the input tensorp: order of the normdim: dimension to normalize alongmaxnorm: maximum norm value
Usage examples
Example
import
torch
Creating tensors
x
=
torch.
tensor
(
[
[
2.0
,
4.0
,
6.0
]
,
[
3.0
,
6.0
,
9.0
]
]
)
Perform L2 norm normalization along dim=1 with a maximum norm of 1
y
=
torch.
renorm
(
x
,
p
=
2
,
dim
=
1
,
maxnorm
=
1
)
(
y
)
The output is:
tensor([[0.2673, 0.5345, 0.8018],
[0.2673, 0.5345, 0.8018]])
Example
import
torch
Creating tensors
x
=
torch.
tensor
(
[
[
1.0
,
2.0
,
3.0
]
]
)
Perform L1 norm normalization along dim=1
y
=
torch.
renorm
(
x
,
p
=
1
,
dim
=
1
,
maxnorm
=
1
)
(
y
)
The output is:
tensor([[0.1667, 0.3333, 0.5000]])