-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Hyperparameters for datamodule #3792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kaushikb11
merged 55 commits into
Lightning-AI:master
from
tilman151:hyperparameters_for_datamodule
Jul 9, 2021
Merged
Changes from all commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
fdd3651
Extract hparam functions to mixin.
9629b32
Make LightningDataModule inherit from HyperparametersMixin.
2771894
Add function to extend hparams.
ea744ae
Add hparams of DataModule to model before training.
3ea5f32
Change examples due to cyclic import.
5eb202e
Merge branch 'master' into hyperparameters_for_datamodule
tilman151 0eaf76a
Merge branch 'master' into hyperparameters_for_datamodule
9ea7989
Add initital_hparams to mixin and move/rename extend_hparams.
f3ea974
Update unit tests.
fc8af41
Add hparams from datamodule to hparams_inital, too.
3f8d44f
Test if datamodule hparams are logged to trainer loggers, too.
822cce4
Simplify error handling.
0f4dc64
Change args of add_datamodule_hparams from hparams to datamodule itself.
0af9968
Add hparams of datamodule only if it has some.
0369c9f
Add one more unit test.
6dde1d2
Fix pep8 complaint.
374932e
Merge branch 'master' into hyperparameters_for_datamodule
tilman151 17ca42c
Merge branch 'master' into hyperparameters_for_datamodule
tilman151 4d8dc2a
Merge branch 'master' into hyperparameters_for_datamodule
d050be0
Merge remote-tracking branch 'origin/hyperparameters_for_datamodule' …
001f754
Merge branch 'master' into hyperparameters_for_datamodule
tilman151 64acd07
Merge branch 'master' into hyperparameters_for_datamodule
tilman151 958b6e5
Merge branch 'master' into hyperparameters_for_datamodule
873b02a
Make training work for SaveHparamsModel.
2268d6e
Merge branch 'master' into hyperparameters_for_datamodule
tilman151 548e976
Merge branch 'master' into hyperparameters_for_datamodule
rohitgr7 b0e1f04
Merge branch 'master' into hyperparameters_for_datamodule
tilman151 ebb53fb
Update pytorch_lightning/core/lightning.py
tilman151 c36ef72
Update pytorch_lightning/utilities/hparams_mixin.py
tilman151 5e2d8d5
Merge branch 'master' into hyperparameters_for_datamodule
eaaf94e
Fix merge conflicts.
806a1e5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6a80679
Fix code style issues.
3481039
Merge remote-tracking branch 'origin/hyperparameters_for_datamodule' …
2228e69
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] dbca3e4
Merge branch 'master' into hyperparameters_for_datamodule
c14361e
Fix indentation error from merge.
a26bd9e
Merge branch 'master' into hyperparameters_for_datamodule
5371b2c
Extract hparam merging function.
eebc6ab
Hold model and data hparams separately and merge on logging.
7faed29
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1ade79f
Merge branch 'master' into hyperparameters_for_datamodule
ethanwharris 8f27cb0
Fixes
ethanwharris 05fad8f
Merge branch 'master' into hyperparameters_for_datamodule
kaushikb11 d12d3c1
Update hparams mixin
kaushikb11 d9c74d9
Update trainer & Lightning module
kaushikb11 d2f477a
Fix torchscript issue
kaushikb11 2ba18c9
Update test
kaushikb11 55a15c0
Update tests
kaushikb11 0d05086
Update datamodule test
kaushikb11 a08304e
Remove merge_hparams & update tests
kaushikb11 d517fd3
Update changelog
kaushikb11 6297bd6
Remove hparams setter
kaushikb11 43c75fe
Update pytorch_lightning/utilities/hparams_mixin.py
kaushikb11 d3d5cf7
Merge branch 'master' into hyperparameters_for_datamodule
kaushikb11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| # Copyright The PyTorch Lightning team. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import copy | ||
| import inspect | ||
| import types | ||
| from argparse import Namespace | ||
| from typing import Optional, Sequence, Union | ||
|
|
||
| from pytorch_lightning.core.saving import ALLOWED_CONFIG_TYPES, PRIMITIVE_TYPES | ||
| from pytorch_lightning.utilities import AttributeDict | ||
| from pytorch_lightning.utilities.parsing import save_hyperparameters | ||
|
|
||
|
|
||
| class HyperparametersMixin: | ||
tilman151 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| __jit_unused_properties__ = ["hparams", "hparams_initial"] | ||
|
|
||
| def save_hyperparameters( | ||
| self, | ||
| *args, | ||
| ignore: Optional[Union[Sequence[str], str]] = None, | ||
| frame: Optional[types.FrameType] = None | ||
| ) -> None: | ||
| """Save arguments to ``hparams`` attribute. | ||
|
|
||
| Args: | ||
| args: single object of `dict`, `NameSpace` or `OmegaConf` | ||
| or string names or arguments from class ``__init__`` | ||
| ignore: an argument name or a list of argument names from | ||
| class ``__init__`` to be ignored | ||
| frame: a frame object. Default is None | ||
|
|
||
| Example:: | ||
| >>> 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 | ||
|
|
||
| >>> 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 | ||
|
|
||
| >>> 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 | ||
|
|
||
| >>> 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 | ||
| """ | ||
| # the frame needs to be created in this file. | ||
| if not frame: | ||
| frame = inspect.currentframe().f_back | ||
| save_hyperparameters(self, *args, ignore=ignore, frame=frame) | ||
|
|
||
| def _set_hparams(self, hp: Union[dict, Namespace, str]) -> None: | ||
| hp = self._to_hparams_dict(hp) | ||
|
|
||
| if isinstance(hp, dict) and isinstance(self.hparams, dict): | ||
| self.hparams.update(hp) | ||
| else: | ||
| self._hparams = hp | ||
|
|
||
| @staticmethod | ||
| def _to_hparams_dict(hp: Union[dict, Namespace, str]): | ||
| if isinstance(hp, Namespace): | ||
| hp = vars(hp) | ||
| if isinstance(hp, dict): | ||
| hp = AttributeDict(hp) | ||
| elif isinstance(hp, PRIMITIVE_TYPES): | ||
| raise ValueError(f"Primitives {PRIMITIVE_TYPES} are not allowed.") | ||
| elif not isinstance(hp, ALLOWED_CONFIG_TYPES): | ||
| raise ValueError(f"Unsupported config type of {type(hp)}.") | ||
| return hp | ||
|
|
||
| @property | ||
| def hparams(self) -> Union[AttributeDict, dict, Namespace]: | ||
| if not hasattr(self, "_hparams"): | ||
| self._hparams = AttributeDict() | ||
| return self._hparams | ||
|
|
||
| @property | ||
| def hparams_initial(self) -> AttributeDict: | ||
| if not hasattr(self, "_hparams_initial"): | ||
| return AttributeDict() | ||
| # prevent any change | ||
| return copy.deepcopy(self._hparams_initial) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.