8

Suppose I have two tensors:

a = torch.randn(10, 1000, 1, 4) b = torch.randn(10, 1000, 6, 4) 

Where the third index is the index of a vector.

I want to take the dot product between each vector in b with respect to the vector in a.

To illustrate, this is what I mean:

dots = torch.Tensor(10, 1000, 6, 1) for b in range(10): for c in range(1000): for v in range(6): dots[b,c,v] = torch.dot(b[b,c,v], a[b,c,0]) 

How would I achieve this using torch functions?

1 Answer 1

13
a = torch.randn(10, 1000, 1, 4) b = torch.randn(10, 1000, 6, 4) c = torch.sum(a * b, dim=-1) print(c.shape) 

torch.Size([10, 1000, 6])

c = c.unsqueeze(-1) print(c.shape) 

torch.Size([10, 1000, 6, 1])

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.