Add metric name validation for hyperparameter search (#241)

This commit is contained in:
Łukasz Zalewski authored and GitHub committed 2022-01-09 00:34:53 +01:00
1 parent c4940edaec
commit 15bef37b1d
2 files changed
+8 -1

No files matched your search

+1
View File
@@ -7,6 +7,7 @@ defaults:
- override /hydra/sweeper: optuna
# choose metric which will be optimized by Optuna
# make sure this is the correct name of some metric logged in lightning module!
optimized_metric: "val/acc_best"
hydra:
+7 -1
View File
@@ -77,7 +77,13 @@ def train(config: DictConfig) -> Optional[float]:
trainer.fit(model=model, datamodule=datamodule)
# Get metric score for hyperparameter optimization
score = trainer.callback_metrics.get(config.get("optimized_metric"))
optimized_metric = config.get("optimized_metric")
if optimized_metric and optimized_metric not in trainer.callback_metrics:
raise Exception(
"Metric for hyperparameter optimization not found! "
"Make sure the `optimized_metric` in `hparams_search` config is correct!"
)
score = trainer.callback_metrics.get(optimized_metric)
# Test the model
if config.get("test_after_training") and not config.trainer.get("fast_dev_run"):