RuntimeError: element 0 of variables does not require grad and does not have a grad_fn

The following code is the source of your error -

You are essentially re-creating pred here which causes it to lose its computation graph.
It looks like a classification problem; try exploring loss functions like this one rather than defining your own like you’ve done here which also naturally has its requires_grad=False.