class_docstr: Importance-weighted variational inference.
  
  Uses importance sampling to produce an improved lower bound on the
  log marginal likelihood. It is the core idea behind
  importance-weighted autoencoders (Burda et al. (2016)).
class_docstr_tok: ["Importance-weighted","variational","inference",".","Uses","importance","sampling","to","produce","an","improved","lower","bound","on","the","log","marginal","likelihood",".","It","is","the","core","idea","behind","importance-weighted","autoencoders","(","Burda","et","al",".","(","2016",")",")","."]
docstr: Build loss function. Its automatic differentiation
  is a stochastic gradient of
  
  $-\mathbb{E}_{q(z^1; \lambda), ..., q(z^K; \lambda)} [
    \log 1/K \sum_{k=1}^K p(x, z^k)/q(z^k; \lambda) ]$
  
  based on the reparameterization trick.
docstr_tok: ["Build","loss","function",".","Its","automatic","differentiation","is","a","stochastic","gradient","of","$","-\\mathbb","{","E","}","_","{","q","(","z^1",";","\\lambda",")",",","...",",","q","(","z^K",";","\\lambda",")","}","[","\\log","1\/K","\\sum_","{","k=1","}","^K","p","(","x",",","z^k",")","\/q","(","z^k",";","\\lambda",")","]","$","based","on","the","reparameterization","trick","."]
code: def build_loss_and_gradients(self, var_list):
      """Build loss function. Its automatic differentiation
      is a stochastic gradient of
  
      $-\mathbb{E}_{q(z^1; \lambda), ..., q(z^K; \lambda)} [
        \log 1/K \sum_{k=1}^K p(x, z^k)/q(z^k; \lambda) ]$
  
      based on the reparameterization trick.
      """
      # Form vector of K log importance weights.
      log_w = []
      for k in range(self.K):
          scope = "inference_" + str(id(self)) + "/" + str(k)
          z_sample = {}
          q_log_prob = 0.0
          for z, qz in six.iteritems(self.latent_vars):
              # Copy q(z) to obtain new set of posterior samples.
              qz_copy = copy(qz, scope=scope)
              z_sample[z] = qz_copy
              q_log_prob += tf.reduce_sum(qz_copy.log_prob(qz_copy))
  
          p_log_prob = 0.0
          for z in six.iterkeys(self.latent_vars):
              # Copy p(z), swapping its conditioning set with samples
              # from variational distribution.
              z_copy = copy(z, z_sample, scope=scope)
              p_log_prob += tf.reduce_sum(z_copy.log_prob(z_sample[z]))
  
          for x, qx in six.iteritems(self.data):
              if isinstance(x, RandomVariable):
                  # Copy p(x | z), swapping its conditioning set with samples
                  # from variational distribution.
                  x_copy = copy(x, z_sample, scope=scope)
                  p_log_prob += tf.reduce_sum(x_copy.log_prob(qx))
  
          log_w += [p_log_prob - q_log_prob]
  
      loss = -reduce_logmeanexp(log_w)
      grads = tf.gradients(loss, [v._ref() for v in var_list])
      grads_and_vars = list(zip(grads, var_list))
      return loss, grads_and_vars
  
file_path: edward/examples/iwvi.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import edward
  import numpy
  import six
  import tensorflow
  from edward.inferences import VariationalInference
  from edward.models import Bernoulli, Normal, RandomVariable
  from edward.util import copy
  from scipy.special import expit
label_desc: Use image node_id 3 for calling the IWVI obj's underlying member method code with example usage: obj.build_loss_and_gradients(var_list) and returns: loss,  grads_and_vars