Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate on best checkpoint after training #144

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions configs/train.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ train: True
# lightning chooses best weights based on the metric specified in checkpoint callback
test: False

# evaluate on validation set, using best model weights achieved during training
# lightning chooses best weights based on the metric specified in checkpoint callback
validate: True

# metric with mode (minimize or maximize) to monitor for checkpointing and early stopping callbacks
monitor_metric: "val/f1"
monitor_mode: "max"
Expand Down
2 changes: 2 additions & 0 deletions src/datamodules/datamodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def setup(self, stage: str):

if stage == "fit":
split_names = [self.train_split, self.val_split]
elif stage == "validate":
split_names = [self.val_split]
elif stage == "test":
split_names = [self.test_split]
else:
Expand Down
11 changes: 11 additions & 0 deletions src/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ def train(cfg: DictConfig) -> Tuple[dict, dict]:
else:
log.warning("the model is not saved because no save_dir is specified")

if cfg.get("validate"):
log.info("Starting validation!")
if best_ckpt_path == "":
log.warning("Best ckpt not found! Using current weights for validation...")
trainer.validate(model=model, datamodule=datamodule, ckpt_path=best_ckpt_path or None)
elif cfg.get("train"):
log.warning(
"Validation after training is skipped! That means, the finally reported validation scores are "
"the values from the *last* checkpoint, not from the *best* checkpoint (which is saved)!"
)

if cfg.get("test"):
log.info("Starting testing!")
if best_ckpt_path == "":
Expand Down
Loading