Skip to content

Commit 1db545a

Browse files
Adrian Wälchliakarnachev
Adrian Wälchli
authored and
akarnachev
committed
Disable validation when val_percent_check=0 (Lightning-AI#1251)
* fix disable validation * add test * update changelog * update docs for val_percent_check * make "fast training" docs consistent
1 parent 004d7ec commit 1db545a

File tree

4 files changed

+69
-15
lines changed

4 files changed

+69
-15
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
2929

3030
### Fixed
3131

32-
3332
- `Trainer.add_argparse_args` classmethod fixed. Now it adds a type for the arguments ([#1147](https://github.com/PyTorchLightning/pytorch-lightning/pull/1147)).
3433
- Fixed bug related to type cheking of `ReduceLROnPlateau` lr schedulers([#1114](https://github.com/PyTorchLightning/pytorch-lightning/issues/1114))
3534
- Fixed a bug to ensure lightning checkpoints to be backward compatible ([#1132](https://github.com/PyTorchLightning/pytorch-lightning/pull/1132))
3635
- Fixed all warnings and errors in the docs build process ([#1191](https://github.com/PyTorchLightning/pytorch-lightning/pull/1191))
36+
- Fixed an issue where `val_percent_check=0` would not disable validation ([#1251](https://github.com/PyTorchLightning/pytorch-lightning/pull/1251))
3737

3838
## [0.7.1] - 2020-03-07
3939

docs/source/fast_training.rst

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Fast Training
2-
================
2+
=============
33
There are multiple options to speed up different parts of the training by choosing to train
44
on a subset of data. This could be done for speed or debugging purposes.
55

66
Check validation every n epochs
7-
-------------------------------------
7+
-------------------------------
88
If you have a small dataset you might want to check validation every n epochs
99

1010
.. code-block:: python
@@ -13,7 +13,7 @@ If you have a small dataset you might want to check validation every n epochs
1313
trainer = Trainer(check_val_every_n_epoch=1)
1414
1515
Force training for min or max epochs
16-
-------------------------------------
16+
------------------------------------
1717
It can be useful to force training for a minimum number of epochs or limit to a max number.
1818

1919
.. seealso::
@@ -26,7 +26,7 @@ It can be useful to force training for a minimum number of epochs or limit to a
2626
2727
2828
Set validation check frequency within 1 training epoch
29-
-------------------------------------------------------
29+
------------------------------------------------------
3030
For large datasets it's often desirable to check validation multiple times within a training loop.
3131
Pass in a float to check that often within 1 training epoch. Pass in an int k to check every k training batches.
3232
Must use an int if using an IterableDataset.
@@ -43,7 +43,7 @@ Must use an int if using an IterableDataset.
4343
trainer = Trainer(val_check_interval=100)
4444
4545
Use training data subset
46-
----------------------------------
46+
------------------------
4747
If you don't want to check 100% of the training set (for debugging or if it's huge), set this flag.
4848

4949
.. code-block:: python
@@ -54,12 +54,11 @@ If you don't want to check 100% of the training set (for debugging or if it's hu
5454
# check 10% only
5555
trainer = Trainer(train_percent_check=0.1)
5656
57-
.. note:: train_percent_check will be overwritten by overfit_pct if overfit_pct > 0
57+
.. note:: ``train_percent_check`` will be overwritten by ``overfit_pct`` if ``overfit_pct`` > 0.
5858

5959
Use test data subset
60-
-------------------------------------
61-
If you don't want to check 100% of the test set (for debugging or if it's huge), set this flag
62-
test_percent_check will be overwritten by overfit_pct if overfit_pct > 0.
60+
--------------------
61+
If you don't want to check 100% of the test set (for debugging or if it's huge), set this flag.
6362

6463
.. code-block:: python
6564
@@ -69,15 +68,19 @@ test_percent_check will be overwritten by overfit_pct if overfit_pct > 0.
6968
# check 10% only
7069
trainer = Trainer(test_percent_check=0.1)
7170
71+
.. note:: ``test_percent_check`` will be overwritten by ``overfit_pct`` if ``overfit_pct`` > 0.
72+
7273
Use validation data subset
73-
--------------------------------------------
74-
If you don't want to check 100% of the validation set (for debugging or if it's huge), set this flag
75-
val_percent_check will be overwritten by overfit_pct if overfit_pct > 0
74+
--------------------------
75+
If you don't want to check 100% of the validation set (for debugging or if it's huge), set this flag.
7676

7777
.. code-block:: python
7878
7979
# DEFAULT
8080
trainer = Trainer(val_percent_check=1.0)
8181
8282
# check 10% only
83-
trainer = Trainer(val_percent_check=0.1)
83+
trainer = Trainer(val_percent_check=0.1)
84+
85+
.. note:: ``val_percent_check`` will be overwritten by ``overfit_pct`` if ``overfit_pct`` > 0 and ignored if
86+
``fast_dev_run=True``.

pytorch_lightning/trainer/trainer.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,8 @@ def run_pretrain_routine(self, model: LightningModule):
876876
return
877877

878878
# check if we should run validation during training
879-
self.disable_validation = not self.is_overriden('validation_step') and not self.fast_dev_run
879+
self.disable_validation = not (self.is_overriden('validation_step') and self.val_percent_check > 0) \
880+
and not self.fast_dev_run
880881

881882
# run tiny validation (if validation defined)
882883
# to make sure program won't crash during val

tests/models/test_cpu.py

+50
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
LightTrainDataloader,
1515
LightningTestModel,
1616
LightTestMixin,
17+
LightValidationMixin
1718
)
1819

1920

@@ -156,6 +157,55 @@ class CurrentTestModel(LightTrainDataloader, LightTestMixin, TestModelBase):
156157
tutils.assert_ok_model_acc(trainer)
157158

158159

160+
def test_disabled_validation():
161+
"""Verify that `val_percent_check=0` disables the validation loop unless `fast_dev_run=True`."""
162+
tutils.reset_seed()
163+
164+
class CurrentModel(LightTrainDataloader, LightValidationMixin, TestModelBase):
165+
166+
validation_step_invoked = False
167+
validation_end_invoked = False
168+
169+
def validation_step(self, *args, **kwargs):
170+
self.validation_step_invoked = True
171+
return super().validation_step(*args, **kwargs)
172+
173+
def validation_end(self, *args, **kwargs):
174+
self.validation_end_invoked = True
175+
return super().validation_end(*args, **kwargs)
176+
177+
hparams = tutils.get_default_hparams()
178+
model = CurrentModel(hparams)
179+
180+
trainer_options = dict(
181+
show_progress_bar=False,
182+
max_epochs=2,
183+
train_percent_check=0.4,
184+
val_percent_check=0.0,
185+
fast_dev_run=False,
186+
)
187+
188+
trainer = Trainer(**trainer_options)
189+
result = trainer.fit(model)
190+
191+
# check that val_percent_check=0 turns off validation
192+
assert result == 1, 'training failed to complete'
193+
assert trainer.current_epoch == 1
194+
assert not model.validation_step_invoked, '`validation_step` should not run when `val_percent_check=0`'
195+
assert not model.validation_end_invoked, '`validation_end` should not run when `val_percent_check=0`'
196+
197+
# check that val_percent_check has no influence when fast_dev_run is turned on
198+
model = CurrentModel(hparams)
199+
trainer_options.update(fast_dev_run=True)
200+
trainer = Trainer(**trainer_options)
201+
result = trainer.fit(model)
202+
203+
assert result == 1, 'training failed to complete'
204+
assert trainer.current_epoch == 0
205+
assert model.validation_step_invoked, 'did not run `validation_step` with `fast_dev_run=True`'
206+
assert model.validation_end_invoked, 'did not run `validation_end` with `fast_dev_run=True`'
207+
208+
159209
def test_single_gpu_batch_parse():
160210
tutils.reset_seed()
161211

0 commit comments

Comments
 (0)