(gan, sess, outdir, checkpoint_path, dataset, options)
return (mean_eval_info, mean_train_consistency, mean_eval_consistency,
          mean_self_consistency)
Build and train GILBO model.

  Args:
    gan: GAN object.
    sess: tf.Session.
    outdir: Output directory. A pickle file will be written there.
    checkpoint_path: Path where gan"s checkpoints are written. Only used to
                     ensure that GILBO files are written to a unique
                     subdirectory of outdir.
    dataset: Name of dataset used to train the GAN.
    options: Options dictionary.

  Returns:
    mean_eval_info: Mean GILBO computed over a large number of images generated
                    by the trained GAN
    mean_train_consistency: Mean consistency of the trained GILBO model with
                            data from the training set.
    mean_eval_consistency: Same consistency measure for the trained model with
                           data from the validation set.
    mean_self_consistency: Same consistency measure for the trained model with
                           data generated by the trained model itself.
    See the GILBO paper for an explanation of these metrics.

  Raises:
    ValueError: If the GAN has uninitialized variables.
Build and train GILBO model.
Build and train GILBO model .
def train_gilbo(gan, sess, outdir, checkpoint_path, dataset, options):
  """Build and train GILBO model.

  Args:
    gan: GAN object.
    sess: tf.Session.
    outdir: Output directory. A pickle file will be written there.
    checkpoint_path: Path where gan"s checkpoints are written. Only used to
                     ensure that GILBO files are written to a unique
                     subdirectory of outdir.
    dataset: Name of dataset used to train the GAN.
    options: Options dictionary.

  Returns:
    mean_eval_info: Mean GILBO computed over a large number of images generated
                    by the trained GAN
    mean_train_consistency: Mean consistency of the trained GILBO model with
                            data from the training set.
    mean_eval_consistency: Same consistency measure for the trained model with
                           data from the validation set.
    mean_self_consistency: Same consistency measure for the trained model with
                           data generated by the trained model itself.
    See the GILBO paper for an explanation of these metrics.

  Raises:
    ValueError: If the GAN has uninitialized variables.
  """
  uninitialized = sess.run(tf.report_uninitialized_variables())
  if uninitialized:
    raise ValueError("Model has uninitialized variables!\n%r" % uninitialized)

  outdir = os.path.join(outdir, checkpoint_path.replace("/", "_"))

  tf.gfile.MakeDirs(outdir)
  with tf.variable_scope("gilbo"):
    ones = tf.ones((gan.batch_size, gan.z_dim))
    # Get a distribution for the prior.
    z_dist = ds.Independent(ds.Uniform(-ones, ones), 1)
    z_sample = z_dist.sample()
    epsneg = np.finfo("float32").epsneg
    # Clip samples from the GAN uniform prior because the Beta distribution
    # doesn"t include the top endpoint and has issues with the bottom endpoint.
    ganz_clip = tf.clip_by_value(gan.z, -(1 - epsneg), 1 - epsneg)

    # Get generated images from the model.
    fake_images = gan.fake_images

    # Build the regressor distribution that encodes images back to predicted
    # samples from the prior.
    with tf.variable_scope("regressor"):
      z_pred_dist = _build_regressor(fake_images, gan.z_dim)
    # Capture the parameters of the distributions for later analysis.
    dist_p1 = z_pred_dist.distribution.distribution.concentration0
    dist_p2 = z_pred_dist.distribution.distribution.concentration1

    # info and avg_info compute the GILBO.
    info = z_pred_dist.log_prob(ganz_clip) - z_dist.log_prob(ganz_clip)
    avg_info = tf.reduce_mean(info)

    # Set up training of the GILBO model.
    lr = options.get("gilbo_learning_rate", 4e-4)
    learning_rate = tf.get_variable(
        "learning_rate", initializer=lr, trainable=False)
    gilbo_step = tf.get_variable("gilbo_step", dtype=tf.int32, initializer=0,
                                 trainable=False)
    opt = tf.train.AdamOptimizer(learning_rate)

    regressor_vars = tf.contrib.framework.get_variables("gilbo/regressor")
    train_op = opt.minimize(-info, var_list=regressor_vars)

  # Initialize the variables we just created.
  uninitialized = plist(tf.report_uninitialized_variables().eval())
  uninitialized_vars = uninitialized.apply(
      tf.contrib.framework.get_variables_by_name)._[0]
  tf.variables_initializer(uninitialized_vars).run()

  saver = tf.train.Saver(uninitialized_vars, max_to_keep=1)
  try:
    checkpoint_path = tf.train.latest_checkpoint(outdir)
    saver.restore(sess, checkpoint_path)
  except ValueError:
    # Failing to restore just indicates that we don"t have a valid checkpoint,
    # so we will just start training a fresh GILBO model.
    pass
  _train_gilbo(sess, gan, saver, learning_rate, gilbo_step, z_sample, avg_info,
               z_pred_dist, train_op, outdir, options)

  mean_eval_info = _eval_gilbo(sess, gan, z_sample, avg_info,
                               dist_p1, dist_p2, fake_images, outdir, options)
  # Collect encoded distributions on the training and eval set in order to do
  # kl-nearest-neighbors on generated samples and measure consistency.
  dataset = datasets.get_dataset(dataset)
  x_train = dataset.load_dataset(split_name="train", num_threads=1)
  x_train = x_train.batch(gan.batch_size, drop_remainder=True)
  x_train = x_train.make_one_shot_iterator().get_next()[0]
  x_train = tf.reshape(x_train, fake_images.shape)

  x_eval = dataset.load_dataset(split_name="test", num_threads=1)
  x_eval = x_eval.batch(gan.batch_size, drop_remainder=True)
  x_eval = x_eval.make_one_shot_iterator().get_next()[0]
  x_eval = tf.reshape(x_eval, fake_images.shape)

  mean_train_consistency = _run_gilbo_consistency(
      x_train, "train", extract_input_images=0,
      save_consistency_images=20, num_batches=5, **locals())
  mean_eval_consistency = _run_gilbo_consistency(
      x_eval, "eval", extract_input_images=0,
      save_consistency_images=20, num_batches=5, **locals())
  mean_self_consistency = _run_gilbo_consistency(
      fake_images, "self", extract_input_images=20,
      save_consistency_images=20, num_batches=5, **locals())
  return (mean_eval_info, mean_train_consistency, mean_eval_consistency,
          mean_self_consistency)
def train_gilbo ( gan , sess , outdir , checkpoint_path , dataset , options ) : uninitialized = sess . run ( tf . report_uninitialized_variables ( ) ) if uninitialized : raise ValueError ( "Model has uninitialized variables!\n%r" % uninitialized ) outdir = os . path . join ( outdir , checkpoint_path . replace ( "/" , "_" ) ) tf . gfile . MakeDirs ( outdir ) with tf . variable_scope ( "gilbo" ) : ones = tf . ones ( ( gan . batch_size , gan . z_dim ) ) # Get a distribution for the prior. z_dist = ds . Independent ( ds . Uniform ( - ones , ones ) , 1 ) z_sample = z_dist . sample ( ) epsneg = np . finfo ( "float32" ) . epsneg # Clip samples from the GAN uniform prior because the Beta distribution # doesn"t include the top endpoint and has issues with the bottom endpoint. ganz_clip = tf . clip_by_value ( gan . z , - ( 1 - epsneg ) , 1 - epsneg ) # Get generated images from the model. fake_images = gan . fake_images # Build the regressor distribution that encodes images back to predicted # samples from the prior. with tf . variable_scope ( "regressor" ) : z_pred_dist = _build_regressor ( fake_images , gan . z_dim ) # Capture the parameters of the distributions for later analysis. dist_p1 = z_pred_dist . distribution . distribution . concentration0 dist_p2 = z_pred_dist . distribution . distribution . concentration1 # info and avg_info compute the GILBO. info = z_pred_dist . log_prob ( ganz_clip ) - z_dist . log_prob ( ganz_clip ) avg_info = tf . reduce_mean ( info ) # Set up training of the GILBO model. lr = options . get ( "gilbo_learning_rate" , 4e-4 ) learning_rate = tf . get_variable ( "learning_rate" , initializer = lr , trainable = False ) gilbo_step = tf . get_variable ( "gilbo_step" , dtype = tf . int32 , initializer = 0 , trainable = False ) opt = tf . train . AdamOptimizer ( learning_rate ) regressor_vars = tf . contrib . framework . get_variables ( "gilbo/regressor" ) train_op = opt . minimize ( - info , var_list = regressor_vars ) # Initialize the variables we just created. uninitialized = plist ( tf . report_uninitialized_variables ( ) . eval ( ) ) uninitialized_vars = uninitialized . apply ( tf . contrib . framework . get_variables_by_name ) . _ [ 0 ] tf . variables_initializer ( uninitialized_vars ) . run ( ) saver = tf . train . Saver ( uninitialized_vars , max_to_keep = 1 ) try : checkpoint_path = tf . train . latest_checkpoint ( outdir ) saver . restore ( sess , checkpoint_path ) except ValueError : # Failing to restore just indicates that we don"t have a valid checkpoint, # so we will just start training a fresh GILBO model. pass _train_gilbo ( sess , gan , saver , learning_rate , gilbo_step , z_sample , avg_info , z_pred_dist , train_op , outdir , options ) mean_eval_info = _eval_gilbo ( sess , gan , z_sample , avg_info , dist_p1 , dist_p2 , fake_images , outdir , options ) # Collect encoded distributions on the training and eval set in order to do # kl-nearest-neighbors on generated samples and measure consistency. dataset = datasets . get_dataset ( dataset ) x_train = dataset . load_dataset ( split_name = "train" , num_threads = 1 ) x_train = x_train . batch ( gan . batch_size , drop_remainder = True ) x_train = x_train . make_one_shot_iterator ( ) . get_next ( ) [ 0 ] x_train = tf . reshape ( x_train , fake_images . shape ) x_eval = dataset . load_dataset ( split_name = "test" , num_threads = 1 ) x_eval = x_eval . batch ( gan . batch_size , drop_remainder = True ) x_eval = x_eval . make_one_shot_iterator ( ) . get_next ( ) [ 0 ] x_eval = tf . reshape ( x_eval , fake_images . shape ) mean_train_consistency = _run_gilbo_consistency ( x_train , "train" , extract_input_images = 0 , save_consistency_images = 20 , num_batches = 5 , * * locals ( ) ) mean_eval_consistency = _run_gilbo_consistency ( x_eval , "eval" , extract_input_images = 0 , save_consistency_images = 20 , num_batches = 5 , * * locals ( ) ) mean_self_consistency = _run_gilbo_consistency ( fake_images , "self" , extract_input_images = 20 , save_consistency_images = 20 , num_batches = 5 , * * locals ( ) ) return ( mean_eval_info , mean_train_consistency , mean_eval_consistency , mean_self_consistency )