Attention Between 3D Matrix and a vector


I’m trying to implement channel attention. I have a matrix of shape NxCxWxH where N is batch size, C is channels, W width, H height and a vector of size NxC. I’m trying to perform a multipication between them.
I already tried to x * y, torch.matmul(x, y) x * y.expand_as(y) but they all resulted in an error that there is a dimension mismatch.
How can I implement it?

There are many ways to achieve it. One of the easy ways to multiply tensors with multiple dimensions is to make use of torch.einsum().

In your case, you can code something like below:

N,C,H,W = 2,3,4,5

import torch
x = torch.randn(N,C,H,W)
y = torch.randn(N,C)
torch.einsum('bchw,bc->bhw', x, y)

Thanks! However I need the output to be in shape (N, C, H, W), like the first input.

use torch.unsqueeze(dim=1) to introduce a singleton dimension after the multiplication.

result = torch.einsum('bchw,bc->bhw', x, y) # b,h,w
result = result.unsqueeze(dim=1)  # b,1,h,w

You can look at torch.Tensor API page to know the different functions available to manipulate a tensor.