How to perform sum of squares of matrices in pytorch?

For example, I have a matrix M of size (m, n). And I want to sum elementwise powers of this matrix M: e.g.
M^a + M^b + M^c. [a,b,c] will be given as a tensor. Note that it is elementwise not matrix multiplication.

Using for loop is too slow.

Is this what you are after?

m, n = 2, 3
M = torch.randint(10,(m, n))

a, b, c = 2.0, 3.0, 4.0
power = torch.tensor([a,b,c])

torch.pow(M.repeat(power.shape[0],1).reshape(power.shape[0],-1), power.repeat(m*n,1).t()).sum(0).view(m,n)

Thanks for help. I have solved the problem. What I need is something like this. If my matrix M is of size [2, 10572] and n is of size [3]. I can do M[:, None, :] ** n[:, None] and get output of size [2, 3, 10572]. Then i just need to sum along axis 1.

I think this is very efficient. Not sure about other solutions provided here though. Have not tested.