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).
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With