Skip to content

Inference Does Not Work for Non-Tree Factor Graphs #8

@bradleykohler96

Description

@bradleykohler96

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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions