Issue with torch.mean

Hello,

I am sure this is a trivial problem :sweat_smile:. I am following a tutorial for grad CAMS which is a binary classification problem. I have 8 classes so my dimensions are different:

grad[0]

tensor([0., 1., 0., 0., 0., 0., 0., 0.], device='cuda:0')

I set my dim to (8,9) which is what I had expected it to be but it didn’t work. If i change the dim to this < or leave it as default from tutorial, I get the following error:

1

w = grad[0].mean(dim=(1,2), keepdim=True)

2

gradcam_map = (w * act[0]).sum(0).detach().cpu()

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-79-76f83767f6d3> in <module>
----> 1 w = grad[0].mean(dim=(1,2), keepdim=True)
      2 gradcam_map = (w * act[0]).sum(0).detach().cpu()

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

So what should I set my dim to?

Thanks!

Hi,

grad[0] only has one dimension. To get the mean over grad[0] use dim=1 or dim=0 as the error message suggests.

Regards.

Cheers for the response.
Unfortunately this causes problems with the rest of the tutorial. The author has used dim=(1,2) for what I assume is grad[0] = [0,1].

Any ideas ?

Hi,

I’ve just checked out the tutorial and I think grad[0] should be
of shape (128, x, x).

Regards.