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

fix trainer final evaluation with tstv & best-model #3363

Merged
Merged
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
56 changes: 28 additions & 28 deletions flair/trainers/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,39 +767,39 @@ def train_custom(
# TensorboardLogger -> closes writer
self.dispatch("_training_finally")

# test best model if test data is present
if self.corpus.test and not train_with_test:
log_line(log)
# test best model if test data is present
if self.corpus.test and not train_with_test:
log_line(log)

self.model.eval()
self.model.eval()

if (base_path / "best-model.pt").exists():
log.info("Loading model from best epoch ...")
self.model.load_state_dict(self.model.load(base_path / "best-model.pt").state_dict())
else:
log.info("Testing using last state of model ...")

test_results = self.model.evaluate(
self.corpus.test,
gold_label_type=self.model.label_type,
mini_batch_size=eval_batch_size,
out_path=base_path / "test.tsv",
embedding_storage_mode="none",
main_evaluation_metric=main_evaluation_metric,
gold_label_dictionary=gold_label_dictionary_for_eval,
exclude_labels=exclude_labels,
return_loss=False,
)
if (base_path / "best-model.pt").exists():
log.info("Loading model from best epoch ...")
self.model.load_state_dict(self.model.load(base_path / "best-model.pt").state_dict())
else:
log.info("Testing using last state of model ...")

test_results = self.model.evaluate(
self.corpus.test,
gold_label_type=self.model.label_type,
mini_batch_size=eval_batch_size,
out_path=base_path / "test.tsv",
embedding_storage_mode="none",
main_evaluation_metric=main_evaluation_metric,
gold_label_dictionary=gold_label_dictionary_for_eval,
exclude_labels=exclude_labels,
return_loss=False,
)

log.info(test_results.detailed_results)
log_line(log)
log.info(test_results.detailed_results)
log_line(log)

# get and return the final test score of best model
self.return_values["test_score"] = test_results.main_score
# get and return the final test score of best model
self.return_values["test_score"] = test_results.main_score

else:
self.return_values["test_score"] = 0
log.info("Test data not provided setting final score to 0")
else:
self.return_values["test_score"] = 0
log.info("Test data not provided setting final score to 0")

# MetricHistoryPlugin -> stores the loss history in return_values
self.dispatch("after_training")
Expand Down