You need a “soft” function to get a meaningful gradient. Use something like torch.nn.functional.grid_sample which interpolates between values in your tensor.
import torch
import torch.nn.functional as F
src = torch.arange(25, dtype=torch.float).reshape(1, 1, 5, 5).requires_grad_() # 1 x 1 x 5 x 5 with 0 ... 25
indices = torch.tensor([[-1, -1], [0, 0]], dtype=torch.float).reshape(1, 1, -1, 2) # 1 x 1 x 2 x 2
output = F.grid_sample(src, indices)
print(output) # tensor([[[[ 0., 12.]]]])
(-1, -1) is the top-left corner. (0, 0) is the center. The src has to be 4-d or 5-d (N x C x IH x IW). Same with indices. If you don’t have batch size (N) or channels ( C) set these to dimensions to size 1.
Hi @colesbury , will the indexing be differentiable when I need to apply different loss operations depending upon the magnitude of the values in the tensor.
Hi! I am wondering what’s meaningful gradient here? Is that possible the gradients are no meaning even the whole forward pass are differentiable? (the grad_fn exists).
Another question is that I only need to crop some region from the feature maps, instead using torch.nn.functional.grid_sample, can I simply use the feature_maps[y1:y2, x1:x2]. My target is to make the coordinates of box trainable, which can find the important region of feature maps. Thanks!