Where can I find the backward function of torch.nn.functional.interpolate function?

Where can I find the backward function of torch.nn.functional.interpolate function?
https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html#torch.nn.functional.interpolate

[quote=“hao_xi, post:1, topic:213447, full:true”] Inscripción
Where can I find the backward function of torch.nn.functional.interpolate function?
https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html#torch.nn.functional.interpolate
[/quote]

Hello,

The backward function for torch.nn.functional.interpolate doesn’t exist directly in PyTorch as the operation is not inherently differentiable in a simple inverse sense. For operations like interpolation, where you upscale or downscale images, there’s no exact reverse operation, but you can achieve similar results by using the torch.nn.functional.grid_sample function for specific tasks, like reversing an upsampling by creating the appropriate grid for sampling.

Depending on the used algorithm, you should be able to find the used method by checking the grad_fn:

x = torch.randn(1, 10, 10, requires_grad=True)
torch.nn.functional.interpolate(x, size=(8)).grad_fn
<UpsampleNearest1DBackward0 object at 0x7f9f6f847e80>

Thank you very much for your reply. I also want to know where I can find the calculation source code for this upsampleNearest1DBackward0 implementation, or how can I manually call its gradient calculation function?

import inspect
data = torch.randn((1,3,224,224), dtype=torch.float32, requires_grad=True)

out = F.interpolate(data, scale_factor=2)
print(inspect.getmro(type(out.grad_fn)))

(<class ‘UpsampleNearest2DBackward1’>, <class ‘object’>)

The grad_fn of this tensor seems to be an object of a class. Then I have an idea. Can I manually call the function of this grad_fn to implement the gradient calculation I want to implement? For example, this scenario: I have a two-layer CNN, and then I want to implement its forward and reverse processes myself instead of calling the existing interface.

To implement a custom backward method you could write custom autograd.Functions as described in this tutorial.