I have a 3D tensor data
of shape (N, W, D)
and another 1D tensor index
of shape (B,)
. I need to sample data
using index
such that my output should be of shape (B,N,D)
. That is, for every element of index
, I need to linearly interpolate data
along dimension 1 and stack the resulting 2D tensors.
Is it possible to do this using PyTorch grid_sample
? If yes, how?
The problems I'm facing are the following.
grid_sample
interpolates in 2D or 3D, but not in 1D.- PyTorch requires batch size of
data
andindex
to be the same. But in my case,data
does not have a batch dimension i.e.,data
is common for every element inindex
. I don't want to repeatdata
,B
times unless there is no other way.
I could write a bilinear interpolation code myself, but I don't want to handle all the boundary cases myself. grid_sample
supports zero padding, which I want to use directly.