class_docstr: :param model: the model (callable containing Pyro primitives)
  :param guide: the guide (callable containing Pyro primitives)
  :param optim: a wrapper a for a PyTorch optimizer
  :type optim: ~pyro.optim.optim.PyroOptim
  :param loss: an instance of a subclass of :class:`~pyro.infer.elbo.ELBO`.
      Pyro provides three built-in losses:
      :class:`~pyro.infer.trace_elbo.Trace_ELBO`,
      :class:`~pyro.infer.tracegraph_elbo.TraceGraph_ELBO`, and
      :class:`~pyro.infer.traceenum_elbo.TraceEnum_ELBO`.
      See the :class:`~pyro.infer.elbo.ELBO` docs to learn how to implement
      a custom loss.
  :type loss: pyro.infer.elbo.ELBO
  :param num_samples: (DEPRECATED) the number of samples for Monte Carlo posterior approximation
  :param num_steps: (DEPRECATED) the number of optimization steps to take in ``run()``
  
  A unified interface for stochastic variational inference in Pyro. The most
  commonly used loss is ``loss=Trace_ELBO()``. See the tutorial
  `SVI Part I <http://pyro.ai/examples/svi_part_i.html>`_ for a discussion.
class_docstr_tok: [":","param","model",":","the","model","(","callable","containing","Pyro","primitives",")",":","param","guide",":","the","guide","(","callable","containing","Pyro","primitives",")",":","param","optim",":","a","wrapper","a","for","a","PyTorch","optimizer",":","type","optim",":","~pyro.optim.optim.PyroOptim",":","param","loss",":","an","instance","of","a","subclass","of",":","class",":","`","~pyro.infer.elbo.ELBO","`",".","Pyro","provides","three","built-in","losses",":",":","class",":","`","~pyro.infer.trace_elbo.Trace_ELBO","`",",",":","class",":","`","~pyro.infer.tracegraph_elbo.TraceGraph_ELBO","`",",","and",":","class",":","`","~pyro.infer.traceenum_elbo.TraceEnum_ELBO","`",".","See","the",":","class",":","`","~pyro.infer.elbo.ELBO","`","docs","to","learn","how","to","implement","a","custom","loss",".",":","type","loss",":","pyro.infer.elbo.ELBO",":","param","num_samples",":","(","DEPRECATED",")","the","number","of","samples","for","Monte","Carlo","posterior","approximation",":","param","num_steps",":","(","DEPRECATED",")","the","number","of","optimization","steps","to","take","in","``","run","(",")","``","A","unified","interface","for","stochastic","variational","inference","in","Pyro",".","The","most","commonly","used","loss","is","``","loss=Trace_ELBO","(",")","``",".","See","the","tutorial","`","SVI","Part","I","<","http",":","\/\/pyro.ai\/examples\/svi_part_i.html",">","`","_","for","a","discussion","."]
code: def __init__(
      self,
      model,
      guide,
      optim,
      loss,
      loss_and_grads=None,
      num_samples=0,
      num_steps=0,
      **kwargs,
  ):
      if num_steps:
          warnings.warn(
              "The `num_steps` argument to SVI is deprecated and will be removed in "
              "a future release. Use `SVI.step` directly to control the "
              "number of iterations.",
              FutureWarning,
          )
      if num_samples:
          warnings.warn(
              "The `num_samples` argument to SVI is deprecated and will be removed in "
              "a future release. Use `pyro.infer.Predictive` class to draw "
              "samples from the posterior.",
              FutureWarning,
          )
  
      self.model = model
      self.guide = guide
      self.optim = optim
      self.num_steps = num_steps
      self.num_samples = num_samples
      super().__init__(**kwargs)
  
      if not isinstance(optim, pyro.optim.PyroOptim):
          raise ValueError(
              "Optimizer should be an instance of pyro.optim.PyroOptim class."
          )
  
      if isinstance(loss, ELBO):
          self.loss = loss.loss
          self.loss_and_grads = loss.loss_and_grads
      else:
          if loss_and_grads is None:
  
              def _loss_and_grads(*args, **kwargs):
                  loss_val = loss(*args, **kwargs)
                  if getattr(loss_val, "requires_grad", False):
                      loss_val.backward(retain_graph=True)
                  return loss_val
  
              loss_and_grads = _loss_and_grads
          self.loss = loss
          self.loss_and_grads = loss_and_grads
  
file_path: pyro/pyro/infer/svi.py
imports: import warnings
  import torch
  import pyro
  import pyro.optim
  import pyro.poutine
  from pyro.infer.abstract_infer import TracePosterior
  from pyro.infer.elbo import ELBO
  from pyro.infer.util import torch_item
label_desc: Use image node_id 1 to create a new SVI object from inherited base classes: TracePosterior with example: obj = SVI(model, guide, optim, loss, loss_and_grads, num_samples, num_steps)