Hi Ko!



 Ko_R:

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

…
RuntimeError: 0 of tensors does not require grad and does not have a grad_fn

It seems that the gradient of post-processed tensor have disappeared


This is to be expected.  Calling y_pred[i].cpu().detach()
“breaks the computation graph” and pytorch can no longer
backpropagate through this part of the processing.
When you run your forward pass through your model and loss
function, pytorch’s autograd facility remembers what functions
were called (the “computation graph”) so that when you then
backpropagate, it can calculate the gradients of the loss function.
But the functions you call in your forward pass must be
autograd-aware, and numpy is not.
You either have to rewrite your numpy processing using pytorch
tensor operators (in which case you get autograd “for free”), or
you have to package this processing (and you can use numpy
in it) as a pytorch torch.autograd.Function 3, and provide your own
backward() method for your Function.
Good luck.
K. Frank