utils

funcs

utils.funcs.evaluate(model, checkpoint)[source]
utils.funcs.infer(model, checkpoint, output_file)[source]
utils.funcs.restore_and_get_results(model, checkpoint, mode)[source]
utils.funcs.train(train_model, eval_model=None, debug_port=None, custom_hooks=None)[source]

hooks

class utils.hooks.BroadcastGlobalVariablesHook(root_rank, device='')[source]

Bases: tensorflow.python.training.session_run_hook.SessionRunHook

SessionRunHook that will broadcast all global variables from root rank to all other processes during initialization. This is necessary to ensure consistent initialization of all workers when training is started with random weights or restored from a checkpoint.

__init__(root_rank, device='')[source]

Construct a new BroadcastGlobalVariablesHook that will broadcast all global variables from root rank to all other processes during initialization. :param root_rank: Rank that will send data, other ranks will receive data. :param device: Device to be used for broadcasting. Uses GPU by default

if Horovod was build with HOROVOD_GPU_BROADCAST.
after_create_session(session, coord)[source]

Called when new TensorFlow session is created.

This is called to signal the hooks that a new session has been created. This has two essential differences with the situation in which begin is called:

  • When this is called, the graph is finalized and ops can no longer be added
    to the graph.
  • This method will also be called as a result of recovering a wrapped
    session, not only at the beginning of the overall session.
Parameters:
  • session – A TensorFlow Session that has been created.
  • coord – A Coordinator object which keeps track of all threads.
begin()[source]

Called once before using the session.

When called, the default graph is the one that will be launched in the session. The hook can modify the graph by adding new operations to it. After the begin() call the graph will be finalized and the other callbacks can not modify the graph anymore. Second call of begin() on the same graph, should not change the graph.

class utils.hooks.PrintLossAndTimeHook(every_steps, model, print_ppl=False)[source]

Bases: tensorflow.python.training.session_run_hook.SessionRunHook

Session hook that prints training samples and prediction from time to time

after_run(run_context, run_values)[source]

Called after each call to run().

The run_values argument contains results of requested ops/tensors by before_run().

The run_context argument is the same one send to before_run call. run_context.request_stop() can be called to stop the iteration.

If session.run() raises any exceptions then after_run() is not called.

Parameters:
  • run_context – A SessionRunContext object.
  • run_values – A SessionRunValues object.
before_run(run_context)[source]

Called before each call to run().

You can return from this call a SessionRunArgs object indicating ops or tensors to add to the upcoming run() call. These ops/tensors will be run together with the ops/tensors originally passed to the original run() call. The run args you return can also contain feeds to be added to the run() call.

The run_context argument is a SessionRunContext that provides information about the upcoming run() call: the originally requested op/tensors, the TensorFlow Session.

At this point graph is finalized and you can not add ops.

Parameters:run_context – A SessionRunContext object.
Returns:None or a SessionRunArgs object.
begin()[source]

Called once before using the session.

When called, the default graph is the one that will be launched in the session. The hook can modify the graph by adding new operations to it. After the begin() call the graph will be finalized and the other callbacks can not modify the graph anymore. Second call of begin() on the same graph, should not change the graph.

class utils.hooks.PrintSamplesHook(every_steps, model)[source]

Bases: tensorflow.python.training.session_run_hook.SessionRunHook

Session hook that prints training samples and prediction from time to time

after_run(run_context, run_values)[source]

Called after each call to run().

The run_values argument contains results of requested ops/tensors by before_run().

The run_context argument is the same one send to before_run call. run_context.request_stop() can be called to stop the iteration.

If session.run() raises any exceptions then after_run() is not called.

Parameters:
  • run_context – A SessionRunContext object.
  • run_values – A SessionRunValues object.
before_run(run_context)[source]

Called before each call to run().

You can return from this call a SessionRunArgs object indicating ops or tensors to add to the upcoming run() call. These ops/tensors will be run together with the ops/tensors originally passed to the original run() call. The run args you return can also contain feeds to be added to the run() call.

The run_context argument is a SessionRunContext that provides information about the upcoming run() call: the originally requested op/tensors, the TensorFlow Session.

At this point graph is finalized and you can not add ops.

Parameters:run_context – A SessionRunContext object.
Returns:None or a SessionRunArgs object.
begin()[source]

Called once before using the session.

When called, the default graph is the one that will be launched in the session. The hook can modify the graph by adding new operations to it. After the begin() call the graph will be finalized and the other callbacks can not modify the graph anymore. Second call of begin() on the same graph, should not change the graph.

class utils.hooks.RunEvaluationHook(every_steps, model, last_step=-1, print_ppl=False)[source]

Bases: tensorflow.python.training.session_run_hook.SessionRunHook

Session hook that runs evaluation on a validation set

after_run(run_context, run_values)[source]

Called after each call to run().

The run_values argument contains results of requested ops/tensors by before_run().

The run_context argument is the same one send to before_run call. run_context.request_stop() can be called to stop the iteration.

If session.run() raises any exceptions then after_run() is not called.

Parameters:
  • run_context – A SessionRunContext object.
  • run_values – A SessionRunValues object.
before_run(run_context)[source]

Called before each call to run().

You can return from this call a SessionRunArgs object indicating ops or tensors to add to the upcoming run() call. These ops/tensors will be run together with the ops/tensors originally passed to the original run() call. The run args you return can also contain feeds to be added to the run() call.

The run_context argument is a SessionRunContext that provides information about the upcoming run() call: the originally requested op/tensors, the TensorFlow Session.

At this point graph is finalized and you can not add ops.

Parameters:run_context – A SessionRunContext object.
Returns:None or a SessionRunArgs object.
begin()[source]

Called once before using the session.

When called, the default graph is the one that will be launched in the session. The hook can modify the graph by adding new operations to it. After the begin() call the graph will be finalized and the other callbacks can not modify the graph anymore. Second call of begin() on the same graph, should not change the graph.

utils

class utils.utils.Logger(stream, log_file)[source]

Bases: object

flush()[source]
write(msg)[source]
utils.utils.array_to_string(row, vocab, delim=' ')[source]
utils.utils.cast_types(input_dict, dtype)[source]
utils.utils.check_base_model_logdir(base_logdir, args, restore_best_checkpoint=False)[source]

A helper function that ensures the logdir is setup correctly

Parameters:
  • args (dict) – Dictionary as returned from get_base_config()
  • base_config (dict) – Dictionary as returned from get_base_config()
  • restore_best_checkpoint (bool) – If True, will look for ckpt_dir + /best_models
Returns:

Either None if continue-learning is not set and training, or

the name of the checkpoint used to restore the model

Return type:

checkpoint

utils.utils.check_logdir(args, base_config, restore_best_checkpoint=False)[source]

A helper function that ensures the logdir is setup correctly

Parameters:
  • args (dict) – Dictionary as returned from get_base_config()
  • base_config (dict) – Dictionary as returned from get_base_config()
  • restore_best_checkpoint (bool) – If True, will look for ckpt_dir + /best_models
Returns:

Either None if continue-learning is not set and training, or

the name of the checkpoint used to restore the model

Return type:

checkpoint

utils.utils.check_params(config, required_dict, optional_dict)[source]
utils.utils.clip_last_batch(last_batch, true_size)[source]
utils.utils.clip_sparse(value, size)[source]
utils.utils.collect_if_horovod(value, hvd, mode='sum')[source]

Collects values from all workers if run on Horovod. Note, that on all workers except first this function will return None.

Parameters:
  • value – value to collect.
  • hvd – horovod.tensorflow module or None
  • mode – could be “sum”, “mean” or “gather”, indicating reduce_sum or gather. For “sum” and “mean” value has to be numerical, for “gather”, value has to be iterable.
Returns:

collected results if run on Horovod or value otherwise.

utils.utils.create_logdir(args, base_config)[source]

A helper function that ensures the logdir and log files are setup corretly. Only called in –enable_logs is set.

Args:
args (dict): Dictionary as returned from get_base_config() base_config (dict): Dictionary as returned from get_base_config()
Returns:Some objects that need to be cleaned up in run.py
utils.utils.create_model(args, base_config, config_module, base_model, hvd, checkpoint=None)[source]

A helpful function that creates the train, eval, and infer models as needed.

Parameters:
  • args (dict) – Dictionary as returned from get_base_config()
  • base_config (dict) – Dictionary as returned from get_base_config()
  • config_module – config_module as returned from get_base_config()
  • base_model (OpenSeq2Seq model) – Dictionary as returned from get_base_config()
  • hvd – Either None if Horovod is not enabled, or the Horovod library
  • checkpoint (str) – checkpoint path as returned from tf.train.latest_checkpoint
Returns:

A compiled model. For the ‘train_eval’ mode, a tuple containing the

(train_model, eval_model) is returned.

Return type:

model

utils.utils.deco_print(line, offset=0, start='*** ', end='\n')[source]
utils.utils.flatten_dict(dct)[source]
utils.utils.get_available_gpus()[source]
utils.utils.get_base_config(args)[source]

This function parses the command line arguments, reads the config file, and gets the base_model from the config.

Parameters:args (str) – The command line arugments
Returns

args (dict): The arguments parsed into a dictionary base_config (dict): The config read from the file and ammended with the

command line arguments

base_model (OpenSeq2Seq model): The model specified in the config file config_module (dict): The raw config file processed by runpy

utils.utils.get_calibration_config(arguments)[source]

This function parses the command line arguments, reads the config file, and gets the base_model from the config.

Parameters:args (str) – The command line arguments
Returns

args (dict): The arguments parsed into a dictionary base_config (dict): The config read from the file and ammended with the

command line arguments

base_model (OpenSeq2Seq model): The model specified in the config file config_module (dict): The raw config file processed by runpy

utils.utils.get_git_diff()[source]
utils.utils.get_git_hash()[source]
utils.utils.get_interactive_infer_results(model, sess, model_in)[source]
utils.utils.get_results_for_epoch(model, sess, compute_loss, mode, verbose=False)[source]
utils.utils.iterate_data(model, sess, compute_loss, mode, verbose, num_steps=None)[source]
utils.utils.log_summaries_from_dict(dict_to_log, output_dir, step)[source]

A function that writes values from dict_to_log to a tensorboard log file inside output_dir.

Parameters:
  • dict_to_log (dict) – A dictiontary containing the tags and scalar values to log. The dictionary values could also contain tf.Summary.Value objects to support logging of image and audio data. In this mode, the dictionary key is ignored, as tf.Summary.Value already contains a tag.
  • output_dir (str) – dir containing the tensorboard file
  • step (int) – current training step
utils.utils.mask_nans(x)[source]
utils.utils.nest_dict(flat_dict)[source]
utils.utils.nested_update(org_dict, upd_dict)[source]
utils.utils.text_ids_to_string(row, vocab, S_ID, EOS_ID, PAD_ID, ignore_special=False, delim=' ')[source]

For _-to-text outputs this function takes a row with ids, target vocabulary and prints it as a human-readable string