How to access grad of view of tensor

Hi everyone,

  • I have a tensor t with requires_grad = True
  • I apply a boolean advanced indexing to t, say t1 = t[mask].
  • The mask is regular so I can reshape t1 to have a shape s, that is t2 = t1.reshape(s) (or t1.view(s))

Now, I want to access the gradient of t2, that is, to retrieve a tensor g with shape s containing the gradient of t at the corresponding positions of mask.

The view/reshape operators make t2 a non-leaf tensor, thus calling t2.grad returns None.

Is there a way to get these gradients?

You can do t2.retain_grad() before the backward.