code: def main(_):
      ed.set_seed(42)
  
      # DATA
      x_train, _, x_test = text8(FLAGS.data_dir)
      vocab = string.ascii_lowercase + " "
      vocab_size = len(vocab)
      encoder = dict(zip(vocab, range(vocab_size)))
      decoder = {v: k for k, v in encoder.items()}
  
      data = generator(
          x_train, FLAGS.batch_size, FLAGS.timesteps, encoder
      )
  
      # MODEL
      x_ph = tf.placeholder(tf.int32, [None, FLAGS.timesteps])
      with tf.variable_scope("language_model"):
          # Shift input sequence to right by 1, [0, x[0], ..., x[timesteps - 2]].
          x_ph_shift = tf.pad(x_ph, [[0, 0], [1, 0]])[:, :-1]
          x = language_model(x_ph_shift, vocab_size)
  
      with tf.variable_scope("language_model", reuse=True):
          x_gen = language_model_gen(5, vocab_size)
  
      imb = range(0, len(x_test) - FLAGS.timesteps, FLAGS.timesteps)
      encoded_x_test = np.asarray(
          [
              [encoder[c] for c in x_test[i : (i + FLAGS.timesteps)]]
              for i in imb
          ],
          dtype=np.int32,
      )
      test_size = encoded_x_test.shape[0]
      print("Test set shape: {}".format(encoded_x_test.shape))
      test_nll = -tf.reduce_sum(x.log_prob(x_ph))
  
      # INFERENCE
      inference = ed.MAP({}, {x: x_ph})
  
      optimizer = tf.train.AdamOptimizer(learning_rate=FLAGS.lr)
      inference.initialize(
          optimizer=optimizer, logdir=FLAGS.log_dir, log_timestamp=False
      )
  
      print(
          "Number of sets of parameters: {}".format(
              len(tf.trainable_variables())
          )
      )
      print(
          "Number of parameters: {}".format(
              np.sum(
                  [
                      np.prod(v.shape.as_list())
                      for v in tf.trainable_variables()
                  ]
              )
          )
      )
      for v in tf.trainable_variables():
          print(v)
  
      sess = ed.get_session()
      tf.global_variables_initializer().run()
  
      # Double n_epoch and print progress every half an epoch.
      n_iter_per_epoch = len(x_train) // (
          FLAGS.batch_size * FLAGS.timesteps * 2
      )
      epoch = 0.0
      for _ in range(FLAGS.n_epoch * 2):
          epoch += 0.5
          print("Epoch: {0}".format(epoch))
          avg_nll = 0.0
  
          pbar = Progbar(n_iter_per_epoch)
          for t in range(1, n_iter_per_epoch + 1):
              pbar.update(t)
              x_batch = next(data)
              info_dict = inference.update({x_ph: x_batch})
              avg_nll += info_dict["loss"]
  
          # Print average bits per character over epoch.
          avg_nll /= (
              n_iter_per_epoch
              * FLAGS.batch_size
              * FLAGS.timesteps
              * np.log(2)
          )
          print("Train average bits/char: {:0.8f}".format(avg_nll))
  
          # Print per-data point log-likelihood on test set.
          avg_nll = 0.0
          for start in range(0, test_size, batch_size):
              end = min(test_size, start + batch_size)
              x_batch = encoded_x_test[start:end]
              avg_nll += sess.run(test_nll, {x_ph: x_batch})
  
          avg_nll /= test_size
          print("Test average NLL: {:0.8f}".format(avg_nll))
  
          # Generate samples from model.
          samples = sess.run(x_gen)
          samples = [
              "".join([decoder[c] for c in sample])
              for sample in samples
          ]
          print("Samples:")
          for sample in samples:
              print(sample)
  
file_path: edward/examples/lstm.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import edward
  import numpy
  import os
  import string
  import tensorflow
  from datetime import datetime
  from edward.models import Categorical
  from edward.util import Progbar
  from observations import text8
label_desc: Use image node_id 5 for calling a global function with example usage: main(_) without return types