Skip to content

.save_hyperparameters() from a parent model doesn't save arguments of the child model #9701

@eladsegal

Description

@eladsegal

🐛 Bug

Calling .save_hyperparameters() from a parent model doesn't save arguments of the child model.

To Reproduce

This is a very simple example that has two models, each inherits from a parent.
In the first one, the parent calls .save_hyperparameters() and in the second one the child does.
You can see that difference in .hparams in each of the models.
https://colab.research.google.com/drive/1J4Z8AFanZUIPMffj0ZO0yiKtlDto1OQw?usp=sharing

Expected behavior

It shouldn't matter where .save_hyperparameters() is called in the inheritance chain, all the parameters passed to the instantiated model should be saved in .hparams.
I believe the solution is already given with collect_init_args:

        init_args = {}
        for local_args in collect_init_args(frame, []):
            init_args.update(local_args)

instead of
https://github.com/PyTorchLightning/pytorch-lightning/blob/a3def9d228b4c849e8a6222cffd22e547a17df64/pytorch_lightning/utilities/parsing.py#L220

It worked for me, so let me know if you'd like me to submit a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedOpen to be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions