Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bayesian Network Models in PyMC3 and NetworkX
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@Fangwq
Copy link

Fangwq commented Mar 7, 2019

I get the same error with pymc3 3.6 and python3.

Traceback (most recent call last):
File "pmml_test.py", line 147, in
[gpm(BN,'D4', num=1),BN.adj['C3']['D4']['bin']], observed=BN.node['D4']['observe'])
File "/Users/fangwq/Library/Python/3.6/lib/python/site-packages/theano/tensor/var.py", line 544, in getitem
theano.tensor.subtensor.Subtensor.convert(arg)
File "/Users/fangwq/Library/Python/3.6/lib/python/site-packages/theano/tensor/subtensor.py", line 354, in convert
raise TypeError("Expected an integer")
TypeError: Expected an integer

Any help, please? Thanks!

@NelisW
Copy link

NelisW commented Jul 1, 2019

Thanks to all above!
I have about four hours experience in pymc3, so the above comments are much appreciated.

Using networkx 2.3 and pyMC3 3.7, cpu on Windows.
Fixed the dict_keyiterator problem.

    32     BN.edge['C3']['D4']['bin'] = T.switch(T.lt(gpm(BN,'D4'),9), 0, 
---> 33                                           T.switch(T.gt(gpm(BN,'D4'), 9) & T.lt(gpm(BN,'D4'),11), 1, 2))

AttributeError: 'DiGraph' object has no attribute 'edge'

When print(BN.__dict__) I see no edge attribute.
The edges seem to be embedded in _adj, _pred and _succ.

BN = nx.DiGraph()
BN.add_node('D1', dtype='Discrete', prob=d1_prob, pos=(2, 4))
BN.add_node('D2', dtype='Discrete', prob=d2_prob, pos=(4, 4))
BN.add_node('C1', dtype='Continuous', mu=c1_mu, sd=c1_sd, pos=(1,3))
BN.add_edge('D1', 'C1')
print(BN.__dict__)
-----------------------------------------------
{
'graph_attr_dict_factory': <class 'dict'>, 'node_dict_factory': <class 'dict'>, 
'node_attr_dict_factory': <class 'dict'>, 'adjlist_outer_dict_factory': <class 'dict'>, 
'adjlist_inner_dict_factory': <class 'dict'>, 'edge_attr_dict_factory': <class 'dict'>, 
'graph': {}, 
'_node': {
  'D1': {'dtype': 'Discrete', 'prob': array([0.3, 0.7]), 'pos': (2, 4)}, 
  'D2': {'dtype': 'Discrete', 'prob': array([0.6, 0.3, 0.1]), 'pos': (4, 4)}, 
  'C1': {'dtype': 'Continuous', 'mu': array([10, 14]), 'sd': array([2, 2]), 'pos': (1, 3)}
  }, 
'_adj': {'D1': {'C1': {}}, 'D2': {}, 'C1': {}},
'_pred': {'D1': {}, 'D2': {}, 'C1': {'D1': {}}}, 
'_succ': {'D1': {'C1': {}}, 'D2': {}, 'C1': {}}
}

D1 is a predecessor for C1.
C1 is a successor for D1.
But C1 is also adjacent to D1?

My bigger issue is what to do with the error on line 32/33 above.

I have no idea how to handle this. Any help will be appreciated!

@tbsexton
Copy link
Author

tbsexton commented Jul 1, 2019

Wow, I had no idea there were comments here! Thanks to all for checking out the example. For those of you with issues/ideas (especially networkx v2 problems), check out the repository that automates a lot of the underlying mechanical/boilerplate code in this example. This notebook was really just a proof-of-concept for that repository.

I haven't gone back to the code for a while, but if there's interest I would be happy to update things to newer pymc3 features. Please see if your above issues persist in the more mature repository. Thanks!

@NelisW
Copy link

NelisW commented Jul 1, 2019

@tbsexton
Copy link
Author

tbsexton commented Jul 1, 2019

@NelisW certainly, and like I said, feel free to open an issue if something works unexpectedly on that repo (as I said, it's been a while).

@eHonnef
Copy link

eHonnef commented Nov 22, 2019

If you are having issues with BN.edges['C3']['D4']['bin'], substitute for BN.edges['C3','D4']['bin'].

As described in the migration guide mentioned before

G.edge have been removed in favor of using G.nodes[n] and G.edges[u, v]

@rezaaskary
Copy link

rezaaskary commented Oct 1, 2020

Do you have any example for dynamic BN simulated on PYMC3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment