How to down-sample a tensor using interpolation?

I want to downsample the last feature map by 2 or 4 using interpolation. the function nn.Upsample can’t take fraction in the factor.

You could use grid_sample for bilinear interpolation.
I created a small example for this use case:

# Create fake image
image = torch.zeros(1, 3, 24, 24)
image[0, :, 6:18, 6:18] = 1.

# Create grid
out_size = 12
x = torch.linspace(-1, 1, out_size).view(-1, 1).repeat(1, out_size)
y = torch.linspace(-1, 1, out_size).repeat(out_size, 1)
grid =, y.unsqueeze(2)), 2)

image_small = F.grid_sample(image, grid)

import matplotlib.pyplot as plt
plt.imshow(image[0].permute(1, 2, 0).numpy())
plt.imshow(image_small[0].permute(1, 2, 0).numpy())

Also, you could try AvgPool2d if that fits your use case.


I’m using AvgPool2d but I’ll test your example thanks

Why the grid has 2 in its fourth dimension? what is related for ?

let’s say I have (B,1,x,y), does this procedure make the interpolation on the last two dimension only ? assuming that each last two dimensions are an image of channel/depth 1 ?

What about downsampling of non rectangular size ? if x and y are not equal

I did it this way

image = torch.zeros(2, 1, 24, 28)
image[0, :, 6:18, 6:18] = 1.

# Create grid
xout_size = 12
yout_size = 14
x = torch.linspace(-1, 1, yout_size).repeat(xout_size, 1)
y = torch.linspace(-1, 1, xout_size).view(-1, 1).repeat(1, yout_size)
grid =, y.unsqueeze(2)), 2)
grid = grid.unsqueeze_(0).repeat(2,1,1,1)
image_small = F.grid_sample(image, grid)

import matplotlib.pyplot as plt

But i got this error when I run the model

return torch._C._VariableFunctions.cudnn_grid_sampler(input, grid)
RuntimeError: get_device is not implemented for type torch.FloatTensor

The fourth dimension stores the x, y input pixel locations which are used to compute output.

Yes, as the doc states, grid_sample computes the output using input pixel locations from the grid, i.e. using x and y in your case.

Downsampling of a non rectangular size should also work.
Your code runs on my machine using PyTorch 0.4.0.
Which version are you using?

I have 0.4.0a0+847fad7

The error seems to be pointing to CuDNN? Are you using the code on GPU?
I’ve also tried it on my GPU and it runs without a problem.

Yes I’m running on GPU and cuda release 9.1, V9.1.85

Could you try to disable CuDNN for a moment and run it again?

torch.backends.cudnn.enabled = False

CudaSpatialGridSamplerBilinear_updateOutput received an invalid combination of arguments - got (int, !Variable!, !Variable!, !Variable!, int), but expected (int state, torch.cuda.FloatTensor input, torch.cuda.FloatTensor grid, torch.cuda.FloatTensor output, int padding_mode)

Could you post the code you are running?
Also, it seems you have an alpha version of 0.4.0.
Could you update to the current stable version using the instructions from the website?

xout_size =x4.shape[2]
yout_size = x4.shape[3]
xc = torch.linspace(-1, 1, yout_size).repeat(xout_size, 1)
yc = torch.linspace(-1, 1, xout_size).view(-1, 1).repeat(1, yout_size)
grid =, yc.unsqueeze(2)), 2)
grid = grid.unsqueeze_(0).repeat(self.MiniBatch, 1, 1, 1)

image_small = F.grid_sample(x, grid)

x4 is an out of shape (M,1,x,y) from a conv2d

I have the version cloned form Github.

It seems you don’t push grid to the GPU.
You probably cloned a few weeks ago, so I would suggest to update to the current stable 0.4.0 release.

I’m pulling the last github right now. could you give me the best way to update the current version after pulling please

Usually you don’t need to pull from master unless you need some features or bug fixes, which are not yet available in the stable binary release or if you would like to implement some new features or bug fixes yourself.
Just visit the homepage and have a look at the install instructions.

Using conda you can simply install PyTorch using

conda install pytorch torchvision -c pytorch

I don’t have conda. I’ll install it using python install and give the output

I got again the last version which is the same version as I had 0.4.0a0+847fad7

Probably the old version is still installed.
You might have to run

pip uninstall torch

a few times after building from source, until no installation is found.

pip -H uninstall torch
sudo python3 install
I have again

it seems like the verison I’m pulling from github is only this one