Lightning-AI / Lightning-AI/pytorch-lightning
Enable Hyperparameter logging from any hook in the LightningModule
@cemde is already working on this.
Since Jul 5, 2022.
- Dominant language
- Python
- Stars
- 31.4k
- Forks
- 3.8k
- Avg merge
- 6d 7h
- Merged PRs (30d)
- 6
Description
## 🚀 Feature
Make it possible to call `save_hyperparameters` from any hook in the `LightningModule`.
### Motivation
Sometimes the dataset has hyperparameters that should be logged. However, the `LightningDataModule` is only accessible from the `LightningModule` once the trainer is initiated. Thus, it would be useful to call `save_hyperparameters` from `on_fit_start`, when the `Trainer` is specified and the hyperparameters from the dataset can easily be collected, e.g. through `self.trainer.datamodule.build_hparams()`.
### Pitch
`log_hyperparameters` shouldn't look for `init` args in the local variables when called outside the `__init__` method.
Currently, this behaviour casues an exception in line 154 in `utilities/parsing.py`
```
local_args = {k: local_vars[k] for k in init_parameters.keys()}
```
because the function is looking for the init parameters in the local variables, which are only available when called from `__init__`.
Suggestion: Remove init parameter logging when called from other places.
### Alternatives
Save init parameters and add them later.
### Additional context
N/A
cc @borda @carmocca @justusschock @awaelchli @ananthsub @ninginthecloud @jjenniferdai @rohitgr7
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.