I have a 64x10x3x32x32 tensor g where the first coordinate is the batch_size. For every 10x3x32x32 subtensor I would like to compute the 2-norm and then sum them up. One way do it is like this
sum_2_normes = torch.zeros(1)
......
for i in range(batch_size):
sum_2_normes += g[i].norm(p=2)
I was wondering if there is a way to do it in one line and/or more efficiently. I tried torch.norm(g, p=2, dim=0) but, as expected according to the documentation I get a 10x3x32x32 tensor, instead of what I need which is a 64x1 tensor
@ptrblck I feel confused about matrix norm and vector norm. I kknow vector norm and matrix norm has different formulation. So how to tell torch.norm() to use which kind of nomr?
Hi, is the solution you proposed more effective than the “for loop” solution, due to, e.g., parallel computation (in theory computing the L2 norm for one batch is independent from another batch)?
Python for loops might be often slower than a single call into a specific function, as it could use vectorized code under the hood.
You could profile both approaches for your current workloads and chose the faster one.
For the norm calculation I would assume that avoiding the loop will yield a better performance.