Solved by KFrank in post #2 
              
              
                Hi kfiros! 

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, …