mirror of
https://github.com/wassname/pytorch-lightning.git
synced 2026-08-22 12:20:50 +08:00
* add more underline * fix LightningMudule import error * remove unneeded blank line * escape asterisk to fix inline emphasis warning * add PULL_REQUEST_TEMPLATE.md * add __init__.py and import imagenet_example * fix duplicate label * add noindex option to fix duplicate object warnings * remove unexpected indent * refer explicit LightningModule * fix minor bug * refer EarlyStopping explicitly * restore exclude patterns * change the way how to refer class * remove unused import * update badges & drop Travis/Appveyor (#826) * drop Travis * drop Appveyor * update badges * fix missing PyPI images & CI badges (#853) * docs - anchor links (#848) * docs - add links * add desc. * add Greeting action (#843) * add Greeting action * Update greetings.yml Co-authored-by: William Falcon <waf2107@columbia.edu> * add pep8speaks (#842) * advanced profiler describe + cleaned up tests (#837) * add py36 compatibility * add test case to capture previous bug * clean up tests * clean up tests * Update lightning_module_template.py * Update lightning.py * respond lint issues * break long line * break more lines * checkout conflicting files from master * shorten url * checkout from upstream/master * remove trailing whitespaces * remove unused import LightningModule * fix sphinx bot warnings * Apply suggestions from code review just to trigger CI * Update .github/workflows/greetings.yml Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com> Co-authored-by: William Falcon <waf2107@columbia.edu> Co-authored-by: Jeremy Jordan <13970565+jeremyjordan@users.noreply.github.com>
80 lines
2.5 KiB
ReStructuredText
80 lines
2.5 KiB
ReStructuredText
Checkpointing
|
|
==============
|
|
|
|
.. _model-saving:
|
|
|
|
Model saving
|
|
-------------------
|
|
To save a LightningModule, provide a :meth:`pytorch_lightning.callbacks.ModelCheckpoint` callback.
|
|
|
|
The Lightning checkpoint also saves the hparams (hyperparams) passed into the LightningModule init.
|
|
|
|
.. note:: hparams is a `Namespace <https://docs.python.org/2/library/argparse.html#argparse.Namespace>`_ or dictionary.
|
|
|
|
.. code-block:: python
|
|
:emphasize-lines: 8
|
|
|
|
from argparse import Namespace
|
|
|
|
# usually these come from command line args
|
|
args = Namespace(**{'learning_rate':0.001})
|
|
|
|
# define you module to have hparams as the first arg
|
|
# this means your checkpoint will have everything that went into making
|
|
# this model (in this case, learning rate)
|
|
class MyLightningModule(pl.LightningModule):
|
|
|
|
def __init__(self, hparams, ...):
|
|
self.hparams = hparams
|
|
|
|
my_model = MyLightningModule(args)
|
|
|
|
# auto-saves checkpoint
|
|
checkpoint_callback = ModelCheckpoint(filepath='my_path')
|
|
Trainer(checkpoint_callback=checkpoint_callback)
|
|
|
|
|
|
Model loading
|
|
-----------------------------------
|
|
|
|
To load a model, use :meth:`pytorch_lightning.core.LightningModule.load_from_checkpoint`
|
|
|
|
.. note:: If lightning created your checkpoint, your model will receive all the hyperparameters used
|
|
to create the checkpoint. (See: :ref:`model-saving`).
|
|
|
|
.. code-block:: python
|
|
|
|
# load weights without mapping
|
|
MyLightningModule.load_from_checkpoint('path/to/checkpoint.ckpt')
|
|
|
|
# load weights mapping all weights from GPU 1 to GPU 0
|
|
map_location = {'cuda:1':'cuda:0'}
|
|
MyLightningModule.load_from_checkpoint('path/to/checkpoint.ckpt', map_location=map_location)
|
|
|
|
Restoring training session
|
|
-----------------------------------
|
|
|
|
If you want to pick up training from where you left off, you have a few options.
|
|
|
|
1. Pass in a logger with the same experiment version to continue training.
|
|
|
|
.. code-block:: python
|
|
|
|
# train the first time and set the version number
|
|
logger = TensorboardLogger(version=10)
|
|
trainer = Trainer(logger=logger)
|
|
trainer.fit(model)
|
|
|
|
# when you init another logger with that same version, the model
|
|
# will continue where it left off
|
|
logger = TensorboardLogger(version=10)
|
|
trainer = Trainer(logger=logger)
|
|
trainer.fit(model)
|
|
|
|
2. A second option is to pass in a path to a checkpoint (see: :ref:`pytorch_lightning.trainer.trainer.Trainer`).
|
|
|
|
.. code-block:: python
|
|
|
|
# train the first time and set the version number
|
|
trainer = Trainer(resume_from_checkpoint='some/path/to/my_checkpoint.ckpt')
|
|
trainer.fit(model) |