(
    input_images, mode, dist_p1, dist_p2, z_pred_dist,
    z_sample, gan, sess, outdir, dataset, extract_input_images=0,
    save_consistency_images=0, num_batches=3000, **unused_kw)
return np.mean(consistency_skls)
Measure consistency of the gilbo estimator with the GAN or VAE.

  Arguments without documentation are variables from the calling function needed
  here. Pass them with **locals().

  Args:
    input_images: Tensor. Dataset images, or images generated by the GAN or VAE.
    mode: "train", "eval", or "self". Which consistency measure to compute.
    dist_p1:
    dist_p2:
    z_pred_dist:
    z_sample:
    gan:
    sess:
    outdir:
    dataset:
    extract_input_images: Number of batches to extract, -1 for all. Default: 0.
    save_consistency_images: Num batches to save, -1 for all. Default: 0.
    num_batches: Number of batches to run. Default: 3000.
    **unused_kw: Unused extra keyword args.

  Returns:
    Symmetric consistency KL. Additionally saves distribution parameters as a
    pickle as well as any requested images as pngs to outdir.
Measure consistency of the gilbo estimator with the GAN or VAE.
Measure consistency of the gilbo estimator with the GAN or VAE .
def _run_gilbo_consistency(
    input_images, mode, dist_p1, dist_p2, z_pred_dist,
    z_sample, gan, sess, outdir, dataset, extract_input_images=0,
    save_consistency_images=0, num_batches=3000, **unused_kw):
  """Measure consistency of the gilbo estimator with the GAN or VAE.

  Arguments without documentation are variables from the calling function needed
  here. Pass them with **locals().

  Args:
    input_images: Tensor. Dataset images, or images generated by the GAN or VAE.
    mode: "train", "eval", or "self". Which consistency measure to compute.
    dist_p1:
    dist_p2:
    z_pred_dist:
    z_sample:
    gan:
    sess:
    outdir:
    dataset:
    extract_input_images: Number of batches to extract, -1 for all. Default: 0.
    save_consistency_images: Num batches to save, -1 for all. Default: 0.
    num_batches: Number of batches to run. Default: 3000.
    **unused_kw: Unused extra keyword args.

  Returns:
    Symmetric consistency KL. Additionally saves distribution parameters as a
    pickle as well as any requested images as pngs to outdir.
  """
  with tf.variable_scope("gilbo"):
    with tf.variable_scope("regressor", reuse=True):
      z_pred_dist_train = _build_regressor(input_images, gan.z_dim)
      z_sample_train = z_pred_dist_train.sample()
    dist_p1_ph = tf.placeholder(tf.float32, dist_p1.shape)
    dist_p2_ph = tf.placeholder(tf.float32, dist_p2.shape)
    consist_dist_p1_ph = tf.placeholder(tf.float32, dist_p1.shape)
    consist_dist_p2_ph = tf.placeholder(tf.float32, dist_p2.shape)
    dist_p1 = z_pred_dist_train.distribution.distribution.concentration0
    dist_p2 = z_pred_dist_train.distribution.distribution.concentration1
    consist_z_dist_p1 = z_pred_dist.distribution.distribution.concentration0
    consist_z_dist_p2 = z_pred_dist.distribution.distribution.concentration1
    base_dist = ds.Beta
    kl_dist_p = ds.Independent(base_dist(dist_p1_ph, dist_p2_ph), 1)
    kl_dist_q = ds.Independent(
        base_dist(consist_dist_p1_ph, consist_dist_p2_ph), 1)
    consistency_kl = kl_dist_p.kl_divergence(kl_dist_q)
    consistency_rkl = kl_dist_q.kl_divergence(kl_dist_p)

  z_dist_p1s, z_dist_p2s = [], []
  consist_z_dist_p1s, consist_z_dist_p2s = [], []
  consistency_kls, consistency_rkls, consistency_skls = [], [], []

  i = 0
  while i < num_batches:
    try:
      samp = sess.run(z_sample)
      z_dist_p1, z_dist_p2, images, train_samp = sess.run(
          [dist_p1, dist_p2, input_images, z_sample_train],
          feed_dict={gan.z: samp})
      z_dist_p1s.append(z_dist_p1)
      z_dist_p2s.append(z_dist_p2)

      (consist_z_dist_p1_out, consist_z_dist_p2_out,
       consistency_images) = sess.run(
           [consist_z_dist_p1, consist_z_dist_p2, gan.fake_images],
           feed_dict={gan.z: train_samp})

      consist_z_dist_p1s.append(consist_z_dist_p1_out)
      consist_z_dist_p2s.append(consist_z_dist_p2_out)

      consist_kls, consist_rkls = sess.run(
          [consistency_kl, consistency_rkl],
          feed_dict={
              dist_p1_ph: z_dist_p1,
              dist_p2_ph: z_dist_p2,
              consist_dist_p1_ph: consist_z_dist_p1_out,
              consist_dist_p2_ph: consist_z_dist_p2_out,
          })

      consistency_kls.append(consist_kls)
      consistency_rkls.append(consist_rkls)
      consistency_skls.append((consist_kls + consist_rkls) / 2.0)

      if save_consistency_images:
        save_consistency_images -= 1

        filename = os.path.join(
            outdir,
            "consistency_image_%s_%06d_%06d.png"
            % (mode, i * gan.batch_size, (i + 1) * gan.batch_size - 1))
        img = consistency_images.reshape(
            [gan.batch_size * consistency_images.shape[1],
             consistency_images.shape[2],
             -1])
        _save_image(img, filename)

      if extract_input_images:
        extract_input_images -= 1

        if mode == "self":
          filename = os.path.join(
              outdir,
              "%s_image_%06d_%06d.png"
              % (mode, i * gan.batch_size, (i + 1) * gan.batch_size - 1))
          img = images.reshape(
              [gan.batch_size * consistency_images.shape[1],
               consistency_images.shape[2],
               -1])
          _save_image(img, filename)
        else:
          for j in range(gan.batch_size):
            filename = os.path.join(
                outdir, "..", dataset,
                "%s_image_%06d.png" % (mode, i * gan.batch_size + j))
            _save_image(images[j], filename)

      if i % 100 == 0:
        tf.logging.info(
            "%s: step:%d consistency KL:%3.1f" %
            (mode, i, np.mean(consistency_skls)))

      i += 1
    except tf.errors.OutOfRangeError:
      break

  out_dists = dict(
      dist_p1=np.reshape(z_dist_p1s, [-1, gan.batch_size]),
      dist_p2=np.reshape(z_dist_p2s, [-1, gan.batch_size]),
      consist_dist_p1=np.reshape(consist_z_dist_p1s, [-1, gan.batch_size]),
      consist_dist_p2=np.reshape(consist_z_dist_p2s, [-1, gan.batch_size]),
      consistency_kl=np.reshape(consistency_kls, [-1, gan.batch_size]),
      consistency_rkl=np.reshape(consistency_rkls, [-1, gan.batch_size]),
      consistency_skl=np.reshape(consistency_skls, [-1, gan.batch_size]),
  )
  with tf.gfile.Open(
      os.path.join(outdir, "%s_consistency_dists.p" % mode), "w") as f:
    pickle.dump(out_dists, f)

  return np.mean(consistency_skls)
def _run_gilbo_consistency ( input_images , mode , dist_p1 , dist_p2 , z_pred_dist , z_sample , gan , sess , outdir , dataset , extract_input_images = 0 , save_consistency_images = 0 , num_batches = 3000 , * * unused_kw ) : with tf . variable_scope ( "gilbo" ) : with tf . variable_scope ( "regressor" , reuse = True ) : z_pred_dist_train = _build_regressor ( input_images , gan . z_dim ) z_sample_train = z_pred_dist_train . sample ( ) dist_p1_ph = tf . placeholder ( tf . float32 , dist_p1 . shape ) dist_p2_ph = tf . placeholder ( tf . float32 , dist_p2 . shape ) consist_dist_p1_ph = tf . placeholder ( tf . float32 , dist_p1 . shape ) consist_dist_p2_ph = tf . placeholder ( tf . float32 , dist_p2 . shape ) dist_p1 = z_pred_dist_train . distribution . distribution . concentration0 dist_p2 = z_pred_dist_train . distribution . distribution . concentration1 consist_z_dist_p1 = z_pred_dist . distribution . distribution . concentration0 consist_z_dist_p2 = z_pred_dist . distribution . distribution . concentration1 base_dist = ds . Beta kl_dist_p = ds . Independent ( base_dist ( dist_p1_ph , dist_p2_ph ) , 1 ) kl_dist_q = ds . Independent ( base_dist ( consist_dist_p1_ph , consist_dist_p2_ph ) , 1 ) consistency_kl = kl_dist_p . kl_divergence ( kl_dist_q ) consistency_rkl = kl_dist_q . kl_divergence ( kl_dist_p ) z_dist_p1s , z_dist_p2s = [ ] , [ ] consist_z_dist_p1s , consist_z_dist_p2s = [ ] , [ ] consistency_kls , consistency_rkls , consistency_skls = [ ] , [ ] , [ ] i = 0 while i < num_batches : try : samp = sess . run ( z_sample ) z_dist_p1 , z_dist_p2 , images , train_samp = sess . run ( [ dist_p1 , dist_p2 , input_images , z_sample_train ] , feed_dict = { gan . z : samp } ) z_dist_p1s . append ( z_dist_p1 ) z_dist_p2s . append ( z_dist_p2 ) ( consist_z_dist_p1_out , consist_z_dist_p2_out , consistency_images ) = sess . run ( [ consist_z_dist_p1 , consist_z_dist_p2 , gan . fake_images ] , feed_dict = { gan . z : train_samp } ) consist_z_dist_p1s . append ( consist_z_dist_p1_out ) consist_z_dist_p2s . append ( consist_z_dist_p2_out ) consist_kls , consist_rkls = sess . run ( [ consistency_kl , consistency_rkl ] , feed_dict = { dist_p1_ph : z_dist_p1 , dist_p2_ph : z_dist_p2 , consist_dist_p1_ph : consist_z_dist_p1_out , consist_dist_p2_ph : consist_z_dist_p2_out , } ) consistency_kls . append ( consist_kls ) consistency_rkls . append ( consist_rkls ) consistency_skls . append ( ( consist_kls + consist_rkls ) / 2.0 ) if save_consistency_images : save_consistency_images -= 1 filename = os . path . join ( outdir , "consistency_image_%s_%06d_%06d.png" % ( mode , i * gan . batch_size , ( i + 1 ) * gan . batch_size - 1 ) ) img = consistency_images . reshape ( [ gan . batch_size * consistency_images . shape [ 1 ] , consistency_images . shape [ 2 ] , - 1 ] ) _save_image ( img , filename ) if extract_input_images : extract_input_images -= 1 if mode == "self" : filename = os . path . join ( outdir , "%s_image_%06d_%06d.png" % ( mode , i * gan . batch_size , ( i + 1 ) * gan . batch_size - 1 ) ) img = images . reshape ( [ gan . batch_size * consistency_images . shape [ 1 ] , consistency_images . shape [ 2 ] , - 1 ] ) _save_image ( img , filename ) else : for j in range ( gan . batch_size ) : filename = os . path . join ( outdir , ".." , dataset , "%s_image_%06d.png" % ( mode , i * gan . batch_size + j ) ) _save_image ( images [ j ] , filename ) if i % 100 == 0 : tf . logging . info ( "%s: step:%d consistency KL:%3.1f" % ( mode , i , np . mean ( consistency_skls ) ) ) i += 1 except tf . errors . OutOfRangeError : break out_dists = dict ( dist_p1 = np . reshape ( z_dist_p1s , [ - 1 , gan . batch_size ] ) , dist_p2 = np . reshape ( z_dist_p2s , [ - 1 , gan . batch_size ] ) , consist_dist_p1 = np . reshape ( consist_z_dist_p1s , [ - 1 , gan . batch_size ] ) , consist_dist_p2 = np . reshape ( consist_z_dist_p2s , [ - 1 , gan . batch_size ] ) , consistency_kl = np . reshape ( consistency_kls , [ - 1 , gan . batch_size ] ) , consistency_rkl = np . reshape ( consistency_rkls , [ - 1 , gan . batch_size ] ) , consistency_skl = np . reshape ( consistency_skls , [ - 1 , gan . batch_size ] ) , ) with tf . gfile . Open ( os . path . join ( outdir , "%s_consistency_dists.p" % mode ) , "w" ) as f : pickle . dump ( out_dists , f ) return np . mean ( consistency_skls )