code: def main(_):
      def ratio_estimator(data, local_vars, global_vars):
          """Takes as input a dict of data x, local variable samples z, and
          global variable samples beta; outputs real values of shape
          (x.shape[0] + z.shape[0],). In this example, there are no local
          variables.
          """
          # data[y] has shape (M,); global_vars[w] has shape (D,)
          # we concatenate w to each data point y, so input has shape (M, 1 + D)
          input = tf.concat(
              [
                  tf.reshape(data[y], [FLAGS.M, 1]),
                  tf.tile(
                      tf.reshape(global_vars[w], [1, FLAGS.D]),
                      [FLAGS.M, 1],
                  ),
              ],
              1,
          )
          hidden = tf.layers.dense(input, 64, activation=tf.nn.relu)
          output = tf.layers.dense(hidden, 1, activation=None)
          return output
  
      ed.set_seed(42)
  
      # DATA
      w_true = np.ones(FLAGS.D) * 5.0
      X_train, y_train = build_toy_dataset(FLAGS.N, w_true)
      X_test, y_test = build_toy_dataset(FLAGS.N, w_true)
      data = generator([X_train, y_train], FLAGS.M)
  
      # MODEL
      X = tf.placeholder(tf.float32, [FLAGS.M, FLAGS.D])
      y_ph = tf.placeholder(tf.float32, [FLAGS.M])
      w = Normal(loc=tf.zeros(FLAGS.D), scale=tf.ones(FLAGS.D))
      y = Normal(loc=ed.dot(X, w), scale=tf.ones(FLAGS.M))
  
      # INFERENCE
      qw = Normal(
          loc=tf.get_variable("qw/loc", [FLAGS.D]) + 1.0,
          scale=tf.nn.softplus(tf.get_variable("qw/scale", [FLAGS.D])),
      )
  
      inference = ed.ImplicitKLqp(
          {w: qw},
          data={y: y_ph},
          discriminator=ratio_estimator,
          global_vars={w: qw},
      )
      inference.initialize(
          n_iter=5000, n_print=100, scale={y: float(FLAGS.N) / FLAGS.M}
      )
  
      sess = ed.get_session()
      tf.global_variables_initializer().run()
  
      for _ in range(inference.n_iter):
          X_batch, y_batch = next(data)
          for _ in range(5):
              info_dict_d = inference.update(
                  variables="Disc",
                  feed_dict={X: X_batch, y_ph: y_batch},
              )
  
          info_dict = inference.update(
              variables="Gen", feed_dict={X: X_batch, y_ph: y_batch}
          )
          info_dict["loss_d"] = info_dict_d["loss_d"]
          info_dict["t"] = (
              info_dict["t"] // 6
          )  # say set of 6 updates is 1 iteration
  
          t = info_dict["t"]
          inference.print_progress(info_dict)
          if t == 1 or t % inference.n_print == 0:
              # Check inferred posterior parameters.
              mean, std = sess.run([qw.mean(), qw.stddev()])
              print("\nInferred mean & std:")
              print(mean)
              print(std)
  
file_path: edward/examples/bayesian_linear_regression_implicitklqp.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import edward
  import numpy
  import tensorflow
  from edward.models import Normal
label_desc: Use image node_id 3 for calling a global function with example usage: main(_) and returns: output