Fix Mixing hparams and arguments in LightningModule (#1505)

* Attempt to fix #1468

* Remove the if statement, it doesn't actually make any difference

* Update docs

* Correct warnings I caused in the last commit

* Add to changelog

* Actually add to changelog

* Clarify documentation and examples

* Update CHANGELOG.md

Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
This commit is contained in:
Hengjian (Henry) Jia
2020-04-19 07:03:40 -04:00
committed by GitHub
co-authored by Jirka Borovec
parent e02146943d
commit 3c6f856f23
2 changed files with 5 additions and 5 deletions
+2
View File
@@ -39,6 +39,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed loggers - flushing last logged metrics even before continue, e.g. `trainer.test()` results ([#1459](https://github.com/PyTorchLightning/pytorch-lightning/pull/1459))
- Fixed LightningModule - Mixing hparams and arguments in `LightningModule.__init__()` crashes load_from_checkpoint() ([#1505](https://github.com/PyTorchLightning/pytorch-lightning/pull/1505))
- Added a missing call to the `on_before_zero_grad` model hook ([#1493](https://github.com/PyTorchLightning/pytorch-lightning/pull/1493)).
-
+3 -5
View File
@@ -1434,6 +1434,7 @@ class LightningModule(ABC, GradInformation, ModelIO, ModelHooks):
it stores the hyperparameters in the checkpoint if you initialized your :class:`LightningModule`
with an argument called ``hparams`` which is a :class:`~argparse.Namespace`
(output of :meth:`~argparse.ArgumentParser.parse_args` when parsing command line arguments).
Any other arguments specified through \*args and \*\*kwargs will be passed to the model.
Example:
.. code-block:: python
@@ -1493,7 +1494,7 @@ class LightningModule(ABC, GradInformation, ModelIO, ModelHooks):
# or load passing whatever args the model takes to load
MyLightningModule.load_from_checkpoint(
'path/to/checkpoint.ckpt',
learning_rate=0.1,
learning_rate=0.1, # These arguments will be passed to the model using **kwargs
layers=2,
pretrained_model=some_model
)
@@ -1544,10 +1545,7 @@ class LightningModule(ABC, GradInformation, ModelIO, ModelHooks):
# load the state_dict on the model automatically
model_args = [hparams] if hparams else []
if len(model_args) > 0:
model = cls(*model_args)
else:
model = cls(*args, **kwargs)
model = cls(*model_args, *args, **kwargs)
model.load_state_dict(checkpoint['state_dict'])
# give model a chance to load something