code: def main(_):
      ed.set_seed(42)
  
      # DATA
      (x_train, _), (x_test, _) = mnist(FLAGS.data_dir)
      x_train = x_train[: FLAGS.N]
  
      # MODEL
      z = Normal(
          loc=tf.zeros([FLAGS.N, FLAGS.d]),
          scale=tf.ones([FLAGS.N, FLAGS.d]),
      )
      logits = generative_network(z)
      x = Bernoulli(logits=logits)
  
      # INFERENCE
      T = FLAGS.n_iter_per_epoch * FLAGS.n_epoch
      qz = Empirical(
          params=tf.get_variable("qz/params", [T, FLAGS.N, FLAGS.d])
      )
  
      inference_e = ed.HMC({z: qz}, data={x: x_train})
      inference_e.initialize()
  
      inference_m = ed.MAP(
          data={x: x_train, z: qz.params[inference_e.t]}
      )
      optimizer = tf.train.AdamOptimizer(0.01, epsilon=1.0)
      inference_m.initialize(optimizer=optimizer)
  
      tf.global_variables_initializer().run()
  
      for _ in range(FLAGS.n_epoch - 1):
          avg_loss = 0.0
          for _ in range(FLAGS.n_iter_per_epoch):
              info_dict_e = inference_e.update()
              info_dict_m = inference_m.update()
              avg_loss += info_dict_m["loss"]
              inference_e.print_progress(info_dict_e)
  
          # Print a lower bound to the average marginal likelihood for an
          # image.
          avg_loss = avg_loss / FLAGS.n_iter_per_epoch
          avg_loss = avg_loss / FLAGS.N
          print("\nlog p(x) >= {:0.3f}".format(avg_loss))
  
          # Prior predictive check.
          images = x.eval()
          for m in range(FLAGS.N):
              imsave(
                  os.path.join(FLAGS.out_dir, "%d.png") % m,
                  images[m].reshape(28, 28),
              )
  
file_path: edward/examples/factor_analysis.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import edward
  import os
  import tensorflow
  from edward.models import Bernoulli, Empirical, Normal
  from observations import mnist
  from scipy.misc import imsave
label_desc: Use image node_id 2 for calling a global function with example usage: main(_) without return types