Skip to content

ReduceLROnPlateau: monitor val_loss like early stopping - #120

Merged
meilame-tayebjee merged 2 commits into
mainfrom
fix/scheduler-monitor-val-loss
Nov 25, 2026
Merged

meilame-tayebjee merged 2 commits into
mainfrom
fix/scheduler-monitor-val-loss

Conversation

@meilame-tayebjee

Copy link
Copy Markdown
Member

Problème

Dans train(), ModelCheckpoint et EarlyStopping surveillent val_loss (quand un jeu de validation est fourni), mais TextClassificationModule.configure_optimizers codait en dur "monitor": "train_loss" pour ReduceLROnPlateau.

Conséquence : la train loss continue de baisser pendant que la val loss stagne, donc le LR n'est quasiment jamais réduit et l'early stopping coupe l'entraînement avant. Le commentaire qui justifiait ce choix (« val_loss pas toujours disponible ») ne tient pas : avec interval="epoch", Lightning fait avancer le scheduler après la boucle de validation.

S'y ajoute un problème de valeurs par défaut : la patience de ReduceLROnPlateau vaut 10 dans PyTorch, alors que patience_early_stopping vaut 3. Même en surveillant la bonne métrique, le LR ne serait jamais réduit.

Changements

  • TextClassificationModule prend un nouvel argument scheduler_monitor (défaut "train_loss" : comportement inchangé pour qui instancie le module directement, et compatible avec les anciens checkpoints).
  • train() calcule une seule métrique monitor (val_loss si validation, sinon train_loss), partagée par le scheduler, ModelCheckpoint et EarlyStopping.
  • Warning si la patience de ReduceLROnPlateau (celle passée dans scheduler_params, sinon le défaut lu dans la signature du scheduler) n'est pas strictement inférieure à patience_early_stopping.

Tests

Nouveau fichier tests/test_scheduler_monitor.py :

  • configure_optimizers : monitor par défaut, monitor personnalisé, scheduler non-plateau ;
  • train() avec / sans validation → val_loss / train_loss ;
  • warning sur la patience (valeur explicite et défaut PyTorch).

5 de ces tests échouent sur main et passent avec ce correctif ; test_sample_weights.py et test_torchTextClassifiers.py passent toujours.

🤖 Generated with Claude Code

meilame-tayebjee and others added 2 commits November 25, 2026 16:22
ReduceLROnPlateau was hard-coded to monitor train_loss while EarlyStopping
and ModelCheckpoint monitor val_loss. Train loss keeps decreasing while
val loss plateaus, so early stopping fired before the LR was ever reduced.

- TextClassificationModule gets a scheduler_monitor argument (default
  "train_loss", unchanged for direct users).
- train() computes one monitor metric and shares it between the
  scheduler, ModelCheckpoint and EarlyStopping.
- Warn when ReduceLROnPlateau patience (PyTorch default: 10) is not
  lower than patience_early_stopping (default: 3).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@claude

claude Bot commented Nov 25, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@meilame-tayebjee
meilame-tayebjee merged commit fba2dbd into main Nov 25, 2026
6 checks passed
@meilame-tayebjee
meilame-tayebjee deleted the fix/scheduler-monitor-val-loss branch November 25, 2026 16:34
meilame-tayebjee added a commit that referenced this pull request Nov 25, 2026
Follow-up to #120. TrainingConfig gets scheduler_monitor (default None:
same metric as early stopping, i.e. val_loss with a validation set,
train_loss otherwise). Setting it to "train_loss" keeps the previous
behaviour. Asking for a val_* metric without a validation set raises a
clear ValueError instead of an obscure Lightning error.

Docs: document scheduler_monitor, which metric early stopping and
checkpointing watch, the patience constraint with ReduceLROnPlateau
(PyTorch default patience is 10), and add a ReduceLROnPlateau example.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant