2020-05-28 02:44:46 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from pytorch_lightning import Trainer
|
2020-09-03 18:17:15 +00:00
|
|
|
from pytorch_lightning.callbacks import LearningRateMonitor
|
|
|
|
from pytorch_lightning.utilities.exceptions import MisconfigurationException
|
2020-05-28 02:44:46 +00:00
|
|
|
from tests.base import EvalModelTemplate
|
2020-09-03 18:17:15 +00:00
|
|
|
import tests.base.develop_utils as tutils
|
2020-05-28 02:44:46 +00:00
|
|
|
|
|
|
|
|
2020-09-03 18:17:15 +00:00
|
|
|
def test_lr_monitor_single_lr(tmpdir):
|
2020-05-28 02:44:46 +00:00
|
|
|
""" Test that learning rates are extracted and logged for single lr scheduler. """
|
|
|
|
tutils.reset_seed()
|
|
|
|
|
|
|
|
model = EvalModelTemplate()
|
|
|
|
model.configure_optimizers = model.configure_optimizers__single_scheduler
|
|
|
|
|
2020-09-03 18:17:15 +00:00
|
|
|
lr_monitor = LearningRateMonitor()
|
2020-05-28 02:44:46 +00:00
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
2020-06-01 15:00:32 +00:00
|
|
|
max_epochs=2,
|
2020-06-17 12:03:28 +00:00
|
|
|
limit_val_batches=0.1,
|
2020-06-17 17:42:28 +00:00
|
|
|
limit_train_batches=0.5,
|
2020-09-03 18:17:15 +00:00
|
|
|
callbacks=[lr_monitor],
|
2020-05-28 02:44:46 +00:00
|
|
|
)
|
|
|
|
result = trainer.fit(model)
|
|
|
|
assert result
|
|
|
|
|
2020-09-03 18:17:15 +00:00
|
|
|
assert lr_monitor.lrs, 'No learning rates logged'
|
|
|
|
assert len(lr_monitor.lrs) == len(trainer.lr_schedulers), \
|
2020-05-28 02:44:46 +00:00
|
|
|
'Number of learning rates logged does not match number of lr schedulers'
|
2020-09-03 18:17:15 +00:00
|
|
|
assert all([k in ['lr-Adam'] for k in lr_monitor.lrs.keys()]), \
|
2020-05-28 02:44:46 +00:00
|
|
|
'Names of learning rates not set correctly'
|
|
|
|
|
|
|
|
|
2020-09-03 18:17:15 +00:00
|
|
|
def test_lr_monitor_no_lr_scheduler(tmpdir):
|
2020-05-28 02:44:46 +00:00
|
|
|
tutils.reset_seed()
|
|
|
|
|
|
|
|
model = EvalModelTemplate()
|
|
|
|
|
2020-09-03 18:17:15 +00:00
|
|
|
lr_monitor = LearningRateMonitor()
|
2020-05-28 02:44:46 +00:00
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
2020-06-01 15:00:32 +00:00
|
|
|
max_epochs=2,
|
2020-06-17 12:03:28 +00:00
|
|
|
limit_val_batches=0.1,
|
2020-06-17 17:42:28 +00:00
|
|
|
limit_train_batches=0.5,
|
2020-09-03 18:17:15 +00:00
|
|
|
callbacks=[lr_monitor],
|
2020-05-28 02:44:46 +00:00
|
|
|
)
|
|
|
|
|
2020-09-03 18:17:15 +00:00
|
|
|
with pytest.warns(RuntimeWarning, match='have no learning rate schedulers'):
|
2020-05-28 02:44:46 +00:00
|
|
|
result = trainer.fit(model)
|
|
|
|
assert result
|
|
|
|
|
|
|
|
|
2020-09-03 18:17:15 +00:00
|
|
|
def test_lr_monitor_no_logger(tmpdir):
|
|
|
|
tutils.reset_seed()
|
|
|
|
|
|
|
|
model = EvalModelTemplate()
|
|
|
|
|
|
|
|
lr_monitor = LearningRateMonitor()
|
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
max_epochs=1,
|
|
|
|
callbacks=[lr_monitor],
|
|
|
|
logger=False
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(MisconfigurationException, match='Trainer that has no logger'):
|
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
|
2020-08-09 16:30:43 +00:00
|
|
|
@pytest.mark.parametrize("logging_interval", ['step', 'epoch'])
|
2020-09-03 18:17:15 +00:00
|
|
|
def test_lr_monitor_multi_lrs(tmpdir, logging_interval):
|
2020-05-28 02:44:46 +00:00
|
|
|
""" Test that learning rates are extracted and logged for multi lr schedulers. """
|
|
|
|
tutils.reset_seed()
|
|
|
|
|
|
|
|
model = EvalModelTemplate()
|
|
|
|
model.configure_optimizers = model.configure_optimizers__multiple_schedulers
|
|
|
|
|
2020-09-03 18:17:15 +00:00
|
|
|
lr_monitor = LearningRateMonitor(logging_interval=logging_interval)
|
2020-05-28 02:44:46 +00:00
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
2020-06-01 15:00:32 +00:00
|
|
|
max_epochs=2,
|
2020-06-17 12:03:28 +00:00
|
|
|
limit_val_batches=0.1,
|
2020-06-17 17:42:28 +00:00
|
|
|
limit_train_batches=0.5,
|
2020-09-03 18:17:15 +00:00
|
|
|
callbacks=[lr_monitor],
|
2020-05-28 02:44:46 +00:00
|
|
|
)
|
|
|
|
result = trainer.fit(model)
|
|
|
|
assert result
|
|
|
|
|
2020-09-03 18:17:15 +00:00
|
|
|
assert lr_monitor.lrs, 'No learning rates logged'
|
|
|
|
assert len(lr_monitor.lrs) == len(trainer.lr_schedulers), \
|
2020-05-28 02:44:46 +00:00
|
|
|
'Number of learning rates logged does not match number of lr schedulers'
|
2020-09-03 18:17:15 +00:00
|
|
|
assert all([k in ['lr-Adam', 'lr-Adam-1'] for k in lr_monitor.lrs.keys()]), \
|
2020-05-28 02:44:46 +00:00
|
|
|
'Names of learning rates not set correctly'
|
2020-08-09 16:30:43 +00:00
|
|
|
|
|
|
|
if logging_interval == 'step':
|
|
|
|
expected_number_logged = trainer.global_step
|
|
|
|
if logging_interval == 'epoch':
|
|
|
|
expected_number_logged = trainer.max_epochs
|
|
|
|
|
2020-09-03 18:17:15 +00:00
|
|
|
assert all(len(lr) == expected_number_logged for lr in lr_monitor.lrs.values()), \
|
2020-08-09 16:30:43 +00:00
|
|
|
'Length of logged learning rates do not match the expected number'
|
2020-05-28 02:44:46 +00:00
|
|
|
|
|
|
|
|
2020-09-03 18:17:15 +00:00
|
|
|
def test_lr_monitor_param_groups(tmpdir):
|
2020-05-28 02:44:46 +00:00
|
|
|
""" Test that learning rates are extracted and logged for single lr scheduler. """
|
|
|
|
tutils.reset_seed()
|
|
|
|
|
|
|
|
model = EvalModelTemplate()
|
|
|
|
model.configure_optimizers = model.configure_optimizers__param_groups
|
|
|
|
|
2020-09-03 18:17:15 +00:00
|
|
|
lr_monitor = LearningRateMonitor()
|
2020-05-28 02:44:46 +00:00
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
2020-06-01 15:00:32 +00:00
|
|
|
max_epochs=2,
|
2020-06-17 12:03:28 +00:00
|
|
|
limit_val_batches=0.1,
|
2020-06-17 17:42:28 +00:00
|
|
|
limit_train_batches=0.5,
|
2020-09-03 18:17:15 +00:00
|
|
|
callbacks=[lr_monitor],
|
2020-05-28 02:44:46 +00:00
|
|
|
)
|
|
|
|
result = trainer.fit(model)
|
|
|
|
assert result
|
|
|
|
|
2020-09-03 18:17:15 +00:00
|
|
|
assert lr_monitor.lrs, 'No learning rates logged'
|
|
|
|
assert len(lr_monitor.lrs) == 2 * len(trainer.lr_schedulers), \
|
2020-05-28 02:44:46 +00:00
|
|
|
'Number of learning rates logged does not match number of param groups'
|
2020-09-03 18:17:15 +00:00
|
|
|
assert all([k in ['lr-Adam/pg1', 'lr-Adam/pg2'] for k in lr_monitor.lrs.keys()]), \
|
2020-05-28 02:44:46 +00:00
|
|
|
'Names of learning rates not set correctly'
|