How to vectorize this matrix equation?

Hi, I have the following matrix equation but I’m not sure how to implement this in PyTorch without using for loop?

image

Thanks a lot.

exclude:

t=a*b
s=t.sum(dim,keepdim=True)
c=s-t

1 Like

A solution along the same logic of the answer given by @googlebot, but using PyTorch functions:

C = torch.sum(torch.mul(A, B)) - torch.mul(A, B)

This should give you what you want! Assumption here is that A and B are 1D tensors, of course.

1 Like