Does the nn.funtional.grid_sample backwards grad to grid??
grid_sample ( input , grid , mode=‘bilinear’ , padding_mode=‘zeros’ )
Hello, I just wonder that the bilinear interpolation operation based ‘grid_sample’ is often used to compute affine transformation or in the spatial transformaiton network
I already know that this function does propagate the grad to the input data
but does this function propagate the grad to the input grid?
@smth @Smrutiranjan_Sahu @system could you plz help me a little?
yes it computes gradient wrt grid.
although you will get 0 grad grid if you are using it with nearest neighbor sampling
why 0 grad with nearest mode but not ‘bilinear’ mode, big thx~
@SimonW I am little bit confused after hours of thinking, how actually did the back end kernel calculate the grad for each grid point? I just cant figure out, much thx
Because the gradient is zero almost everywhere and undefined otherwise. It’s the same reason why
ceil(x) gives all zero gradients.
In forward, each output pixel is a linear interpolation (weighted sum) of input pixels, where the interpolation weights are computed using the mode and grid values. In most cases, this computation is simple and differentiable, e.g., bilinear. So on a high level, it goes like grad_output -> grad_interpolation_weights -> grad grid. (In real code there are a lot of other detailed optimizations.) If you are interested, CPU kernel is at https://github.com/pytorch/pytorch/blob/b039a715ce4e9cca82ae3bf72cb84652957b2844/aten/src/ATen/native/cpu/GridSamplerKernel.cpp and GPU kernel is at https://github.com/pytorch/pytorch/blob/b039a715ce4e9cca82ae3bf72cb84652957b2844/aten/src/ATen/native/cuda/GridSampler.cu.