AI4OPT / ML4OPF

Machine Learning for Optimal Power Flow
MIT License
5 stars 4 forks source link

`BasicNeuralNet.load_from_checkpoint` assumes `log_dir` is the parent of `path` #7

Closed klamike closed 3 months ago

klamike commented 4 months ago

We need the slices from hparams.yaml to initialize the model, but are actually relying on PyTorch Lightning's save_hyperparameters to save them to the directory above the checkpoint. This is the case when saving the checkpoint in the logger directory. However if there is no logger, or the checkpoint is saved elsewhere, the slices separated from the checkpoint.

https://github.com/AI4OPT/ML4OPF/blob/0dd79a5b7ba7e1826bd70d110969c0a986455b93/ml4opf/models/basic_nn/basic_nn.py#L164-L171

Workaround is to log and checkpoint in the same directory, like tests/test_models.py: https://github.com/AI4OPT/ML4OPF/blob/0dd79a5b7ba7e1826bd70d110969c0a986455b93/tests/test_models.py#L92-L98

That is, save checkpoints to model.trainer.logger.log_dir / <directory>.

Temporary fix can save the path to the logdir in config.json. Better fix would be to (re-)save the yaml file in the checkpoint directory.