code: def test_subsample_gradient_sequential(
      Elbo, reparameterized, subsample
  ):
      pyro.clear_param_store()
      data = torch.tensor([-0.5, 2.0])
      subsample_size = 1 if subsample else len(data)
      num_particles = 5000
      precision = 0.333
      Normal = (
          dist.Normal
          if reparameterized
          else fakes.NonreparameterizedNormal
      )
  
      def model():
          with pyro.plate("data", len(data), subsample_size) as ind:
              x = data[ind]
              z = pyro.sample("z", Normal(0, 1).expand_by(x.shape))
              pyro.sample("x", Normal(z, 1), obs=x)
  
      def guide():
          loc = pyro.param(
              "loc", lambda: torch.zeros(len(data), requires_grad=True)
          )
          scale = pyro.param(
              "scale", lambda: torch.tensor([1.0], requires_grad=True)
          )
          with pyro.plate("data", len(data), subsample_size) as ind:
              pyro.sample("z", Normal(loc[ind], scale))
  
      optim = Adam({"lr": 0.1})
      elbo = Elbo(num_particles=10, strict_enumeration_warning=False)
      inference = SVI(model, guide, optim, elbo)
      iters = num_particles // 10
      with xfail_if_not_implemented():
          for _ in range(iters):
              inference.loss_and_grads(model, guide)
  
      params = dict(pyro.get_param_store().named_parameters())
      actual_grads = {
          name: param.grad.detach().cpu().numpy() / iters
          for name, param in params.items()
      }
  
      expected_grads = {
          "loc": np.array([0.5, -2.0]),
          "scale": np.array([2.0]),
      }
      for name in sorted(params):
          logger.info(
              "expected {} = {}".format(name, expected_grads[name])
          )
          logger.info(
              "actual   {} = {}".format(name, actual_grads[name])
          )
      assert_equal(actual_grads, expected_grads, prec=precision)
  
file_path: pyro/tests/infer/test_gradient.py
imports: import logging
  import numpy
  import pytest
  import torch
  import torch.optim
  from torch.distributions import constraints
  import pyro
  import pyro.distributions
  import pyro.poutine
  from pyro.distributions.testing import fakes
  from pyro.infer import SVI, JitTrace_ELBO, JitTraceEnum_ELBO, JitTraceGraph_ELBO, JitTraceMeanField_ELBO, Trace_ELBO, TraceEnum_ELBO, TraceGraph_ELBO, TraceMeanField_ELBO, config_enumerate
  from pyro.optim import Adam
  from tests.common import assert_equal, xfail_if_not_implemented, xfail_param
label_desc: Use image node_id 6 for calling a global function with example usage: test_subsample_gradient_sequential(Elbo, reparameterized, subsample) without return types