class_docstr: Variational inference with the KL divergence
  
  $\text{KL}( q(z; \lambda) \| p(z \mid x) ).$
  
  This class minimizes the objective by automatically selecting from a
  variety of black box inference techniques.
  
  #### Notes
  
  `KLqp` also optimizes any model parameters $p(z \mid x;
  \theta)$. It does this by variational EM, maximizing
  
  $\mathbb{E}_{q(z; \lambda)} [ \log p(x, z; \theta) ]$
  
  with respect to $\theta$.
  
  In conditional inference, we infer $z$ in $p(z, \beta
  \mid x)$ while fixing inference over $\beta$ using another
  distribution $q(\beta)$. During gradient calculation, instead
  of using the model's density
  
  $\log p(x, z^{(s)}), z^{(s)} \sim q(z; \lambda),$
  
  for each sample $s=1,\ldots,S$, `KLqp` uses
  
  $\log p(x, z^{(s)}, \beta^{(s)}),$
  
  where $z^{(s)} \sim q(z; \lambda)$ and $\beta^{(s)}
  \sim q(\beta)$.
  
  The objective function also adds to itself a summation over all
  tensors in the `REGULARIZATION_LOSSES` collection.
class_docstr_tok: ["Variational","inference","with","the","KL","divergence","$","\\text","{","KL","}","(","q","(","z",";","\\lambda",")","\\|","p","(","z","\\mid","x",")",")",".","$","This","class","minimizes","the","objective","by","automatically","selecting","from","a","variety","of","black","box","inference","techniques",".","#","#","#","#","Notes","`","KLqp","`","also","optimizes","any","model","parameters","$","p","(","z","\\mid","x",";","\\theta",")","$",".","It","does","this","by","variational","EM",",","maximizing","$","\\mathbb","{","E","}","_","{","q","(","z",";","\\lambda",")","}","[","\\log","p","(","x",",","z",";","\\theta",")","]","$","with","respect","to","$","\\theta","$",".","In","conditional","inference",",","we","infer","$","z","$","in","$","p","(","z",",","\\beta","\\mid","x",")","$","while","fixing","inference","over","$","\\beta","$","using","another","distribution","$","q","(","\\beta",")","$",".","During","gradient","calculation",",","instead","of","using","the","model","'s","density","$","\\log","p","(","x",",","z^","{","(","s",")","}",")",",","z^","{","(","s",")","}","\\sim","q","(","z",";","\\lambda",")",",","$","for","each","sample","$","s=1",",","\\ldots",",","S","$",",","`","KLqp","`","uses","$","\\log","p","(","x",",","z^","{","(","s",")","}",",","\\beta^","{","(","s",")","}",")",",","$","where","$","z^","{","(","s",")","}","\\sim","q","(","z",";","\\lambda",")","$","and","$","\\beta^","{","(","s",")","}","\\sim","q","(","\\beta",")","$",".","The","objective","function","also","adds","to","itself","a","summation","over","all","tensors","in","the","`","REGULARIZATION_LOSSES","`","collection","."]
docstr: Wrapper for the `KLqp` loss function.
  
  $-\text{ELBO} =
      -\mathbb{E}_{q(z; \lambda)} [ \log p(x, z) - \log q(z; \lambda) ]$
  
  KLqp supports
  
  1. score function gradients [@paisley2012variational]
  2. reparameterization gradients [@kingma2014auto]
  
  of the loss function.
  
  If the KL divergence between the variational model and the prior
  is tractable, then the loss function can be written as
  
  $-\mathbb{E}_{q(z; \lambda)}[\log p(x \mid z)] +
      \text{KL}( q(z; \lambda) \| p(z) ),$
  
  where the KL term is computed analytically [@kingma2014auto]. We
  compute this automatically when $p(z)$ and $q(z; \lambda)$ are
  Normal.
code: def build_loss_and_gradients(self, var_list):
      """Wrapper for the `KLqp` loss function.
  
      $-\\text{ELBO} =
          -\mathbb{E}_{q(z; \lambda)} [ \log p(x, z) - \log q(z; \lambda) ]$
  
      KLqp supports
  
      1. score function gradients [@paisley2012variational]
      2. reparameterization gradients [@kingma2014auto]
  
      of the loss function.
  
      If the KL divergence between the variational model and the prior
      is tractable, then the loss function can be written as
  
      $-\mathbb{E}_{q(z; \lambda)}[\log p(x \mid z)] +
          \\text{KL}( q(z; \lambda) \| p(z) ),$
  
      where the KL term is computed analytically [@kingma2014auto]. We
      compute this automatically when $p(z)$ and $q(z; \lambda)$ are
      Normal.
      """
      is_reparameterizable = all(
          [
              rv.reparameterization_type
              == tf.contrib.distributions.FULLY_REPARAMETERIZED
              for rv in six.itervalues(self.latent_vars)
          ]
      )
      is_analytic_kl = all(
          [
              isinstance(z, Normal) and isinstance(qz, Normal)
              for z, qz in six.iteritems(self.latent_vars)
          ]
      )
      if not is_analytic_kl and self.kl_scaling:
          raise TypeError(
              "kl_scaling must be None when using non-analytic KL term"
          )
      if is_reparameterizable:
          if is_analytic_kl:
              return build_reparam_kl_loss_and_gradients(self, var_list)
          # elif is_analytic_entropy:
          #    return build_reparam_entropy_loss_and_gradients(self, var_list)
          else:
              return build_reparam_loss_and_gradients(self, var_list)
      else:
          # Prefer Rao-Blackwellization over analytic KL. Unknown what
          # would happen stability-wise if the two are combined.
          # if is_analytic_kl:
          #   return build_score_kl_loss_and_gradients(self, var_list)
          # Analytic entropies may lead to problems around
          # convergence; for now it is deactivated.
          # elif is_analytic_entropy:
          #    return build_score_entropy_loss_and_gradients(self, var_list)
          # else:
          return build_score_rb_loss_and_gradients(self, var_list)
  
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
  from edward.util import copy, get_descendants
label_desc: Use image node_id 3 for calling the KLqp obj's underlying member method code with example usage: obj.build_loss_and_gradients(var_list) and returns: build_score_rb_loss_and_gradients, build_reparam_kl_loss_and_gradients, build_reparam_loss_and_gradients