John
John

Reputation: 11

Pytorch tensor dimension multiplication

I'm trying to implement the grad-camm algorithm:

https://arxiv.org/pdf/1610.02391.pdf

My arguments are:

activations: Tensor with shape torch.Size([1, 512, 14, 14])

alpha values : Tensor with shape torch.Size([512])

I want to multiply each activation (in dimension index 1 (sized 512)) in each corresponding alpha value: for example if the i'th index out of the 512 in the activation is 4 and the i'th alpha value is 5, then my new i'th activation would be 20.

The shape of the output should be torch.Size([1, 512, 14, 14])

Upvotes: 0

Views: 4517

Answers (1)

Ivan
Ivan

Reputation: 40648

Assuming the desired output is of shape (1, 512, 14, 14).

You can achieve this with torch.einsum:

torch.einsum('nchw,c->nchw', x, y)

Or with a simple dot product, but you will first need to add a couple of additional dimensions on y:

x*y[None, :, None, None]

Here's an example with x.shape = (1, 4, 2, 2) and y = (4,):

>>> x = torch.arange(16).reshape(1, 4, 2, 2)
tensor([[[[ 0,  1],
          [ 2,  3]],

         [[ 4,  5],
          [ 6,  7]],

         [[ 8,  9],
          [10, 11]],

         [[12, 13],
          [14, 15]]]])

>>> y = torch.arange(1, 5)
tensor([1, 2, 3, 4])

>>> x*y[None, :, None, None]
tensor([[[[ 0,  1],
          [ 2,  3]],

         [[ 8, 10],
          [12, 14]],

         [[24, 27],
          [30, 33]],

         [[48, 52],
          [56, 60]]]])

Upvotes: 1

Related Questions