How to channel-wise multiply an matrix and a vector

Hi all,

I’d like to implement a function like the squeeze-excitation attention, for example, we have a matrix BxCxHxW, and we also have an C-dim vector (both are in the form of tensor). I’d like to channel-wise multiply the matrix and vector. How can I implement it?

Previously, in senet, we just do it by: mat*camap, but I have tested it on pytorch 1.2, it shows
where mat: 3x16x16, camat: 3-dim vector

mat*camap

Traceback (most recent call last):

File “”, line 1, in

RuntimeError: The size of tensor a (16) must match the size of tensor b (3) at non-singleton dimension 2

You can do like this:
mat * camap.unsqueeze(dim=-1). unsqueeze(dim=-1)

Thanks. I’ll try. One more question, if we have the batch dimension in mat, we also have to expand the batch dimension for the camap or not?

Yes, it is necessary.

@Sunshine352, Thanks so much.