Error running pyro example

Hello,

following @ngoodman 's suggestion in the other thread, I tried pyro, specifically the bayesian regression example:

http://pyro.ai/examples/bayesian_regression.html#Inference

However, I run into the following error message

RuntimeError: Only Variables created explicitly by the user (graph leaves) support the deepcopy protocol at the moment

The full backtrace is below, I’m using pytorch and pyro built from source today on python3.5.
I would be very grateful for a hint.

Thank you and best regards

Thomas

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-9-a8c3f37fb224> in <module>()
      3 for j in range(num_iterations):
      4     # calculate the loss and take a gradient step
----> 5     loss = svi.step(data)
      6     if j % 100 == 0:
      7         print("[iteration %04d] loss: %.4f" % (j + 1, loss / float(N)))

/usr/local/lib/python3.5/dist-packages/pyro/infer/svi.py in step(self, *args, **kwargs)
     96         """
     97         # get loss and compute gradients
---> 98         loss = self.loss_and_grads(self.model, self.guide, *args, **kwargs)
     99 
    100         # get active params

/usr/local/lib/python3.5/dist-packages/pyro/infer/elbo.py in loss_and_grads(self, model, guide, *args, **kwargs)
     63         :rtype: float
     64         """
---> 65         return self.which_elbo.loss_and_grads(model, guide, *args, **kwargs)

/usr/local/lib/python3.5/dist-packages/pyro/infer/trace_elbo.py in loss_and_grads(self, model, guide, *args, **kwargs)
    131         elbo = 0.0
    132         # grab a trace from the generator
--> 133         for weight, model_trace, guide_trace, log_r in self._get_traces(model, guide, *args, **kwargs):
    134             elbo_particle = weight * 0
    135             surrogate_elbo_particle = weight * 0

/usr/local/lib/python3.5/dist-packages/pyro/infer/trace_elbo.py in _get_traces(self, model, guide, *args, **kwargs)
     78                 continue
     79 
---> 80             guide_trace = poutine.trace(guide).get_trace(*args, **kwargs)
     81             model_trace = poutine.trace(poutine.replay(model, guide_trace)).get_trace(*args, **kwargs)
     82 

/usr/local/lib/python3.5/dist-packages/pyro/poutine/trace_poutine.py in get_trace(self, *args, **kwargs)
    160         """
    161         self(*args, **kwargs)
--> 162         return self.trace.copy()
    163 
    164     def _reset(self):

/usr/local/lib/python3.5/dist-packages/pyro/poutine/trace.py in copy(self)
     50         and the self.graph_type attribute
     51         """
---> 52         return Trace(super(Trace, self).copy(), graph_type=self.graph_type)
     53 
     54     def log_pdf(self, site_filter=lambda name, site: True):

/usr/lib/python3/dist-packages/networkx/classes/graph.py in copy(self)
   1446 
   1447         """
-> 1448         return deepcopy(self)
   1449 
   1450     def is_multigraph(self):

/usr/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/usr/lib/python3.5/copy.py in _reconstruct(x, info, deep, memo)
    295     if state is not None:
    296         if deep:
--> 297             state = deepcopy(state, memo)
    298         if hasattr(y, '__setstate__'):
    299             y.__setstate__(state)

/usr/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.5/copy.py in _deepcopy_dict(x, memo)
    241     memo[id(x)] = y
    242     for key, value in x.items():
--> 243         y[deepcopy(key, memo)] = deepcopy(value, memo)
    244     return y
    245 d[dict] = _deepcopy_dict

/usr/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    180                             raise Error(
    181                                 "un(deep)copyable object of type %s" % cls)
--> 182                 y = _reconstruct(x, rv, 1, memo)
    183 
    184     # If is its own copy, don't memoize.

/usr/lib/python3.5/copy.py in _reconstruct(x, info, deep, memo)
    318             if deep:
    319                 key = deepcopy(key, memo)
--> 320                 value = deepcopy(value, memo)
    321             y[key] = value
    322     return y

/usr/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    153     copier = _deepcopy_dispatch.get(cls)
    154     if copier:
--> 155         y = copier(x, memo)
    156     else:
    157         try:

/usr/lib/python3.5/copy.py in _deepcopy_dict(x, memo)
    241     memo[id(x)] = y
    242     for key, value in x.items():
--> 243         y[deepcopy(key, memo)] = deepcopy(value, memo)
    244     return y
    245 d[dict] = _deepcopy_dict

/usr/lib/python3.5/copy.py in deepcopy(x, memo, _nil)
    164             copier = getattr(x, "__deepcopy__", None)
    165             if copier:
--> 166                 y = copier(memo)
    167             else:
    168                 reductor = dispatch_table.get(cls)

/usr/local/lib/python3.5/dist-packages/torch/autograd/variable.py in __deepcopy__(self, memo)
     90     def __deepcopy__(self, memo):
     91         if not self.is_leaf:
---> 92             raise RuntimeError("Only Variables created explicitly by the user "
     93                                "(graph leaves) support the deepcopy protocol at the moment")
     94         result = type(self)(self.data.clone())

RuntimeError: Only Variables created explicitly by the user (graph leaves) support the deepcopy protocol at the moment

HI Thomas, Thanks for reporting this! Would you mind making an issue over at the pyro github? One of the devs will look into it shortly!

[edit: nvm, i made the issue. keep an eye over there for solutions.]

Hi Noah,

thank you for the prompt reply and for filing the issue.
I thought that it was likely something I did wrong…

Best regards

Thomas