code: def compute_perplexity(model, sess, name):
      """Compute perplexity of the output of the model.
  
      Args:
        model: model for compute perplexity.
        sess: tensorflow session to use.
        name: name of the batch.
  
      Returns:
        The perplexity of the eval outputs.
      """
      total_loss = 0
      total_predict_count = 0
      start_time = time.time()
  
      while True:
          try:
              output_tuple = model.eval(sess)
              total_loss += (
                  output_tuple.eval_loss * output_tuple.batch_size
              )
              total_predict_count += output_tuple.predict_count
          except tf.errors.OutOfRangeError:
              break
  
      perplexity = utils.safe_exp(total_loss / total_predict_count)
      utils.print_time(
          "  eval %s: perplexity %.2f" % (name, perplexity), start_time
      )
      return perplexity
  
imports: from __future__ import print_function
  import collections
  import os
  import time
  import numpy
  import six
  import tensorflow
  from tensorflow.python.ops import lookup_ops
  from .utils import misc_utils, vocab_utils, iterator_utils
label_desc: Use image node_id 21 for calling a global function with example usage: compute_perplexity(model, sess, name) and returns: perplexity