[Solved] Runtime error while calling SVI

Hi,

I’m trying out Bayesian Regression. I’ve run into RuntimeError: bool value of Tensor with more than one value is ambiguous while performing svi.step. train_X and train_y are torch tensors of size (torch.Size([120, 2]) and torch.Size([120])).

guide = AutoDiagonalNormal(model)
optim = Adam({'lr' : 0.03})
svi = SVI(model, guide, optim, loss=Trace_ELBO)

def train():
  pyro.clear_param_store()
  for j in range(num_iterations):
    # calculate loss; apply gradients
    loss = svi.step(train_X, train_y) #  <-- ERROR
    if j%100 == 0:  #  for every 100 steps
      print("iteration {j} : loss [{loss}]".format(
          j=j+1, loss=loss/len(data))
           )

train()

I’m using torch-1.0.0 and pyro-ppl-0.3.0.
Would really appreciate your help in resolving this.


Traceback

<ipython-input-28-69443a3a9594> in train()
      3   for j in range(num_iterations):
      4     # calculate loss; apply gradients
----> 5     loss = svi.step(train_X, train_y)
      6     if j%100 == 0:  #  for every 100 steps
      7       print("iteration {j} : loss [{loss}]".format(

/usr/local/lib/python3.6/dist-packages/pyro/infer/svi.py in step(self, *args, **kwargs)
     97         # get loss and compute gradients
     98         with poutine.trace(param_only=True) as param_capture:
---> 99             loss = self.loss_and_grads(self.model, self.guide, *args, **kwargs)
    100 
    101         params = set(site["value"].unconstrained()

/usr/local/lib/python3.6/dist-packages/pyro/infer/svi.py in _loss_and_grads(*args, **kwargs)
     56             if loss_and_grads is None:
     57                 def _loss_and_grads(*args, **kwargs):
---> 58                     loss_val = loss(*args, **kwargs)
     59                     loss_val.backward(retain_graph=True)
     60                     return loss_val

/usr/local/lib/python3.6/dist-packages/pyro/infer/elbo.py in __init__(self, num_particles, max_plate_nesting, max_iarange_nesting, vectorize_particles, strict_enumeration_warning, ignore_jit_warnings, retain_graph)
     71         self.vectorize_particles = vectorize_particles
     72         self.retain_graph = retain_graph
---> 73         if self.vectorize_particles and self.num_particles > 1:
     74             self.max_plate_nesting += 1
     75         self.strict_enumeration_warning = strict_enumeration_warning

RuntimeError: bool value of Tensor with more than one value is ambiguous

Edit

Hi, in your snippet you need to pass an instance of Trace_ELBO rather than the class to SVI:

svi = SVI(model, guide, optim, loss=Trace_ELBO())  # not loss=Trace_ELBO
1 Like

Thank you @eb8680_2. How silly of me!
Works now.