opennmt.runner module

Main library entrypoint.

class opennmt.runner.Runner(model, config, seed=None, num_devices=1, gpu_allow_growth=False, session_config=None, auto_config=False, hvd=None)[source]

Bases: object

Class for managing training, inference, and export. It is mostly a wrapper around tf.estimator.Estimator.

__init__(model, config, seed=None, num_devices=1, gpu_allow_growth=False, session_config=None, auto_config=False, hvd=None)[source]

Initializes the runner parameters.

Parameters:
  • model – A opennmt.models.model.Model instance to run.
  • config – The run configuration.
  • seed – The random seed to set.
  • num_devices – The number of devices (GPUs) to use for training.
  • gpu_allow_growth – Allow GPU memory to grow dynamically.
  • session_configtf.ConfigProto overrides.
  • auto_config – If True, use automatic configuration values defined by model.
  • hvd – Optional Horovod object.
Raises:

NotImplementedError – If auto_config is True but model does not define any automatic configuration values.

is_chief()[source]

Returns True if this runner is the master runner.

train_and_evaluate(checkpoint_path=None)[source]

Runs the training and evaluation loop.

Parameters:checkpoint_path – The checkpoint path to load the model weights from it.
Returns:A tuple with a dict of evaluation metrics and the export result or None in TensorFlow 1.8 and older.
train(checkpoint_path=None)[source]

Runs the training loop.

Parameters:checkpoint_path – The checkpoint path to load the model weights from it.
Returns:The path to the final model directory.
evaluate(checkpoint_path=None)[source]

Runs evaluation.

Parameters:checkpoint_path – The checkpoint path to load the model weights from it.
Returns:A dict of evaluation metrics.
average_checkpoints(output_dir, max_count=8)[source]

Averages checkpoints.

Parameters:
  • output_dir – The directory that will contain the averaged checkpoint.
  • max_count – The maximum number of checkpoints to average.
Returns:

The path to the directory containing the averaged checkpoint.

infer(features_file, predictions_file=None, checkpoint_path=None, log_time=False)[source]

Runs inference.

Parameters:
  • features_file – The file(s) to infer from.
  • predictions_file – If set, predictions are saved in this file.
  • checkpoint_path – Path of a specific checkpoint to predict. If None, the latest is used.
  • log_time – If True, several time metrics will be printed in the logs at the end of the inference loop.
export(checkpoint_path=None, export_dir_base=None)[source]

Exports a model.

Parameters:
  • checkpoint_path – The checkpoint path to export. If None, the latest is used.
  • export_dir_base – The base directory in which a timestamped subdirectory containing the exported model will be created. Defaults to $MODEL_DIR/export/manual.
Returns:

The string path to the exported directory.

score(features_file, predictions_file, checkpoint_path=None, output_file=None)[source]

Scores existing predictions.

Parameters:
  • features_file – The input file.
  • predictions_file – The predictions file to score.
  • checkpoint_path – Path of a specific checkpoint to use. If None, the latest is used.
  • output_file – The file where the scores are saved. Otherwise, they will be printed on the standard output.
Raises:

ValueError – if no checkpoint are found or if the model is not a sequence to sequence model.