HyperparametersMixin — PyTorch Lightning 2.5.1.post0 documentation (original) (raw)

class lightning.pytorch.core.mixins.HyperparametersMixin[source]

Bases: object

save_hyperparameters(*args, ignore=None, frame=None, logger=True)[source]

Save arguments to hparams attribute.

Parameters:

Return type:

None

Example::

from lightning.pytorch.core.mixins import HyperparametersMixin class ManuallyArgsModel(HyperparametersMixin): ... def init(self, arg1, arg2, arg3): ... super().init() ... # manually assign arguments ... self.save_hyperparameters('arg1', 'arg3') ... def forward(self, *args, **kwargs): ... ... model = ManuallyArgsModel(1, 'abc', 3.14) model.hparams "arg1": 1 "arg3": 3.14

from lightning.pytorch.core.mixins import HyperparametersMixin class AutomaticArgsModel(HyperparametersMixin): ... def init(self, arg1, arg2, arg3): ... super().init() ... # equivalent automatic ... self.save_hyperparameters() ... def forward(self, *args, **kwargs): ... ... model = AutomaticArgsModel(1, 'abc', 3.14) model.hparams "arg1": 1 "arg2": abc "arg3": 3.14

from lightning.pytorch.core.mixins import HyperparametersMixin class SingleArgModel(HyperparametersMixin): ... def init(self, params): ... super().init() ... # manually assign single argument ... self.save_hyperparameters(params) ... def forward(self, *args, **kwargs): ... ... model = SingleArgModel(Namespace(p1=1, p2='abc', p3=3.14)) model.hparams "p1": 1 "p2": abc "p3": 3.14

from lightning.pytorch.core.mixins import HyperparametersMixin class ManuallyArgsModel(HyperparametersMixin): ... def init(self, arg1, arg2, arg3): ... super().init() ... # pass argument(s) to ignore as a string or in a list ... self.save_hyperparameters(ignore='arg2') ... def forward(self, *args, **kwargs): ... ... model = ManuallyArgsModel(1, 'abc', 3.14) model.hparams "arg1": 1 "arg3": 3.14

property hparams_: Union[AttributeDict, MutableMapping]_

The collection of hyperparameters saved with save_hyperparameters(). It is mutable by the user. For the frozen set of initial hyperparameters, use hparams_initial.

Returns:

Mutable hyperparameters dictionary

property hparams_initial_: AttributeDict_

The collection of hyperparameters saved with save_hyperparameters(). These contents are read-only. Manual updates to the saved hyperparameters can instead be performed through hparams.

Returns:

immutable initial hyperparameters

Return type:

AttributeDict