Skip to content

'model_to_graphviz' throws TypeError when using shared variables #3483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sidravi1 opened this issue May 20, 2019 · 3 comments
Closed

'model_to_graphviz' throws TypeError when using shared variables #3483

sidravi1 opened this issue May 20, 2019 · 3 comments

Comments

@sidravi1
Copy link
Contributor

pm.model_to_graphviz gives a TypeError with sharedVariable

Please provide a minimal, self-contained, and reproducible example.

import pymc3 as pm
import numpy as np
import theano as th

# some data
x = np.linspace(0, 10, 100)
y = x * 5 + np.random.normal(0, 0.2)

x_shared = th.shared(x)
y_shared = th.shared(y)

# simple model
with pm.Model() as test:
    
    beta = pm.Normal('beta', 0, 10)
    _y = beta * x_shared 
    
    sigma = pm.HalfNormal('sigma', 2)
    obs = pm.Normal('obs', _y, sigma, observed=y_shared)

# generate DAG
pm.model_to_graphviz(test)

Please provide the full traceback.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-1e5201be2a87> in <module>
----> 1 pm.model_to_graphviz(test)

~/anaconda3/envs/cotopaxi/lib/python3.6/site-packages/pymc3/model_graph.py in model_to_graphviz(model)
    184     """
    185     model = pm.modelcontext(model)
--> 186     return ModelGraph(model).make_graph()

~/anaconda3/envs/cotopaxi/lib/python3.6/site-packages/pymc3/model_graph.py in make_graph(self)
    154         graph = graphviz.Digraph(self.model.name)
    155         for shape, var_names in self.get_plates().items():
--> 156             label = ' x '.join(map('{:,d}'.format, shape))
    157             if label:
    158                 # must be preceded by 'cluster' to get a box around it

TypeError: unsupported format string passed to TensorVariable.__format__

Please provide any additional information below.

The fix seems straightforward. Just need an extra line:

if isinstance(shape, th.tensor.sharedvar.SharedVariable):
    shape = shape.eval()

before line 156 in model_graph.py

Versions and main components

  • PyMC3 Version: 3.6
  • Theano Version: 1.0.4
  • Python Version: 3.6
  • Operating system: Ubuntu
  • How did you install PyMC3: (conda/pip) conda
@ColCarroll
Copy link
Member

Thanks for report and fix (which looks right to me). Do you have any interest/time in submitting it?

@sidravi1
Copy link
Contributor Author

Would love to! :)

ColCarroll pushed a commit that referenced this issue May 23, 2019
…3490)

* fix for 3483 + modified test_model_grah.py to test it

* fixed model_graph.py to handle shardvars

* adding import for theano
@ColCarroll
Copy link
Member

Fixed by #3490

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants