code: def test_inference():
      class Linear(Parameterized):
          def __init__(self, a):
              super().__init__()
              self.a = Parameter(a)
  
          def forward(self, x):
              return self.a * x
  
      target_a = torch.tensor(2.0)
      x_train = torch.rand(100)
      y_train = target_a * x_train + torch.rand(100) * 0.001
      linear = Linear(torch.tensor(1.0))
      linear.a = PyroSample(dist.Normal(0, 10))
      linear.autoguide("a", dist.Normal)
  
      def model(x, y):
          linear.set_mode("model")
          mu = linear(x)
          with pyro.plate("plate"):
              return pyro.sample("y", dist.Normal(mu, 0.1), obs=y)
  
      def guide(x, y):
          linear.set_mode("guide")
          linear._load_pyro_samples()
  
      loss_fn = pyro.infer.Trace_ELBO().differentiable_loss
      optimizer = torch.optim.Adam(linear.parameters(), lr=0.5)
  
      def closure():
          optimizer.zero_grad()
          loss = loss_fn(model, guide, x_train, y_train)
          loss.backward()
          return loss
  
      for i in range(200):
          optimizer.step(closure)
  
      linear.mode = "guide"
      assert_equal(linear.a, target_a, prec=0.05)
  
file_path: pyro/tests/contrib/gp/test_parameterized.py
imports: import torch
  from torch.distributions import constraints
  from torch.nn import Parameter
  import pyro
  import pyro.distributions
  from pyro.contrib.gp.parameterized import Parameterized
  from pyro.nn.module import PyroParam, PyroSample
  from tests.common import assert_equal
label_desc: Use image node_id 3 for calling a global function with example usage: test_inference() and returns: loss, unknown, pyro