Lightning-AI / Lightning-AI/pytorch-lightning

Enable Hyperparameter logging from any hook in the LightningModule

Open
#12,624 9 comments 0 reactions 1 assignee View on GitHub

@cemde is already working on this.

Since Jul 5, 2022.

feature lightningmodule
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.