Contracting two rank 3 tensors along a certain dimension

I want to multiply two tensors so that the first dimension is kept but every 6 elements of a[:] are element-wise scalar-multiplied with its associated elements in b so that the resulting tensor c is of shape [1,3,2]. I tried to use torch.dot, and other methods and ended up using Einsteins sum convention, but I still cant seem to get it right

a = torch.ones(3,3,2)
    print("a", a.shape)
    b = torch.ones(3,1,1)
    print("b", b.shape)
    c = torch.einsum('bij,bkl->ij', a, b)
    print("c", c.shape)

Help is appreciated

I’m probably missing something, but can’t you just do a * b?

I cannot, since that does yield a contraction along the first dimension

'bij,bij->ij' - you need to tell the relation between the non-contracted dims, too.
And unsqueeze to get the 0 dim back if you need it.

Best regards

Thomas

1 Like

thank you that worked