The error log should show which line is producing the error, you can refer on that.
Also, why detach in the middle of the forward function?
clf.fit(x.detach().cpu().numpy(),targets.detach().cpu().numpy())
preds = clf.predict(x.detach().cpu().numpy())

You know that the back-prop cannot continue from these two lines upward, right?