Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking a norm of matrix rows/cols in pytorch

Tags:

pytorch

The norm of a vector can be taken by

torch.norm(vec)

However, how to take a norm of a set of vectors grouped as a matrix (either as rows or columns)?

For example, if a matrix size is (5,8), then the rows norms should return a vector of norms of size (5).

like image 782
cerebrou Avatar asked Jan 01 '23 17:01

cerebrou


1 Answers

torch.norm without extra arguments performs what is called a Frobenius norm which is effectively reshaping the matrix into one long vector and returning the 2-norm of that. To take the norm along a particular dimension provide the optional dim argument.

For example torch.norm(mat, dim=1) will compute the 2-norm along the columns (i.e. this will compute the 2-norm of each row) thus converting a mat of size [N,M] to a vector of norms of size [N].

To compute the norm of the columns use dim=0.

like image 63
jodag Avatar answered Jan 27 '23 09:01

jodag