Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix hparams assign in init #4189

Merged
merged 1 commit into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion pytorch_lightning/core/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ def hparams(self) -> Union[AttributeDict, dict, Namespace]:
@property
def hparams_initial(self) -> AttributeDict:
if not hasattr(self, "_hparams_initial"):
self._hparams_initial = AttributeDict()
return AttributeDict()
# prevent any change
return copy.deepcopy(self._hparams_initial)

Expand All @@ -1617,6 +1617,9 @@ def hparams(self, hp: Union[dict, Namespace, Any]):
hparams_assignment_name = self.__get_hparams_assignment_variable()
self._hparams_name = hparams_assignment_name
self._set_hparams(hp)
# this resolves case when user does not uses `save_hyperparameters` and do hard assignement in init
if not hasattr(self, "_hparams_initial"):
self._hparams_initial = copy.deepcopy(self._hparams)

def __get_hparams_assignment_variable(self):
""""""
Expand Down
18 changes: 14 additions & 4 deletions tests/models/test_hparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,17 +556,27 @@ def test_args(tmpdir):
SubClassVarArgs.load_from_checkpoint(raw_checkpoint_path)


class RuntimeParamChangeModel(BoringModel):
def __init__(self, running_arg):
class RuntimeParamChangeModelSaving(BoringModel):
def __init__(self, **kwargs):
super().__init__()
self.save_hyperparameters()


def test_init_arg_with_runtime_change(tmpdir):
model = RuntimeParamChangeModel(123)
class RuntimeParamChangeModelAssign(BoringModel):
def __init__(self, **kwargs):
super().__init__()
self.hparams = kwargs


@pytest.mark.parametrize("cls", [RuntimeParamChangeModelSaving, RuntimeParamChangeModelAssign])
def test_init_arg_with_runtime_change(tmpdir, cls):
"""Test that we save/export only the initial hparams, no other runtime change allowed"""
model = cls(running_arg=123)
assert model.hparams.running_arg == 123
model.hparams.running_arg = -1
assert model.hparams.running_arg == -1
model.hparams = Namespace(abc=42)
assert model.hparams.abc == 42

trainer = Trainer(
default_root_dir=tmpdir,
Expand Down