Also you don’t want to train the targetnetwork.
So you have to put the line  "with torch.no_grads():" in

[“with torch.no_grads():”]
if sample.next_state is not None:
opt_pred[sample.action] += t.max(self.target_net.predict(sample.next_state)) * self.gamma