Successful resolution of pytorch vector normalized vector length divided by the mold

welcome to my blog

Problem Description: Want to vector normalization, or let the vector length divided by the mold

Solutions, see example

import torch
import torch.nn.functional as F
a = torch.arange(9, dtype= torch.float)
a = a.reshape((3,3))
print(a)
'''
tensor([[0., 1., 2.],
        [3., 4., 5.],
        [6., 7., 8.]])
'''
# 对二维数组按行归一化
# p=2表示二范式, dim=1表示按行归一化
b = F.normalize(a, p=2, dim=1)
print(b)
'''
tensor([[0.0000, 0.4472, 0.8944],
        [0.4243, 0.5657, 0.7071],
        [0.4915, 0.5735, 0.6554]])
'''
Published 489 original articles · won praise 101 · Views 150,000 +

Guess you like

Origin blog.csdn.net/littlehaes/article/details/103593400