Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Pytorch do row normalization for each matrix in a 3D Tensor(Variable)?

If I have a 3D Tensor (Variable) with size [a,b,c]. consider it as a b*c matrix, and I hope that all these a matrix got row normalized.

like image 308
Qinqing Liu Avatar asked Nov 21 '17 06:11

Qinqing Liu


People also ask

How do you normalize a tensor data?

To normalize the input tensor we first subtract the mean from the tensor and then the result is divided by the standard deviation. Print the tensor to see how the tensor looks like after normalization.

What is row normalization?

Row normalization has a name -- ipsative scaling -- which typically involves rescaling a set of features by either dividing by the maximum value for the set or subtracting the mean of the features.


1 Answers

You can use the normalize function.

import torch.nn.functional as f
f.normalize(input, p=2, dim=2)

The dim=2 argument tells along which dimension to normalize (divide each row vector by its p-norm.

like image 130
activatedgeek Avatar answered Jan 04 '23 01:01

activatedgeek