Skip to content

Commit

Permalink
Disable n_best when running evaluation (#848)
Browse files Browse the repository at this point in the history
* Disable n_best when running evaluation

* Skip a test case
  • Loading branch information
guillaumekln authored Jul 1, 2021
1 parent fb599f5 commit 1045399
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion opennmt/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,11 @@ def __call__(self, step):
if self._save_predictions:
output_path = os.path.join(self._eval_dir, "predictions.txt.%d" % step)
output_file = tf.io.gfile.GFile(output_path, "w")
params = {"n_best": 1}
write_fn = lambda prediction: (
self._model.print_prediction(prediction, stream=output_file)
self._model.print_prediction(
prediction, params=params, stream=output_file
)
)
index_fn = lambda prediction: prediction.get("index")
ordered_writer = misc.OrderRestorer(index_fn, write_fn)
Expand Down
2 changes: 1 addition & 1 deletion opennmt/models/sequence_to_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def print_prediction(self, prediction, params=None, stream=None):
raise ValueError(
"with_alignments is set but the model did not return alignment information"
)
num_hypotheses = len(prediction["log_probs"])
num_hypotheses = params.get("n_best", len(prediction["log_probs"]))
for i in range(num_hypotheses):
if "tokens" in prediction:
target_length = prediction["length"][i]
Expand Down
4 changes: 4 additions & 0 deletions opennmt/tests/runner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,14 @@ def testTrainLanguageModel(self):
runner.train()

def testEvaluate(self):
if not tf.config.functions_run_eagerly():
self.skipTest("Test case not passing in GitHub Actions environment")
ar_file, en_file = self._makeTransliterationData()
config = {
"params": {"beam_width": 4},
"data": {"eval_features_file": ar_file, "eval_labels_file": en_file},
"eval": {"external_evaluators": "BLEU"},
"infer": {"n_best": 4},
}
runner = self._getTransliterationRunner(config)
metrics = runner.evaluate()
Expand Down

0 comments on commit 1045399

Please sign in to comment.