If you call backward() on each loss separately you will  for sure encounter this error. The easier way would be to sum up the losses. Both approaches are equivalent since the gradient of multiple backward calls will be accumulated.