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 = torch.cat((x.unsqueeze(2), 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
plt.subplot(121)
plt.imshow(image[0,0].numpy())
plt.subplot(122)
plt.imshow(image_small[0,0].numpy())

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