MSE loss backward fails while numpy-processed tensor is treated as a prediction tensor in MSE loss function

Ko_R: <p>I’m sorry,I’m new to pytorch.maybe it’s a stupid question <img src="https://discuss.pytorch.org/images/emoji/apple/joy.png?v=9" title=":joy:" class="emoji" alt=":joy:"></p>
<p>when I wanna train a model,whose output is a spherical tensor named y_pred. The shape is [B,C,H,W] and I need to transform this spherical tensor to cartesian tensor,but numpy has a convenient function named np.apply_along_axis which can map my own coordinate transformation function( my own function named : np_spherical2cartesian ) to every element along a specific dimension.</p>
<p>so my process is</p>
<pre><code class="lang-python">
# 1. change pytorch tensor to numpy array
# 2. transform coordinate in numpy array
pred_car = [np.apply_along_axis(np_spherical2cartesian, dim=0, y_pred[i].cpu().detach().numpy()) for i in range(bs)] # bs is batch-size

#3. numpy to pytorch tensor for mse calculation
pred_car = [torch.from_numpy(i) for i in pred_car]
pred_car = torch.stack(pred_car)

criterion = nn.MSELoss()
loss = criterion(pred_car,target_car)

</code></pre>
<p>I can print the loss value but I got this error when run loss.backward():</p>
<pre><code class="lang-bash">RuntimeError: 0 of tensors does not require grad and does not have a grad_fn
</code></pre>
<p>It seems that the gradient of post-processed tensor have disappeared, is there any solution to solve this?</p>
<p>Thank you guys very much!!</p>

KFrank: <p>Hi Ko!</p>
<aside class="quote no-group quote-modified" data-username="Ko_R" data-post="1" data-topic="90594" 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/ko_r/48/16285_2.png" class="avatar"> Ko_R:</div>
<blockquote>
<pre data-code-wrap="python"><code class="lang-python">pred_car = [np.apply_along_axis(np_spherical2cartesian, dim=0, y_pred[i].cpu().detach().numpy()) for i in range(bs)] # bs is batch-size
</code></pre>
<p>…</p>
<pre data-code-wrap="bash"><code class="lang-bash">RuntimeError: 0 of tensors does not require grad and does not have a grad_fn
</code></pre>
<p>It seems that the gradient of post-processed tensor have disappeared</p>
</blockquote>
</aside>
<p>This is to be expected.  Calling <code>y_pred[i].cpu().detach()</code><br>
“breaks the computation graph” and pytorch can no longer<br>
backpropagate through this part of the processing.</p>
<p>When you run your forward pass through your model and loss<br>
function, pytorch’s autograd facility remembers what functions<br>
were called (the “computation graph”) so that when you then<br>
backpropagate, it can calculate the gradients of the loss function.</p>
<p>But the functions you call in your forward pass must be<br>
autograd-aware, and numpy is not.</p>
<p>You either have to rewrite your numpy processing using pytorch<br>
tensor operators (in which case you get autograd “for free”), or<br>
you have to package this processing (and you can use numpy<br>
in it) as a pytorch <a href="https://pytorch.org/docs/stable/autograd.html#torch.autograd.Function" rel="noopener nofollow ugc">torch.autograd.Function</a>, and provide your own<br>
<code>backward()</code> method for your <code>Function</code>.</p>
<p>Good luck.</p>
<p>K. Frank</p>