Giter Site home page Giter Site logo

fglib's Introduction

Build Status Coverage Status PyPI version

logo of fglib

fglib

The factor graph library (fglib) is a Python package to simulate message passing on factor graphs. It supports the

  • sum-product algorithm (belief propagation)
  • max-product algorithm
  • max-sum algorithm
  • mean-field algorithm - in development

with discrete and Gaussian random variables.

Installation

Install fglib with the Python Package Index by using

pip install fglib

Install fglib with setuptools by using

python setup.py install

Dependencies

Documentation

In order to generate the documentation site for the factor graph library, execute the following commands from the top-level directory.

$ cd docs/
$ make html

Example

Examples (like the following one) are located in the examples/ directory.

"""A simple example of the sum-product algorithm

This is a simple example of the sum-product algorithm on a factor graph
with Discrete random variables.

      /--\      +----+      /--\      +----+      /--\
     | x1 |-----| fa |-----| x2 |-----| fb |-----| x3 |
      \--/      +----+      \--/      +----+      \--/
                             |
                           +----+
                           | fc |
                           +----+
                             |
                            /--\
                           | x4 |
                            \--/

The following joint distributions are used for the factor nodes.

     fa   | x2=0 x2=1 x2=2     fb   | x3=0 x3=1     fc   | x4=0 x4=1
     ---------------------     ----------------     ----------------
     x1=0 | 0.3  0.2  0.1      x2=0 | 0.3  0.2      x2=0 | 0.3  0.2
     x1=1 | 0.3  0.0  0.1      x2=1 | 0.3  0.0      x2=1 | 0.3  0.0
                               x2=2 | 0.1  0.1      x2=2 | 0.1  0.1

"""

from fglib import graphs, nodes, inference, rv

# Create factor graph
fg = graphs.FactorGraph()

# Create variable nodes
x1 = nodes.VNode("x1", rv.Discrete)  # with 2 states (Bernoulli)
x2 = nodes.VNode("x2", rv.Discrete)  # with 3 states
x3 = nodes.VNode("x3", rv.Discrete)
x4 = nodes.VNode("x4", rv.Discrete)

# Create factor nodes (with joint distributions)
dist_fa = [[0.3, 0.2, 0.1],
           [0.3, 0.0, 0.1]]
fa = nodes.FNode("fa", rv.Discrete(dist_fa, x1, x2))

dist_fb = [[0.3, 0.2],
           [0.3, 0.0],
           [0.1, 0.1]]
fb = nodes.FNode("fb", rv.Discrete(dist_fb, x2, x3))

dist_fc = [[0.3, 0.2],
           [0.3, 0.0],
           [0.1, 0.1]]
fc = nodes.FNode("fc", rv.Discrete(dist_fc, x2, x4))

# Add nodes to factor graph
fg.set_nodes([x1, x2, x3, x4])
fg.set_nodes([fa, fb, fc])

# Add edges to factor graph
fg.set_edge(x1, fa)
fg.set_edge(fa, x2)
fg.set_edge(x2, fb)
fg.set_edge(fb, x3)
fg.set_edge(x2, fc)
fg.set_edge(fc, x4)

# Perform sum-product algorithm on factor graph
# and request belief of variable node x4
belief = inference.sum_product(fg, x4)

# Print belief of variables
print("Belief of variable node x4:")
print(belief)

References

  1. B. J. Frey, F. R. Kschischang, H.-A. Loeliger, and N. Wiberg, "Factor graphs and algorithms," in Proc. 35th Allerton Conf. Communications, Control, and Computing, Monticello, IL, Sep. 29-Oct. 1, 1997, pp. 666-680.

  2. F. R. Kschischang, B. J. Frey, and H.-A. Loeliger, “Factor graphs and the sum-product algorithm,” IEEE Trans. Inform. Theory, vol. 47, no. 2, pp. 498–519, Feb. 2001.

  3. H.-A. Loeliger, “An introduction to factor graphs,” IEEE Signal Process. Mag., vol. 21, no. 1, pp. 28–41, Jan. 2004.

  4. H.-A. Loeliger, J. Dauwels, H. Junli, S. Korl, P. Li, and F. R. Kschischang, “The factor graph approach to model-based signal processing,” Proc. IEEE, vol. 95, no. 6, pp. 1295–1322, Jun. 2007.

  5. H. Wymeersch, Iterative Receiver Design. Cambridge, UK: Cambridge University Press, 2007.

  6. C. M. Bishop, Pattern Recognition and Machine Learning, 8th ed., ser. Information Science and Statistics. New York, USA: Springer Science+Business Media, 2009.

fglib's People

Contributors

danbar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

fglib's Issues

Max-sum value assignment Error


fg = graphs.FactorGraph()

x1 = nodes.VNode("x1", rv.Discrete)
x2 = nodes.VNode("x2", rv.Discrete)
x3 = nodes.VNode("x3", rv.Discrete)
x4 = nodes.VNode("x4", rv.Discrete)
x5 = nodes.VNode("x5", rv.Discrete)

dist_fa = [
    [
        [0.1, 0.2],
        [0.1, 0.1]
    ],
    [
        [0.2, 0.05],
        [0.2, 0.05]
    ]
           ]
fa = nodes.FNode("fa", rv.Discrete(dist_fa, x1, x2,x3))

dist_fb = [[0.1, 0.4],
           [0.2, 0.3]]
fb = nodes.FNode("fb", rv.Discrete(dist_fb, x3, x4))

dist_fc = [[0.5, 0.1],
           [0.2, 0.2]]
fc = nodes.FNode("fc", rv.Discrete(dist_fc, x3, x5))

fg.set_nodes([x1, x2, x3, x4, x5])
fg.set_nodes([fa,fb,fc])

fg.set_edge(x1, fa)
fg.set_edge(x2, fa)
fg.set_edge(fa, x3)
fg.set_edge(x4, fb)
fg.set_edge(fb, x3)
fg.set_edge(x3, fc)
fg.set_edge(fc, x5)
fg.set_edge(x2, fb)
fg.set_edge(fb, x3)
fg.set_edge(x2, fc)
fg.set_edge(fc, x4)

belief = inference.max_sum(fg,x5)`

for this problem I get Value assignment
x5 0
x3 1
x1 3
x2 1
x4 0
but all of the variables are binary. SO x1 = 3 does not looks right

Inference Does Not Work for Non-Tree Factor Graphs

It looks like inference is not working for non-tree structures.
For example consider the following simple factor graph with nodes x1, x2, x3 and factors fa, fb, fc.

from fglib import graphs, nodes, inference, rv

# Create factor graph
fg = graphs.FactorGraph()

# Create variable nodes
x1 = nodes.VNode("x1", rv.Discrete)
x2 = nodes.VNode("x2", rv.Discrete)
x3 = nodes.VNode("x3", rv.Discrete)

# Create factor nodes (with joint distributions)
dist_fa = [[1.0, 0.0],
           [0.2, 0.8]]
fa = nodes.FNode("fa", rv.Discrete(dist_fa, x1, x2))

dist_fb = [[1.0, 0.0],
           [0.3, 0.7]]
fb = nodes.FNode("fb", rv.Discrete(dist_fb, x2, x3))

dist_fc = [[1.0, 0.0],
           [0.4, 0.6]]
fc = nodes.FNode("fc", rv.Discrete(dist_fc, x1, x3))

# Add nodes to factor graph
fg.set_nodes([x1, x2, x3])
fg.set_nodes([fa, fb, fc])

# Add edges to factor graph
fg.set_edge(x1, fa)
fg.set_edge(fa, x2)
fg.set_edge(x2, fb)
fg.set_edge(fb, x3)
fg.set_edge(x1, fc)
fg.set_edge(fc, x3)

# Perform sum-product algorithm on factor graph
# and request belief of variable node x3
belief = inference.sum_product(fg, x3)

# Print belief of variables
print("Belief of variable node x3:")
print(belief)

The terminal output is shown below.

Traceback (most recent call last):
  File "fglib_example.py", line 35, in <module>
    belief = inference.sum_product(fg, x3)
  File "/home/bradley/.local/lib/python3.6/site-packages/fglib/inference.py", line 63, in sum_product
    return belief_propagation(graph, query_node)
  File "/home/bradley/.local/lib/python3.6/site-packages/fglib/inference.py", line 42, in belief_propagation
    msg = u.spa(v)
  File "/home/bradley/.local/lib/python3.6/site-packages/fglib/nodes.py", line 270, in spa
    msg *= self.graph[n][self]['object'].get_message(n, self)
  File "/home/bradley/.local/lib/python3.6/site-packages/fglib/rv.py", line 255, in __imul__
    return self.__mul__(other)
  File "/home/bradley/.local/lib/python3.6/site-packages/fglib/rv.py", line 212, in __mul__
    if len(self.dim) < len(other.dim):
AttributeError: 'NoneType' object has no attribute 'dim'

Error in example of sum product

Dear Author,

I got this error after installing this package. Can you tell me what I need to do?

`TypeError Traceback (most recent call last)
in ()
39 # Perform sum-product algorithm on factor graph
40 # and request belief of variable node x4
---> 41 belief = inference.sum_product(fg, x4)
42
43 # Print belief of variables

~\Downloads\BitBucket\python\FGlib\fglib\inference.py in sum_product(graph, query_node)
61
62 # Sum-Product algorithm is equivalent to Belief Propagation
---> 63 return belief_propagation(graph, query_node)
64
65

~\Downloads\BitBucket\python\FGlib\fglib\inference.py in belief_propagation(graph, query_node)
49
50 # Return marginal distribution
---> 51 return query_node.belief()
52
53

~\Downloads\BitBucket\python\FGlib\fglib\nodes.py in belief(self, normalize)
132
133 # Pick first node
--> 134 n = next(iterator)
135
136 # Product over all incoming messages

TypeError: 'list' object is not an iterator`

The 'sphinx-build' command was not found.

Hi~
When running make html, I was told that:
"The 'sphinx-build' command was not found. Make sure you have Sphinx
installed, then set the SPHINXBUILD environment variable to point
to the full path of the 'sphinx-build' executable. Alternatively you
may add the Sphinx directory to PATH."
How to deal with this exception?
Thanks~

Gaussian Nodes returns TypeError: marginalize() got an unexpected keyword argument 'normalize'

Hello,

When trying the test case (test.txt) with Gaussian variables, I receive the following error

Traceback (most recent call last):
File "npbp.py", line 31, in
belief = inference.sum_product(fg, x4)
File "/home/jdmartin86/.local/lib/python3.5/site-packages/fglib/inference.py", line 63, in sum_product
return belief_propagation(graph, query_node)
File "/home/jdmartin86/.local/lib/python3.5/site-packages/fglib/inference.py", line 42, in belief_propagation
msg = u.spa(v)
File "/home/jdmartin86/.local/lib/python3.5/site-packages/fglib/nodes.py", line 271, in spa
msg = msg.marginalize(n, normalize=False)
TypeError: marginalize() got an unexpected keyword argument 'normalize'

Error in max-product algorithm

I just copy your code in max-product algorithm example. But I got
AttributeError: 'NoneType' object has no attribute 'normalize'.

I guess you do not initialize the value when running the max-product algorithm.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.