code: def perplexity(test_data, predictions, topics, vocabulary):
      """
      Compute the perplexity of a set of test documents given a set
      of predicted topics.
  
      Let theta be the matrix of document-topic probabilities, where
      theta_ik = p(topic k | document i). Let Phi be the matrix of term-topic
      probabilities, where phi_jk = p(word j | topic k).
  
      Then for each word in each document, we compute for a given word w
      and document d
  
      .. math::
          p(word | \theta[doc_id,:], \phi[word_id,:]) =
         \sum_k \theta[doc_id, k] * \phi[word_id, k]
  
      We compute loglikelihood to be:
  
      .. math::
          l(D) = \sum_{i \in D} \sum_{j in D_i} count_{i,j} * log Pr(word_{i,j} | \theta, \phi)
  
      and perplexity to be
  
      .. math::
          \exp \{ - l(D) / \sum_i \sum_j count_{i,j} \}
  
      Parameters
      ----------
      test_data : SArray of type dict or SFrame with a single column of type dict
          Documents in bag-of-words format.
  
      predictions : SArray
          An SArray of vector type, where each vector contains estimates of the
          probability that this document belongs to each of the topics.
          This must have the same size as test_data; otherwise an exception
          occurs. This can be the output of
          :py:func:`~turicreate.topic_model.TopicModel.predict`, for example.
  
      topics : SFrame
          An SFrame containing two columns: 'vocabulary' and 'topic_probabilities'.
          The value returned by m['topics'] is a valid input for this argument,
          where m is a trained :py:class:`~turicreate.topic_model.TopicModel`.
  
      vocabulary : SArray
          An SArray of words to use. All words in test_data that are not in this
          vocabulary will be ignored.
  
      Notes
      -----
      For more details, see equations 13-16 of [PattersonTeh2013].
  
  
      References
      ----------
      .. [PERP] `Wikipedia - perplexity <http://en.wikipedia.org/wiki/Perplexity>`_
  
      .. [PattersonTeh2013] Patterson, Teh. `"Stochastic Gradient Riemannian
         Langevin Dynamics on the Probability Simplex"
         <http://www.stats.ox.ac.uk/~teh/research/compstats/PatTeh2013a.pdf>`_
         NIPS, 2013.
  
      Examples
      --------
      >>> from turicreate import topic_model
      >>> train_data, test_data = turicreate.text_analytics.random_split(docs)
      >>> m = topic_model.create(train_data)
      >>> pred = m.predict(train_data)
      >>> topics = m['topics']
      >>> p = topic_model.perplexity(test_data, pred,
                                     topics['topic_probabilities'],
                                     topics['vocabulary'])
      >>> p
      1720.7  # lower values are better
      """
      test_data = _check_input(test_data)
      assert isinstance(
          predictions, _SArray
      ), "Predictions must be an SArray of vector type."
      assert predictions.dtype == _array.array, (
          "Predictions must be probabilities. Try using m.predict() with "
          + "output_type='probability'."
      )
  
      opts = {
          "test_data": test_data,
          "predictions": predictions,
          "topics": topics,
          "vocabulary": vocabulary,
      }
      response = _turicreate.extensions._text.topicmodel_get_perplexity(
          opts
      )
      return response["perplexity"]
  
imports: from __future__ import print_function
  from __future__ import division
  from __future__ import absolute_import
  import turicreate
  from turicreate.toolkits._model import Model
  from turicreate.data_structures.sframe import SFrame
  from turicreate.data_structures.sarray import SArray
  from turicreate.toolkits.text_analytics._util import _check_input
  from turicreate.toolkits.text_analytics._util import random_split
  from turicreate.toolkits._internal_utils import _check_categorical_option_type, _precomputed_field, _toolkit_repr_print
  import sys
  import operator
  import array
label_desc: Use image node_id 2 for calling a global function with example usage: perplexity(test_data, predictions, topics, vocabulary) and returns: response