Hi kfiros!



 kfiros:

I want to do in tensor operation and not in loop


torch.einsum() is your go-to tool when you want to sum multiplied slices
(“contract indices”) of tensors.
Here is an example script:
import torch
print (torch.__version__)

_ = torch.manual_seed (2021)

N = 5
m = 3

matrices_tensor = torch.randn (N, N, m, m)
target = torch.randn (N, m, 1)

output_einsum = torch.einsum ('ijkl, jlm -> ik', matrices_tensor, target)

output = torch.zeros((N, m))
for i in range (N):
    for j in range (N):
        output[i, :] += (matrices_tensor[i, j] @ target[j, :]).squeeze()

print ('output_einsum.shape =', output_einsum.shape)
print ('output.shape =', output.shape)
print (torch.allclose (output_einsum, output))

And here is its output:
1.7.1
output_einsum.shape = torch.Size([5, 3])
output.shape = torch.Size([5, 3])
True

(Note that your target has a trailing singleton dimension, while your
output does not so I had to squeeze() the result of the matrix
multiplication to make your code work.)
Best.
K. Frank