Solved by KFrank in post #2 
              
              
                Hi Pedro! 

torch.einsum is the general-purpose tool for “contracting” various 
indices of tensors.  Here is an example: 
>>> import torch
>>> torch.__version__
'1.7.1'
>>> a = 3
>>> b = 2
>>> c = 5
>>> d = 3
>>> e = 4
>>> t1 = torch.randn ((a, b, c))
>>> t2 = torch.randn ((a, d, e))
>>> tt = torch.…