code: def main(_):
      ed.set_seed(42)
  
      # DATA
      (x_train, _), (x_test, _), (x_valid, _) = caltech101_silhouettes(
          FLAGS.data_dir
      )
      x_train_generator = generator(x_train, FLAGS.batch_size)
      x_ph = tf.placeholder(tf.int32, [None, 28 * 28])
  
      # MODEL
      zs = [0] * len(FLAGS.hidden_sizes)
      for l in reversed(range(len(FLAGS.hidden_sizes))):
          if l == len(FLAGS.hidden_sizes) - 1:
              logits = tf.zeros(
                  [tf.shape(x_ph)[0], FLAGS.hidden_sizes[l]]
              )
          else:
              logits = tf.layers.dense(
                  tf.cast(zs[l + 1], tf.float32),
                  FLAGS.hidden_sizes[l],
                  activation=None,
              )
          zs[l] = Bernoulli(logits=logits)
  
      x = Bernoulli(
          logits=tf.layers.dense(
              tf.cast(zs[0], tf.float32), 28 * 28, activation=None
          )
      )
  
      # INFERENCE
      # Define variational model with reverse ordering as probability model:
      # if p is 15-100-300 from top-down, q is 300-100-15 from bottom-up.
      qzs = [0] * len(FLAGS.hidden_sizes)
      for l in range(len(FLAGS.hidden_sizes)):
          if l == 0:
              logits = tf.layers.dense(
                  tf.cast(x_ph, tf.float32),
                  FLAGS.hidden_sizes[l],
                  activation=None,
              )
          else:
              logits = tf.layers.dense(
                  tf.cast(qzs[l - 1], tf.float32),
                  FLAGS.hidden_sizes[l],
                  activation=None,
              )
          qzs[l] = Bernoulli(logits=logits)
  
      inference = ed.KLqp(
          {z: qz for z, qz in zip(zs, qzs)}, data={x: x_ph}
      )
      optimizer = tf.train.AdamOptimizer(FLAGS.step_size)
      inference.initialize(
          optimizer=optimizer, n_samples=FLAGS.n_train_samples
      )
  
      # Build tensor for log-likelihood given one variational sample to run
      # on test data.
      x_post = ed.copy(x, {z: qz for z, qz in zip(zs, qzs)})
      x_neg_log_prob = -tf.reduce_sum(x_post.log_prob(x_ph)) / tf.cast(
          tf.shape(x_ph)[0], tf.float32
      )
  
      sess = ed.get_session()
      tf.global_variables_initializer().run()
  
      for epoch in range(FLAGS.n_epoch):
          print("Epoch {}".format(epoch))
          train_loss = 0.0
  
          pbar = Progbar(FLAGS.n_iter_per_epoch)
          for t in range(1, FLAGS.n_iter_per_epoch + 1):
              pbar.update(t)
              x_batch = next(x_train_generator)
              info_dict = inference.update(feed_dict={x_ph: x_batch})
              train_loss += info_dict["loss"]
  
          # Print per-data point loss, averaged over training epoch.
          train_loss /= FLAGS.n_iter_per_epoch
          train_loss /= FLAGS.batch_size
          print(
              "Training negative log-likelihood: {:0.3f}".format(
                  train_loss
              )
          )
  
          test_loss = [
              sess.run(x_neg_log_prob, {x_ph: x_test})
              for _ in range(FLAGS.n_test_samples)
          ]
          test_loss = np.mean(test_loss)
          print(
              "Test negative log-likelihood: {:0.3f}".format(test_loss)
          )
  
          # Prior predictive check.
          images = sess.run(
              x, {x_ph: x_batch}
          )  # feed ph to determine sample size
          for m in range(FLAGS.batch_size):
              imsave(
                  "{}/{}.png".format(out_dir, m),
                  images[m].reshape(28, 28),
              )
  
file_path: edward/examples/sigmoid_belief_network.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import edward
  import numpy
  import os
  import tensorflow
  from edward.models import Bernoulli
  from edward.util import Progbar
  from observations import caltech101_silhouettes
  from scipy.misc import imsave
label_desc: Use image node_id 2 for calling a global function with example usage: main(_) without return types