4

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.

  1. grid_sample interpolates in 2D or 3D, but not in 1D.
  2. PyTorch requires batch size of data and index to be the same. But in my case, data does not have a batch dimension i.e., data is common for every element in index. I don't want to repeat data, 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.

Nagabhushan S N
  • 6,407
  • 8
  • 44
  • 87
  • 1
    For your 2nd concern, you can extend `index` instead of repeating it. – Ivan Apr 06 '23 at 07:59
  • Bilinear interpolation is already implemented in `torch.nn.functional.interpolate` – DerekG Apr 06 '23 at 13:59
  • @DerekG `interpolate` simply upsamples the given `data` tensor. I don't want that. I want to sample `data` tensor at different locations given by `index` tensor. – Nagabhushan S N Apr 07 '23 at 00:54
  • Can you provide a bit more detail on "I need to linearly interpolate `data` along dimension 1 and stack the resulting 2D tensors". It's not clear from the question how the indexing value effects this interpolation. – DerekG Apr 07 '23 at 14:53
  • I'd also note that using `expand` can be used to provide a tensor view of data with the same attributes as `repeat`, i.e. data is not copied. This is likely a preferable approach if it lets you use `grid_sample` as you described. – DerekG Apr 07 '23 at 14:54

0 Answers0