GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — torchTextClassifiers/tests/test_scheduler_monitor.py at main · InseeFrLab/torchTextClassifiers Expand file tree/ Copy pathtest_scheduler_monitor.pyMore file actions 166 lines (143 loc) · 6.16 KB / Copy pathtest_scheduler_monitor.pyFile metadata and controls166 lines (143 loc) · 6.16 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 """ReduceLROnPlateau must monitor the same metric as early stopping / checkpointing.""" import logging import pytest import torch from torchTextClassifiers import ModelConfig, TrainingConfig, torchTextClassifiers from torchTextClassifiers.model import TextClassificationModule from torchTextClassifiers.tokenizers import NGramTokenizer class DummyClassificationModel(torch.nn.Module): def __init__(self, num_classes): super().__init__() self.num_classes = num_classes self.linear = torch.nn.Linear(2, num_classes) def forward(self, input_ids, attention_mask=None, categorical_vars=None, **kwargs): return self.linear(input_ids) def _build_module(scheduler, **kwargs): return TextClassificationModule( model=DummyClassificationModel(num_classes=2), loss=torch.nn.CrossEntropyLoss(), optimizer=torch.optim.Adam, optimizer_params={"lr": 1e-3}, scheduler=scheduler, scheduler_params={}, **kwargs, ) class TestConfigureOptimizers: def test_plateau_defaults_to_train_loss(self): _, [config] = _build_module( torch.optim.lr_scheduler.ReduceLROnPlateau ).configure_optimizers() assert config["monitor"] == "train_loss" def test_plateau_uses_given_monitor(self): module = _build_module( torch.optim.lr_scheduler.ReduceLROnPlateau, scheduler_monitor="val_loss" ) _, [config] = module.configure_optimizers() assert config["monitor"] == "val_loss" def test_non_plateau_scheduler_has_no_monitor(self): module = TextClassificationModule( model=DummyClassificationModel(num_classes=2), loss=torch.nn.CrossEntropyLoss(), optimizer=torch.optim.Adam, optimizer_params={"lr": 1e-3}, scheduler=torch.optim.lr_scheduler.StepLR, scheduler_params={"step_size": 1}, scheduler_monitor="val_loss", ) _, [scheduler] = module.configure_optimizers() assert isinstance(scheduler, torch.optim.lr_scheduler.StepLR) def _classifier(texts): tokenizer = NGramTokenizer( min_count=1, min_n=2, max_n=4, num_tokens=50, len_word_ngrams=2, output_dim=20 ) tokenizer.train(list(texts)) return torchTextClassifiers( tokenizer=tokenizer, model_config=ModelConfig(embedding_dim=8, num_classes=2) ) def _training_config(tmp_path, scheduler_patience=1, early_stopping_patience=3): return TrainingConfig( num_epochs=2, batch_size=4, lr=1e-3, num_workers=0, raw_labels=False, scheduler=torch.optim.lr_scheduler.ReduceLROnPlateau, scheduler_params={"patience": scheduler_patience}, patience_early_stopping=early_stopping_patience, trainer_params={"default_root_dir": str(tmp_path), "enable_progress_bar": False}, ) class TestTrainSchedulerMonitor: def test_monitors_val_loss_with_validation_set(self, tmp_path, sample_text_data, sample_labels): ttc = _classifier(sample_text_data) ttc.train( X_train=sample_text_data, y_train=sample_labels, X_val=sample_text_data, y_val=sample_labels, training_config=_training_config(tmp_path), ) assert ttc.lightning_module.scheduler_monitor == "val_loss" def test_falls_back_to_train_loss_without_validation_set( self, tmp_path, sample_text_data, sample_labels ): ttc = _classifier(sample_text_data) ttc.train( X_train=sample_text_data, y_train=sample_labels, training_config=_training_config(tmp_path), ) assert ttc.lightning_module.scheduler_monitor == "train_loss" def test_user_can_keep_train_loss_with_validation_set( self, tmp_path, sample_text_data, sample_labels ): ttc = _classifier(sample_text_data) config = _training_config(tmp_path) config.scheduler_monitor = "train_loss" ttc.train( X_train=sample_text_data, y_train=sample_labels, X_val=sample_text_data, y_val=sample_labels, training_config=config, ) assert ttc.lightning_module.scheduler_monitor == "train_loss" def test_val_monitor_without_validation_set_raises( self, tmp_path, sample_text_data, sample_labels ): ttc = _classifier(sample_text_data) config = _training_config(tmp_path) config.scheduler_monitor = "val_loss" with pytest.raises(ValueError, match="requires a validation set"): ttc.train(X_train=sample_text_data, y_train=sample_labels, training_config=config) @pytest.mark.parametrize("scheduler_patience, warns", [(3, True), (2, False)]) def test_warns_when_scheduler_patience_not_below_early_stopping( self, tmp_path, caplog, sample_text_data, sample_labels, scheduler_patience, warns ): ttc = _classifier(sample_text_data) with caplog.at_level(logging.WARNING, logger="torchTextClassifiers"): ttc.train( X_train=sample_text_data, y_train=sample_labels, X_val=sample_text_data, y_val=sample_labels, training_config=_training_config( tmp_path, scheduler_patience=scheduler_patience, early_stopping_patience=3 ), ) assert any("ReduceLROnPlateau patience" in r.message for r in caplog.records) is warns def test_warns_with_default_scheduler_patience( self, tmp_path, caplog, sample_text_data, sample_labels ): # No 'patience' given: PyTorch's default (10) is not below early stopping (3) ttc = _classifier(sample_text_data) config = _training_config(tmp_path) config.scheduler_params = None with caplog.at_level(logging.WARNING, logger="torchTextClassifiers"): ttc.train( X_train=sample_text_data, y_train=sample_labels, X_val=sample_text_data, y_val=sample_labels, training_config=config, ) assert any("ReduceLROnPlateau patience (10)" in r.message for r in caplog.records) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |