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

username: hao_xi
cooked: <p>Where can I find the backward function of torch.nn.functional.interpolate function?<br>
<a href="https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html#torch.nn.functional.interpolate" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html#torch.nn.functional.interpolate</a></p>

username: ruby05guy
cooked: <p>[quote=“hao_xi, post:1, topic:213447, full:true”] <a href="https://www.ingresosolidario.com.co" rel="noopener nofollow ugc">Inscripción</a><br>
Where can I find the backward function of torch.nn.functional.interpolate function?<br>
<a href="https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html#torch.nn.functional.interpolate" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html#torch.nn.functional.interpolate</a><br>
[/quote]</p>
<p>Hello,</p>
<p>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.</p>

username: ptrblck
cooked: <p>Depending on the used algorithm, you should be able to find the used method by checking the <code>grad_fn</code>:</p>
<pre data-code-wrap="python"><code class="lang-python">x = torch.randn(1, 10, 10, requires_grad=True)
torch.nn.functional.interpolate(x, size=(8)).grad_fn
&lt;UpsampleNearest1DBackward0 object at 0x7f9f6f847e80&gt;
</code></pre>

username: hao_xi
cooked: <p>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?</p>
<aside class="quote no-group" data-username="ptrblck" data-post="3" data-topic="213447" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>Depending on the used algorithm, you should be able to find the used method by checking the <code>grad_fn</code>:</p>
<pre data-code-wrap="python"><code class="lang-python">x = torch.randn(1, 10, 10, requires_grad=True)
torch.nn.functional.interpolate(x, size=(8)).grad_fn
&lt;UpsampleNearest1DBackward0 object at 0x7f9f6f847e80&gt;
</code></pre>
</blockquote>
</aside>

username: hao_xi
cooked: <p>import inspect<br>
data = torch.randn((1,3,224,224), dtype=torch.float32, requires_grad=True)</p>
<p>out = F.interpolate(data, scale_factor=2)<br>
print(inspect.getmro(type(out.grad_fn)))</p>
<blockquote>
<blockquote>
<p>(&lt;class ‘UpsampleNearest2DBackward1’&gt;, &lt;class ‘object’&gt;)</p>
</blockquote>
</blockquote>
<p>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.</p>
<aside class="quote no-group" data-username="ptrblck" data-post="3" data-topic="213447" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>Depending on the used algorithm, you should be able to find the used method by checking the <code>grad_fn</code>:</p>
<pre data-code-wrap="python"><code class="lang-python">x = torch.randn(1, 10, 10, requires_grad=True)
torch.nn.functional.interpolate(x, size=(8)).grad_fn
&lt;UpsampleNearest1DBackward0 object at 0x7f9f6f847e80&gt;
</code></pre>
</blockquote>
</aside>

username: ptrblck
cooked: <p>To implement a custom <code>backward</code> method you could write custom <code>autograd.Function</code>s as described in <a href="https://pytorch.org/tutorials/beginner/examples_autograd/two_layer_net_custom_function.html">this tutorial</a>.</p>