TensorFlow 2.0 Beta is available Learn more

tf.contrib.gan.estimator.TPUGANEstimator

View source on GitHub

Class TPUGANEstimator

An estimator for Generative Adversarial Networks (GANs) on TPU.

Inherits From: TPUEstimator

Aliases:

  • Class tf.contrib.gan.estimator.tpu_gan_estimator.TPUGANEstimator

This Estimator is backed by TFGAN. It is similar to tfgan.GANEstimator, but works on TPU.

Example:

    import tensorflow as tf
    tfgan = tf.contrib.gan

    # See TFGAN's `train.py` for a description of the generator and
    # discriminator API.
    def generator_fn(generator_inputs):
      ...
      return generated_data

    def discriminator_fn(data, conditioning):
      ...
      return logits

    # Create GAN estimator.
    config = tpu_config.RunConfig(model_dir='/my/dir')
    gan_estimator = tfgan.estimator.TPUGANEstimator(
        generator_fn=generator_fn,
        discriminator_fn=discriminator_fn,
        generator_loss_fn=tfgan.losses.wasserstein_generator_loss,
        discriminator_loss_fn=tfgan.losses.wasserstein_discriminator_loss,
        generator_optimizer=tf.compat.v1.train.AdamOptimizer(0.1, 0.5),
        discriminator_optimizer=tf.compat.v1.train.AdamOptimizer(0.1, 0.5),
        train_batch_size=4,
        config=config)

    # Train estimator.
    gan_estimator.train(train_input_fn, train_steps)

    # Evaluate resulting estimator.
    gan_estimator.evaluate(eval_input_fn, eval_steps)

    # Generate samples from generator.
    predictions = np.array([
        x['generated_data'] for x in gan_estimator.predict(predict_input_fn)])

__init__

View source

__init__(
    generator_fn=None,
    discriminator_fn=None,
    generator_loss_fn=None,
    discriminator_loss_fn=None,
    generator_optimizer=None,
    discriminator_optimizer=None,
    get_eval_metric_ops_fn=None,
    add_summaries=None,
    joint_train=False,
    gan_train_steps=tfgan_tuples.GANTrainSteps(1, 1),
    model_dir=None,
    config=None,
    params=None,
    use_tpu=True,
    train_batch_size=None,
    eval_batch_size=None,
    predict_batch_size=None,
    batch_axis=None,
    eval_on_tpu=True,
    export_to_tpu=True,
    warm_start_from=None
)

Initializes a TPUGANEstimator instance.

Args:

  • generator_fn: A python function that takes a Tensor, Tensor list, or Tensor dictionary as inputs and returns the outputs of the GAN generator. See TFGAN for more details and examples. Additionally, if it has an argument called mode, the Estimator's mode will be passed in (ex TRAIN, EVAL, PREDICT). This is useful for things like batch normalization.
  • discriminator_fn: A python function that takes the output of generator_fn or real data in the GAN setup, and generator_inputs. Outputs a Tensor in the range [-inf, inf]. See TFGAN for more details and examples.
  • generator_loss_fn: The loss function on the generator. Takes a GANModel tuple.
  • discriminator_loss_fn: The loss function on the discriminator. Takes a GANModel tuple.
  • generator_optimizer: The optimizer for generator updates, or a function that takes no arguments and returns an optimizer. This function will be called when the default graph is the GANEstimator's graph, so utilities like tf.contrib.framework.get_or_create_global_step will work.
  • discriminator_optimizer: Same as generator_optimizer, but for the discriminator updates.
  • get_eval_metric_ops_fn: A function that takes a list of arguments and returns a dict of metric results keyed by name. The output of this function is passed into tf.estimator.EstimatorSpec during evaluation. The arguments must be:
    • generator_inputs
    • generated_data
    • real_data
    • discriminator_real_outputs
    • discriminator_gen_outputs
  • add_summaries: None, a single SummaryType, or a list of SummaryType. This is ignored for jobs that run on TPU, such as the train job if use_tpu is True or the eval job if eval_on_tpu is True.
  • joint_train: A Python boolean. If True, jointly train the generator and the discriminator. If False, sequentially train them. See train.py in TFGAN for more details on the differences between the two GAN training methods.
  • gan_train_steps: A tfgan.GANTrainSteps named tuple describing the ratio of generator to discriminator steps. For now, only supports 1:1 training.
  • model_dir: Same as TPUEstimator: Directory to save model parameters, graph and etc. This can also be used to load checkpoints from the directory into a estimator to continue training a previously saved model. If None, the model_dir in config will be used if set. If both are set, they must be same. If both are None, a temporary directory will be used.
  • config: Same as TPUEstimator: An tpu_config.RunConfig configuration object. Cannot be None.
  • params: Same as TPUEstimator: An optional dict of hyper parameters that will be passed into input_fn and model_fn. Keys are names of parameters, values are basic python types. There are reserved keys for TPUEstimator, including 'batch_size'.
  • use_tpu: Same as TPUEstimator: A bool indicating whether TPU support is enabled. Currently, TPU training and evaluation respect this bit, but eval_on_tpu can override execution of eval. See below. Predict still happens on CPU.
  • train_batch_size: Same as TPUEstimator: An int representing the global training batch size. TPUEstimator transforms this global batch size to a per-shard batch size, as params['batch_size'], when calling input_fn and model_fn. Cannot be None if use_tpu is True. Must be divisible by total number of replicas.
  • eval_batch_size: Same as TPUEstimator: An int representing evaluation batch size. Must be divisible by total number of replicas.
  • predict_batch_size: Same as TPUEstimator: An int representing the prediction batch size. Must be divisible by total number of replicas.
  • batch_axis: Same as TPUEstimator: A python tuple of int values describing how each tensor produced by the Estimator input_fn should be split across the TPU compute shards. For example, if your input_fn produced (images, labels) where the images tensor is in HWCN format, your shard dimensions would be [3, 0], where 3 corresponds to the N dimension of your images Tensor, and 0 corresponds to the dimension along which to split the labels to match up with the corresponding images. If None is supplied, and per_host_input_for_training is True, batches will be sharded based on the major dimension. If tpu_config.per_host_input_for_training is False or PER_HOST_V2, batch_axis is ignored.
  • eval_on_tpu: Same as TPUEstimator: If False, evaluation runs on CPU or GPU. In this case, the model_fn must return EstimatorSpec when called with mode as EVAL.
  • export_to_tpu: Same as TPUEstimator: If True, export_savedmodel() exports a metagraph for serving on TPU besides the one on CPU.
  • warm_start_from: Same as TPUEstimator: Optional string filepath to a checkpoint or SavedModel to warm-start from, or a tf.estimator.WarmStartSettings object to fully configure warm-starting. If the string filepath is provided instead of a WarmStartSettings, then all variables are warm-started, and it is assumed that vocabularies and Tensor names are unchanged.

Raises:

  • ValueError: If loss functions aren't callable.
  • ValueError: If gan_train_steps isn't a tfgan_tuples.GANTrainSteps tuple.
  • ValueError: If gan_train_steps isn't 1:1 training.

Properties

config

model_dir

model_fn

Returns the model_fn which is bound to self.params.

Returns:

The model_fn with following signature: def model_fn(features, labels, mode, config)

params

Methods

eval_dir

View source

eval_dir(name=None)

Shows the directory name where evaluation metrics are dumped.

Args:

  • name: Name of the evaluation if user needs to run multiple evaluations on different data sets, such as on training data vs test data. Metrics for different evaluations are saved in separate folders, and appear separately in tensorboard.

Returns:

A string which is the path of directory contains evaluation metrics.

evaluate

View source

evaluate(
    input_fn,
    steps=None,
    hooks=None,
    checkpoint_path=None,
    name=None
)

experimental_export_all_saved_models

View source

experimental_export_all_saved_models(
    export_dir_base,
    input_receiver_fn_map,
    assets_extra=None,
    as_text=False,
    checkpoint_path=None
)

Exports a SavedModel with tf.MetaGraphDefs for each requested mode.

For each mode passed in via the input_receiver_fn_map, this method builds a new graph by calling the input_receiver_fn to obtain feature and label Tensors. Next, this method calls the Estimator's model_fn in the passed mode to generate the model graph based on those features and labels, and restores the given checkpoint (or, lacking that, the most recent checkpoint) into the graph. Only one of the modes is used for saving variables to the SavedModel (order of preference: tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL, then tf.estimator.ModeKeys.PREDICT), such that up to three tf.MetaGraphDefs are saved with a single set of variables in a single SavedModel directory.

For the variables and tf.MetaGraphDefs, a timestamped export directory below export_dir_base, and writes a SavedModel into it containing the tf.MetaGraphDef for the given mode and its associated signatures.

For prediction, the exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.

For training and evaluation, the train_op is stored in an extra collection, and loss, metrics, and predictions are included in a SignatureDef for the mode in question.

Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.

Args:

  • export_dir_base: A string containing a directory in which to create timestamped subdirectories containing exported SavedModels.
  • input_receiver_fn_map: dict of tf.estimator.ModeKeys to input_receiver_fn mappings, where the input_receiver_fn is a function that takes no arguments and returns the appropriate subclass of InputReceiver.
  • assets_extra: A dict specifying how to populate the assets.extra directory within the exported SavedModel, or None if no extra assets are needed.
  • as_text: whether to write the SavedModel proto in text format.
  • checkpoint_path: The checkpoint path to export. If None (the default), the most recent checkpoint found within the model directory is chosen.

Returns:

The string path to the exported directory.

Raises:

  • ValueError: if any input_receiver_fn is None, no export_outputs are provided, or no checkpoint can be found.

export_saved_model

View source

export_saved_model(
    export_dir_base,
    serving_input_receiver_fn,
    assets_extra=None,
    as_text=False,
    checkpoint_path=None,
    experimental_mode=ModeKeys.PREDICT
)

Exports inference graph as a SavedModel into the given dir.

For a detailed guide, see Using SavedModel with Estimators.

This method builds a new graph by first calling the serving_input_receiver_fn to obtain feature Tensors, and then calling this Estimator's model_fn to generate the model graph based on those features. It restores the given checkpoint (or, lacking that, the most recent checkpoint) into this graph in a fresh session. Finally it creates a timestamped export directory below the given export_dir_base, and writes a SavedModel into it containing a single tf.MetaGraphDef saved from this session.

The exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.

Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.

The experimental_mode parameter can be used to export a single train/eval/predict graph as a SavedModel. See experimental_export_all_saved_models for full docs.

Args:

  • export_dir_base: A string containing a directory in which to create timestamped subdirectories containing exported SavedModels.
  • serving_input_receiver_fn: A function that takes no argument and returns a tf.estimator.export.ServingInputReceiver or tf.estimator.export.TensorServingInputReceiver.
  • assets_extra: A dict specifying how to populate the assets.extra directory within the exported SavedModel, or None if no extra assets are needed.
  • as_text: whether to write the SavedModel proto in text format.
  • checkpoint_path: The checkpoint path to export. If None (the default), the most recent checkpoint found within the model directory is chosen.
  • experimental_mode: tf.estimator.ModeKeys value indicating with mode will be exported. Note that this feature is experimental.

Returns:

The string path to the exported directory.

Raises:

  • ValueError: if no serving_input_receiver_fn is provided, no export_outputs are provided, or no checkpoint can be found.

export_savedmodel

View source

export_savedmodel(
    export_dir_base,
    serving_input_receiver_fn,
    assets_extra=None,
    as_text=False,
    checkpoint_path=None,
    strip_default_attrs=False
)

Exports inference graph as a SavedModel into the given dir. (deprecated)

For a detailed guide, see Using SavedModel with Estimators.

This method builds a new graph by first calling the serving_input_receiver_fn to obtain feature Tensors, and then calling this Estimator's model_fn to generate the model graph based on those features. It restores the given checkpoint (or, lacking that, the most recent checkpoint) into this graph in a fresh session. Finally it creates a timestamped export directory below the given export_dir_base, and writes a SavedModel into it containing a single tf.MetaGraphDef saved from this session.

The exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.

Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.

Args:

  • export_dir_base: A string containing a directory in which to create timestamped subdirectories containing exported SavedModels.
  • serving_input_receiver_fn: A function that takes no argument and returns a tf.estimator.export.ServingInputReceiver or tf.estimator.export.TensorServingInputReceiver.
  • assets_extra: A dict specifying how to populate the assets.extra directory within the exported SavedModel, or None if no extra assets are needed.
  • as_text: whether to write the SavedModel proto in text format.
  • checkpoint_path: The checkpoint path to export. If None (the default), the most recent checkpoint found within the model directory is chosen.
  • strip_default_attrs: Boolean. If True, default-valued attributes will be removed from the NodeDefs. For a detailed guide, see Stripping Default-Valued Attributes.

Returns:

The string path to the exported directory.

Raises:

  • ValueError: if no serving_input_receiver_fn is provided, no export_outputs are provided, or no checkpoint can be found.

get_variable_names

View source

get_variable_names()

Returns list of all variable names in this model.

Returns:

List of names.

Raises:

  • ValueError: If the Estimator has not produced a checkpoint yet.

get_variable_value

View source

get_variable_value(name)

Returns value of the variable given by name.

Args:

  • name: string or a list of string, name of the tensor.

Returns:

Numpy array - value of the tensor.

Raises:

  • ValueError: If the Estimator has not produced a checkpoint yet.

latest_checkpoint

View source

latest_checkpoint()

Finds the filename of the latest saved checkpoint file in model_dir.

Returns:

The full path to the latest checkpoint or None if no checkpoint was found.

predict

View source

predict(
    input_fn,
    predict_keys=None,
    hooks=None,
    checkpoint_path=None,
    yield_single_examples=True
)

train

View source

train(
    input_fn,
    hooks=None,
    steps=None,
    max_steps=None,
    saving_listeners=None
)