code: def main(_):
      ed.set_seed(42)
  
      # DATA
      x_data = build_toy_dataset(FLAGS.N)
  
      # MODEL
      pi = Dirichlet(concentration=tf.ones(FLAGS.K))
      mu = Normal(0.0, 1.0, sample_shape=[FLAGS.K, FLAGS.D])
      sigma = InverseGamma(
          concentration=1.0, rate=1.0, sample_shape=[FLAGS.K, FLAGS.D]
      )
      c = Categorical(
          logits=tf.log(pi) - tf.log(1.0 - pi), sample_shape=FLAGS.N
      )
      x = Normal(loc=tf.gather(mu, c), scale=tf.gather(sigma, c))
  
      # INFERENCE
      qpi = Empirical(
          params=tf.get_variable(
              "qpi/params",
              [FLAGS.T, FLAGS.K],
              initializer=tf.constant_initializer(1.0 / FLAGS.K),
          )
      )
      qmu = Empirical(
          params=tf.get_variable(
              "qmu/params",
              [FLAGS.T, FLAGS.K, FLAGS.D],
              initializer=tf.zeros_initializer(),
          )
      )
      qsigma = Empirical(
          params=tf.get_variable(
              "qsigma/params",
              [FLAGS.T, FLAGS.K, FLAGS.D],
              initializer=tf.ones_initializer(),
          )
      )
      qc = Empirical(
          params=tf.get_variable(
              "qc/params",
              [FLAGS.T, FLAGS.N],
              initializer=tf.zeros_initializer(),
              dtype=tf.int32,
          )
      )
  
      gpi = Dirichlet(concentration=tf.constant([1.4, 1.6]))
      gmu = Normal(
          loc=tf.constant([[1.0, 1.0], [-1.0, -1.0]]),
          scale=tf.constant([[0.5, 0.5], [0.5, 0.5]]),
      )
      gsigma = InverseGamma(
          concentration=tf.constant([[1.1, 1.1], [1.1, 1.1]]),
          rate=tf.constant([[1.0, 1.0], [1.0, 1.0]]),
      )
      gc = Categorical(logits=tf.zeros([FLAGS.N, FLAGS.K]))
  
      inference = ed.MetropolisHastings(
          latent_vars={pi: qpi, mu: qmu, sigma: qsigma, c: qc},
          proposal_vars={pi: gpi, mu: gmu, sigma: gsigma, c: gc},
          data={x: x_data},
      )
  
      inference.initialize()
  
      sess = ed.get_session()
      tf.global_variables_initializer().run()
  
      for _ in range(inference.n_iter):
          info_dict = inference.update()
          inference.print_progress(info_dict)
  
          t = info_dict["t"]
          if t == 1 or t % inference.n_print == 0:
              qpi_mean, qmu_mean = sess.run([qpi.mean(), qmu.mean()])
              print("")
              print("Inferred membership probabilities:")
              print(qpi_mean)
              print("Inferred cluster means:")
              print(qmu_mean)
  
file_path: edward/examples/mixture_gaussian_mh.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import edward
  import numpy
  import six
  import tensorflow
  from edward.models import Categorical, Dirichlet, Empirical, InverseGamma, Normal
  from scipy.stats import norm
label_desc: Use image node_id 2 for calling a global function with example usage: main(_) without return types