I’m sorry,I’m new to pytorch.maybe it’s a stupid question
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.
so my process is
# 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)
I can print the loss value but I got this error when run loss.backward():
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, is there any solution to solve this?
Thank you guys very much!!