Hi
The problem is that if you have to do add onehot_pred.requires_grad = True, that means it was False before. And so that means that some non-differentiable operation was applied to it.
Also fyi you can change dc = torch.tensor(0).float() to dc = 0.
And onehot_pred = torch.Tensor() to onehot_pred = [], onehot_pred = torch.cat((onehot_pred,pred),dim=0) to onehot_pred.append(pred) and add after the loop onehot_pred = torch.cat(onehot_pred, dim=0).