class_docstr: An implementation of Reweighted Wake Sleep following reference [1].
  
  .. note:: Sampling and log_prob evaluation asymptotic complexity:
  
  1) Using wake-theta and/or wake-phi
      O(`num_particles`) samples from guide,
      O(`num_particles`) `log_prob` evaluations of model and guide
  
  2) Using sleep-phi
      O(`num_sleep_particles`) samples from model,
      O(`num_sleep_particles`) `log_prob` evaluations of guide
  
  if 1) and 2) are combined,
      O(`num_particles`) samples from the guide,
      O(`num_sleep_particles`) from the model,
      O(`num_particles` + `num_sleep_particles`) `log_prob` evaluations of the guide, and
      O(`num_particles`) evaluations of the model
  
  .. note:: This is particularly useful for models with stochastic branching,
      as described in [2].
  
  .. note:: This returns _two_ losses, one each for (a) the model parameters (`theta`), computed using the
      `iwae` objective, and (b) the guide parameters (`phi`), computed using (a combination of) the `csis`
      objective and a self-normalized importance-sampled version of the `csis` objective.
  
  .. note:: In order to enable computing the sleep-phi terms, the guide program must have its observations
      explicitly passed in through the keyworded argument `observations`. Where the value of the observations
      is unknown during definition, such as for amortized variational inference, it may be given a default
      argument as `observations=None`, and the correct value supplied during learning through
      `svi.step(observations=...)`.
  
  .. warning:: Mini-batch training is not supported yet.
  
  :param int num_particles: The number of particles/samples used to form the objective
      (gradient) estimator. Default is 2.
  :param insomnia: The scaling between the wake-phi and sleep-phi terms. Default is 1.0 [wake-phi]
  :param bool model_has_params: Indicate if model has learnable params. Useful in avoiding extra
      computation when running in pure sleep mode [csis]. Default is True.
  :param int num_sleep_particles: The number of particles used to form the sleep-phi estimator.
      Matches `num_particles` by default.
  :param bool vectorize_particles: Whether the traces should be vectorised
      across `num_particles`. Default is True.
  :param int max_plate_nesting: Bound on max number of nested
      :func:`pyro.plate` contexts. Default is infinity.
  :param bool strict_enumeration_warning: Whether to warn about possible
      misuse of enumeration, i.e. that
      :class:`~pyro.infer.traceenum_elbo.TraceEnum_ELBO` is used iff there
      are enumerated sample sites.
  
  References:
  
  [1] `Reweighted Wake-Sleep`,
      Jörg Bornschein, Yoshua Bengio
  
  [2] `Revisiting Reweighted Wake-Sleep for Models with Stochastic Control Flow`,
      Tuan Anh Le, Adam R. Kosiorek, N. Siddharth, Yee Whye Teh, Frank Wood
class_docstr_tok: ["An","implementation","of","Reweighted","Wake","Sleep","following","reference","[","1","]",".","..","note",":",":","Sampling","and","log_prob","evaluation","asymptotic","complexity",":","1",")","Using","wake-theta","and\/or","wake-phi","O","(","`","num_particles","`",")","samples","from","guide",",","O","(","`","num_particles","`",")","`","log_prob","`","evaluations","of","model","and","guide","2",")","Using","sleep-phi","O","(","`","num_sleep_particles","`",")","samples","from","model",",","O","(","`","num_sleep_particles","`",")","`","log_prob","`","evaluations","of","guide","if","1",")","and","2",")","are","combined",",","O","(","`","num_particles","`",")","samples","from","the","guide",",","O","(","`","num_sleep_particles","`",")","from","the","model",",","O","(","`","num_particles","`","+","`","num_sleep_particles","`",")","`","log_prob","`","evaluations","of","the","guide",",","and","O","(","`","num_particles","`",")","evaluations","of","the","model","..","note",":",":","This","is","particularly","useful","for","models","with","stochastic","branching",",","as","described","in","[","2","]",".","..","note",":",":","This","returns","_two_","losses",",","one","each","for","(","a",")","the","model","parameters","(","`","theta","`",")",",","computed","using","the","`","iwae","`","objective",",","and","(","b",")","the","guide","parameters","(","`","phi","`",")",",","computed","using","(","a","combination","of",")","the","`","csis","`","objective","and","a","self-normalized","importance-sampled","version","of","the","`","csis","`","objective",".","..","note",":",":","In","order","to","enable","computing","the","sleep-phi","terms",",","the","guide","program","must","have","its","observations","explicitly","passed","in","through","the","keyworded","argument","`","observations","`",".","Where","the","value","of","the","observations","is","unknown","during","definition",",","such","as","for","amortized","variational","inference",",","it","may","be","given","a","default","argument","as","`","observations=None","`",",","and","the","correct","value","supplied","during","learning","through","`","svi.step","(","observations=","...",")","`",".","..","warning",":",":","Mini-batch","training","is","not","supported","yet",".",":","param","int","num_particles",":","The","number","of","particles\/samples","used","to","form","the","objective","(","gradient",")","estimator",".","Default","is","2.",":","param","insomnia",":","The","scaling","between","the","wake-phi","and","sleep-phi","terms",".","Default","is","1.0","[","wake-phi","]",":","param","bool","model_has_params",":","Indicate","if","model","has","learnable","params",".","Useful","in","avoiding","extra","computation","when","running","in","pure","sleep","mode","[","csis","]",".","Default","is","True",".",":","param","int","num_sleep_particles",":","The","number","of","particles","used","to","form","the","sleep-phi","estimator",".","Matches","`","num_particles","`","by","default",".",":","param","bool","vectorize_particles",":","Whether","the","traces","should","be","vectorised","across","`","num_particles","`",".","Default","is","True",".",":","param","int","max_plate_nesting",":","Bound","on","max","number","of","nested",":","func",":","`","pyro.plate","`","contexts",".","Default","is","infinity",".",":","param","bool","strict_enumeration_warning",":","Whether","to","warn","about","possible","misuse","of","enumeration",",","i.e",".","that",":","class",":","`","~pyro.infer.traceenum_elbo.TraceEnum_ELBO","`","is","used","iff","there","are","enumerated","sample","sites",".","References",":","[","1","]","`","Reweighted","Wake-Sleep","`",",","J\u00f6rg","Bornschein",",","Yoshua","Bengio","[","2","]","`","Revisiting","Reweighted","Wake-Sleep","for","Models","with","Stochastic","Control","Flow","`",",","Tuan","Anh","Le",",","Adam","R.","Kosiorek",",","N.","Siddharth",",","Yee","Whye","Teh",",","Frank","Wood"]
docstr: :returns: returns model loss and guide loss
  :rtype: float, float
  
  Computes the re-weighted wake-sleep estimators for the model (wake-theta) and the
    guide (insomnia * wake-phi + (1 - insomnia) * sleep-phi).
docstr_tok: [":","returns",":","returns","model","loss","and","guide","loss",":","rtype",":","float",",","float","Computes","the","re-weighted","wake-sleep","estimators","for","the","model","(","wake-theta",")","and","the","guide","(","insomnia","*","wake-phi","+","(","1","-","insomnia",")","*","sleep-phi",")","."]
code: def loss(self, model, guide, *args, **kwargs):
      """
      :returns: returns model loss and guide loss
      :rtype: float, float
  
      Computes the re-weighted wake-sleep estimators for the model (wake-theta) and the
        guide (insomnia * wake-phi + (1 - insomnia) * sleep-phi).
      """
      with torch.no_grad():
          wake_theta_loss, phi_loss = self._loss(
              model, guide, args, kwargs
          )
  
      return wake_theta_loss, phi_loss
  
file_path: pyro/pyro/infer/rws.py
imports: import math
  import torch
  import pyro
  import pyro.poutine
  from pyro.infer.elbo import ELBO
  from pyro.infer.enum import get_importance_trace
  from pyro.infer.util import is_validation_enabled
  from pyro.poutine.util import prune_subsample_sites
  from pyro.util import check_if_enumerated, check_model_guide_match, warn_if_nan
label_desc: Use image node_id 4 for calling the ReweightedWakeSleep obj's underlying member method code with example usage: obj.loss(model, guide) and returns: wake_theta_loss,  phi_loss