Skip to content

Enable Hyperparameter logging from any hook in the LightningModule #12624

@cemde

Description

@cemde

🚀 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

Metadata

Metadata

Assignees

Labels

featureIs an improvement or enhancementlightningmodulepl.LightningModule

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions