You are detaching the computation graph in a few places:


int(round(y_pred)) will detach y_pred so that the backward call cannot compute the gradients of all preceding operations
numpy operations are not differentiable in PyTorch, so you would have to use the corresponding PyTorch method or write the backward function manually
re-wrapping the output in a torch.tensor(, requires_grad=True) will also detach the input from all preceding operations