Skip to content

TensorFlow

TensorFlowTrial Interface

TensorFlow trials are created by subclassing the abstract class TensorFlowTrial. Users must define the following abstract methods to create the deep learning model associated with a specific trial, and to subsequently train and evaluate it:

  • data_placeholders(self): Specifies the input data as a dictionary that maps string names to tf.placeholder values. If there are multiple entries in the dictionary, they should match the string names provided to the first argument of the Batch constructor in make_data_loaders().
  • label_placeholders(self): Specifies the label data as a dictionary that maps string names to tf.placeholder values. If there are multiple entries in the dictionary, they should match the string names provided to the first argument of the Batch constructor in make_data_loaders().
  • batch_size(self): Specifies the batch size to use for training.
  • optimizer(self): Specifies the learning algorithm, e.g., the minimize() method of tf.train.MomentumOptimizer.
  • build_graph(self, data_placeholders, label_placeholders, is_training): Builds the TensorFlow graph of variables and operations used for training and validation. data_placeholders and label_placeholders are dictionaries that map string names to placeholder tensors, as specified by data_placeholders(self) and label_placeholders(self). is_training is a Boolean tf.Tensor that is True during a training step and False during a validation step. This function should return a dictionary that maps string names to tf.Tensor nodes in the graph. Any outputs that may potentially be used as training and/or validation metrics should be returned by this function. This function must return at least one tf.Tensor named "loss", which is the value that will be optimized during training.
  • validation_metrics(self): Specifies a list of names of metrics that will be evaluated on the validation data. Each of these names must correspond to a tf.Tensor value returned by build_graph(self).

Note

We recommend initializing the TensorFlow graph in __init__(self, training_loader, validation_loader, hparams) because (i) the graph is required to define all of the aforementioned abstract methods; and (ii) __init__() is the only method of TensorFlowTrial that has access to hparams.

Optional Methods

  • training_metrics(self): Specifies a list of names of metrics that will be evaluated on each batch of training data. Training loss is always computed and reported as a metric named loss. If supplied, this function defines the metrics to be computed in addition to the training loss. Each of the returned metric names must correspond to a tf.Tensor value returned by build_graph(self).
  • session_config(self): Specifies the tf.ConfigProto to be used by the TensorFlow session. By default, tf.ConfigProto(allow_soft_placement=True) is used.

Data Loading

Loading data into TensorFlowTrial models is done by utilizing Batch Loaders.

Examples

EstimatorTrial Interface

To use TensorFlow's high-level tf.estimator.Estimator API with PEDL, users should subclass the abstract class EstimatorTrial. Users must define the following abstract methods to create the deep learning model associated with a specific trial, and to subsequently train and evaluate it:

  • build_estimator(self, hparams): Specifies the tf.estimator.Estimator instance to be used during training and validation. This may be an instance of a Premade Estimator provided by the TensorFlow team, or a Custom Estimator created by the user.
  • build_train_spec(self, hparams): Specifies the tf.estimator.TrainSpec to be used for training steps. This training specification will contain a TensorFlow input_fn which constructs the input data for a training step. Unlike the standard Tensorflow input_fn interface, EstimatorTrial only supports an input_fn that returns a tf.data.Dataset object. A function that returns a tuple of features and labels is currently not supported by EstimatorTrial. Additionally, the max_steps attribute of the training specification will be ignored; instead, the batches_per_step option in the experiment configuration is used to determine how many batches each training step uses.
  • build_eval_spec(self, hparams): Specifies the tf.estimator.EvalSpec to be used for validation steps. This evaluation spec will contain a TensorFlow input_fn which constructs the input data for a validation step. The validation step will evaluate steps batches, or evaluate until the input_fn raises an end-of-input exception if steps is None.

Optional Methods

  • build_serving_input_receiver_fns(self, hparams): Optionally returns a Python dictionary mapping string names to serving_input_receiver_fns. If specified, each serving input receiver function will be used to export a distinct SavedModel inference graph when a PEDL checkpoint is saved, using Estimator.export_saved_model. The exported models are saved under subdirectories named by the keys of the respective serving input receiver functions. For example, returning
    {
      "raw": tf.estimator.export.build_raw_serving_input_receiver_fn(...),
      "parsing": tf.estimator.export.build_parsing_serving_input_receiver_fn(...)
    }
    
    from this function would configure PEDL to export two SavedModel inference graphs in every checkpoint under raw and parsing subdirectories, respectively. By default, this function returns an empty dictionary and the PEDL checkpoint directory only contains metadata associated with the training graph.

Examples