Skip to content

Commit

Permalink
Merge pull request #221 from edbeeching/Ivan-267-patch-1
Browse files Browse the repository at this point in the history
Improve exception handling in SB3 example script
  • Loading branch information
Ivan-267 authored Jan 22, 2025
2 parents f439635 + 3e78fcd commit f4b5392
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions examples/stable_baselines3_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ def close_env():
print("Exception while closing env: ", e)


def cleanup():
handle_onnx_export()
handle_model_save()
close_env()


path_checkpoint = os.path.join(args.experiment_dir, args.experiment_name + "_checkpoints")
abs_path_checkpoint = os.path.abspath(path_checkpoint)

Expand Down Expand Up @@ -213,12 +219,10 @@ def func(progress_remaining: float) -> float:
learn_arguments["callback"] = checkpoint_callback
try:
model.learn(**learn_arguments)
except KeyboardInterrupt:
except (KeyboardInterrupt, ConnectionError, ConnectionResetError):
print(
"""Training interrupted by user. Will save if --save_model_path was
"""Training interrupted by user or a ConnectionError. Will save if --save_model_path was
used and/or export if --onnx_export_path was used."""
)

close_env()
handle_onnx_export()
handle_model_save()
finally:
cleanup()

0 comments on commit f4b5392

Please sign in to comment.