title: How to build an autograd-compatible Pytorch module that resizes tensors like images?
body: <p>I was wondering if I can build an image resize module in Pytorch that takes a torch.tensor of 3*H*W as the input and return a tensor as the resized image.</p>
  
  <p>I know it is possible to convert tensor to PIL Image and use torchvision,
  but I also hope to back propagate gradients from the resized image to the original image, and the following example will return such error (in PyTorch 0.4.0 on Windows 10):</p>
  
  <pre><code>import numpy as np
  from torchvision import transforms
  
  t2i = transforms.ToPILImage()
  i2t = transforms.ToTensor()
  
  trans = transforms.Compose(
      t2i, transforms.Resize(size=200), i2t]
  )
  
  test = np.random.normal(size=[3, 300, 300])
  test = torch.tensor(test, requires_grad=True)
  resized = trans(test)
  resized.backward()
  
  print(test.grad)
  
  Traceback (most recent call last):
    File "D:/Projects/Python/PyTorch/test.py", line 41, in &lt;module&gt;
      main()
    File "D:/Projects/Python/PyTorch/test.py", line 33, in main
      resized = trans(test)
    File "D:\Anaconda3\envs\pytorch\lib\site-packages\torchvision\transforms\transforms.py", line 42, in __call__
      img = t(img)
    File "D:\Anaconda3\envs\pytorch\lib\site-packages\torchvision\transforms\transforms.py", line 103, in __call__
      return F.to_pil_image(pic, self.mode)
    File "D:\Anaconda3\envs\pytorch\lib\site-packages\torchvision\transforms\functional.py", line 102, in to_pil_image
      npimg = np.transpose(pic.numpy(), (1, 2, 0))
  RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.
  </code></pre>
  
  <p>It seems like I cannot "imresize" a tensor without detaching it from autograd first, but detaching it prevents me from computing gradients.</p>
  
  <p>Is there a way to build a torch function/module that does the same thing as <code>torchvision.transforms.Resize</code> that is autograd compatiable? Any help is much appreciated!</p>