class_docstr: Maximum a posteriori.
  
  This class implements gradient-based optimization to solve the
  optimization problem,
  
  $\min_{z} - p(z \mid x).$
  
  This is equivalent to using a `PointMass` variational distribution
  and minimizing the unnormalized objective,
  
  $- \mathbb{E}_{q(z; \lambda)} [ \log p(x, z) ].$
  
  #### Notes
  
  This class is currently restricted to optimization over
  differentiable latent variables. For example, it does not solve
  discrete optimization.
  
  This class also minimizes the loss with respect to any model
  parameters $p(z \mid x; \theta)$.
  
  In conditional inference, we infer $z$ in $p(z, \beta
  \mid x)$ while fixing inference over $\beta$ using another
  distribution $q(\beta)$. `MAP` optimizes
  $\mathbb{E}_{q(\beta)} [ \log p(x, z, \beta) ]$, leveraging
  a single Monte Carlo sample, $\log p(x, z, \beta^*)$, where
  $\beta^* \sim q(\beta)$. This is a lower bound to the
  marginal density $\log p(x, z)$, and it is exact if
  $q(\beta) = p(\beta \mid x)$ (up to stochasticity).
  
  #### Examples
  
  Most explicitly, `MAP` is specified via a dictionary:
  
  ```python
  qpi = PointMass(params=ed.to_simplex(tf.Variable(tf.zeros(K-1))))
  qmu = PointMass(params=tf.Variable(tf.zeros(K*D)))
  qsigma = PointMass(params=tf.nn.softplus(tf.Variable(tf.zeros(K*D))))
  ed.MAP({pi: qpi, mu: qmu, sigma: qsigma}, data)
  ```
  
  We also automate the specification of `PointMass` distributions,
  so one can pass in a list of latent variables instead:
  
  ```python
  ed.MAP([beta], data)
  ed.MAP([pi, mu, sigma], data)
  ```
  
  Note that for `MAP` to optimize over latent variables with
  constrained continuous support, the point mass must be constrained
  to have the same support while its free parameters are
  unconstrained; see, e.g., `qsigma` above. This is different than
  performing MAP on the unconstrained space: in general, the MAP of
  the transform is not the transform of the MAP.
  
  The objective function also adds to itself a summation over all
  tensors in the `REGULARIZATION_LOSSES` collection.
docstr: Build loss function. Its automatic differentiation
  is the gradient of
  
  $- \log p(x,z).$
code: def build_loss_and_gradients(self, var_list):
      """Build loss function. Its automatic differentiation
      is the gradient of
  
      $- \log p(x,z).$
      """
      # Form dictionary in order to replace conditioning on prior or
      # observed variable with conditioning on a specific value.
      scope = tf.get_default_graph().unique_name("inference")
      dict_swap = {
          z: qz.value() for z, qz in six.iteritems(self.latent_vars)
      }
      for x, qx in six.iteritems(self.data):
          if isinstance(x, RandomVariable):
              if isinstance(qx, RandomVariable):
                  dict_swap[x] = qx.value()
              else:
                  dict_swap[x] = qx
  
      p_log_prob = 0.0
      for z in six.iterkeys(self.latent_vars):
          z_copy = copy(z, dict_swap, scope=scope)
          p_log_prob += tf.reduce_sum(
              self.scale.get(z, 1.0) * z_copy.log_prob(dict_swap[z])
          )
  
      for x in six.iterkeys(self.data):
          if isinstance(x, RandomVariable):
              if dict_swap:
                  x_copy = copy(x, dict_swap, scope=scope)
              else:
                  x_copy = x
              p_log_prob += tf.reduce_sum(
                  self.scale.get(x, 1.0) * x_copy.log_prob(dict_swap[x])
              )
  
      reg_penalty = tf.reduce_sum(tf.losses.get_regularization_losses())
      loss = -p_log_prob + reg_penalty
  
      grads = tf.gradients(loss, var_list)
      grads_and_vars = list(zip(grads, var_list))
      return loss, grads_and_vars
  
file_path: edward/edward/inferences/map.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import six
  import tensorflow
  from edward.inferences.variational_inference import VariationalInference
  from edward.models import RandomVariable, PointMass
  from edward.util import copy, transform
label_desc: Use image node_id 2 for calling the MAP obj's underlying member method code with example usage: obj.build_loss_and_gradients(var_list) and returns: loss,  grads_and_vars