NPUEstimatorSpec Constructor

Description

Constructor of the NPUEstimatorSpec class. The NPUEstimatorSpec class inherits the EstimatorSpec class of the TensorFlow and can call the native APIs of the base class to define specific model objects.

EstimatorSpec is the return data structure of model_fn, including the mode, predictions, loss, train_op, and export_outputs fields. If EstimatorSpec cannot meet the training requirements, define NPUEstimatorSpec to replace EstimatorSpec.

Prototype

def __new__(cls,
mode,
predictions=None,
loss=None,
train_op=None,
eval_metric_ops=None,
export_outputs=None,
training_chief_hooks=None,
training_hooks=None,
scaffold=None,
evaluation_hooks=None,
prediction_hooks=None,
host_call=None)

Options

Option

Input/Output

Description

NPUEstimatorSpec inherits the following EstimatorSpec options:

mode

Input

Mode, indicating whether training, validation, or inference is being performed.

  • ModeKeys.TRAIN: training
  • ModeKeys.EVAL: validation
  • ModeKeys.PREDICT: inference

predictions

Input

Inference output tensor, required when mode is set to ModeKeys.PREDICT

loss

Input

Training loss

train_op

Input

Training operator

eval_metric_ops

Input

Dictionary of the measurement result (based on the tensor name). The dictionary value can be one of the following:

  • Metric instance
  • Result of calling the metric function, that is, the (metric_tensor, update_op) tuple.

export_outputs

Input

Used to save a model, describing the output format of the exported model

training_chief_hooks

Input

SessionRunHooks set of the master node during training

training_hooks

Input

SessionRunHooks set during training

scaffold

Input

Defines scaffold (providing the capability of customizing saver, init_op, summary_op, and global_step).

evaluation_hooks

Input

SessionRunHooks set during validation

prediction_hook

Input

SessionRunHooks set during inference

NPUEstimatorSpec has the following options added:

host_call

Input

Captures the summary information and sends the information of each step back to the host side.

host_call is a tuple consisting of a function and a list or dictionary of tensors. It is used to return a list of tensors.

host_call applies to train() and evaluate().

Returns

An object of the NPUEstimatorSpec class

Examples

1
2
3
4
from npu_bridge.npu_init import *
...
host_call = (_host_call_fn, [global_step, loss])
return NPUEstimatorSpec(mode=tf.estimator.ModeKeys.TRAIN, loss=loss, train_op=train_op, host_call=host_call)