During and after training we need a way to evaluate our models to make sure they are not overfitting while training and
generalize well on unseen or real-world data. There are generally 2 stages of evaluation: validation and testing. To some
degree they serve the same purpose, to make sure models works on real data but they have some practical differences.
Validation is usually done during training, traditionally after each training epoch. It can be used for hyperparameter optimization or tracking model performance during training.
It's a part of the training process.
Testing is usually done once we are satisfied with the training and only with the best model selected from the validation metrics.
Let's see how these can be performed with Lightning.
*******
Testing
*******
Lightning allows the user to test their models with any compatible test dataloaders. This can be done before/after training
and is completely agnostic to :meth:`~pytorch_lightning.trainer.trainer.Trainer.fit` call. The logic used here is defined under
In this case, the options you pass to trainer will be used when
running the test set (ie: 16-bit, dp, ddp, etc...)
Test with Additional DataLoaders
================================
You can still run inference on a test dataset even if the :meth:`~pytorch_lightning.core.hooks.DataHooks.test_dataloader` method hasn't been
defined within your :doc:`lightning module <../common/lightning_module>` instance. This would be the case when your test data
is not available at the time your model was declared.
..code-block:: python
# setup your data loader
test_dataloader = DataLoader(...)
# test (pass in the loader)
trainer.test(dataloaders=test_dataloader)
You can either pass in a single dataloader or a list of them. This optional named
parameter can be used in conjunction with any of the above use cases. Additionally,
you can also pass in an :doc:`datamodules <../data/datamodule>` that have overridden the
:ref:`datamodule_test_dataloader_label` method.
..code-block:: python
class MyDataModule(pl.LightningDataModule):
...
def test_dataloader(self):
return DataLoader(...)
# setup your datamodule
dm = MyDataModule(...)
# test (pass in datamodule)
trainer.test(datamodule=dm)
----------
**********
Validation
**********
Lightning allows the user to validate their models with any compatible ``val dataloaders``. This can be done before/after training.
The logic associated to the validation is defined within the :meth:`~pytorch_lightning.core.lightning.LightningModule.validation_step`.
Apart from this ``.validate`` has same API as ``.test``, but would rely respectively on :meth:`~pytorch_lightning.core.lightning.LightningModule.validation_step` and :meth:`~pytorch_lightning.core.lightning.LightningModule.test_step`.
..note::
``.validate`` method uses the same validation logic being used under validation happening within