mirror of
https://github.com/wassname/pytorch-lightning.git
synced 2026-09-10 12:21:57 +08:00
made load from checkpoint flexible (#1307)
* made load from checkpoint flexible * made load from checkpoint flexible * made load from checkpoint flexible
This commit is contained in:
@@ -84,7 +84,7 @@ To save your own checkpoint call:
|
||||
Checkpoint Loading
|
||||
------------------
|
||||
|
||||
To load a model along with its weights, biases and hyperparameters use following method:
|
||||
To load a model along with its weights, biases and hyperparameters use following method.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@@ -92,9 +92,42 @@ To load a model along with its weights, biases and hyperparameters use following
|
||||
model.eval()
|
||||
y_hat = model(x)
|
||||
|
||||
A LightningModule is no different than a nn.Module. This means you can load it and use it for
|
||||
predictions as you would a nn.Module.
|
||||
The above only works if you used `hparams` in your model definition
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
class MyModel(pl.LightningModule):
|
||||
|
||||
def __init__(self, hparams):
|
||||
self.hparams = hparams
|
||||
self.l1 = nn.Linear(hparams.in_dim, hparams.out_dim)
|
||||
|
||||
But if you don't and instead pass individual parameters
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
class MyModel(pl.LightningModule):
|
||||
|
||||
def __init__(self, in_dim, out_dim):
|
||||
self.l1 = nn.Linear(in_dim, out_dim)
|
||||
|
||||
you can restore the model like this
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
model = MyModel.load_from_checkpoint(PATH, in_dim=128, out_dim=10)
|
||||
|
||||
|
||||
.. note:: To restore the trainer state as well use
|
||||
:meth:`pytorch_lightning.trainer.trainer.Trainer.resume_from_checkpoint`.
|
||||
Restoring Training State
|
||||
------------------------
|
||||
|
||||
If you don't just want to load weights, but instead restore the full training,
|
||||
do the following:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
model = LitModel()
|
||||
trainer = Trainer(resume_from_checkpoint='some/path/to/my_checkpoint.ckpt')
|
||||
|
||||
# automatically restores model, epoch, step, LR schedulers, apex, etc...
|
||||
trainer.fit(model)
|
||||
|
||||
Reference in New Issue
Block a user