I want to implement a typical attention mechanism and I need to compute the dot product between a sequence of vectors and a query vector. I was wondering, which is the best way to implement this operation with batched data.
Suppose that I have the following data:
import torch

batch_size = 32
seq_length = 50
dim = 100

sequence = torch.randn(batch_size, seq_length, dim)
query = torch.randn(batch_size, dim)

What i need as an output is a tensor with dimenionality (batch_size, seq_length).
Edit:
What i am doing now is this:
energies = torch.matmul(sequence, query.unsqueeze(2))