biaslab / ForneyLab.jl

Julia package for automatically generating Bayesian inference algorithms through message passing on Forney-style factor graphs.
MIT License
149 stars 33 forks source link

Belief Propagation #185

Closed oliverdutton closed 2 years ago

oliverdutton commented 2 years ago

Hi, can this library encode the problem below where there are no random variables, and if it can is there any chance you'd provide an example.

i.e. what is the Julia, ForneyLab equivalent to

import numpy as np
import factorgraph as fg

# Make an empty graph
g = fg.Graph()

# Add some discrete random variables (RVs)
g.rv('a', 2)
g.rv('b', 3)

# Add some factors, unary and binary
g.factor(['a'], potential=np.array([0.3, 0.7]))
g.factor(['b', 'a'], potential=np.array([
        [0.2, 0.8],
        [0.4, 0.6],
        [0.1, 0.9],
]))

# Run (loopy) belief propagation (LBP)
iters, converged = g.lbp(normalize=True)
print 'LBP ran for %d iterations. Converged = %r' % (iters, converged)
print

# Print out the final messages from LBP
g.print_messages()
print

# Print out the final marginals
g.print_rv_marginals()

It'll be something like

using ForneyLab

# Declare a new graph
g = FactorGraph()

a = Variable(id=:a)
b = Variable(id=:b)

(Code which defined the factor matrix between a and b (named :ab))

placeholder(a, :a);
placeholder(b, :b);
placeholder(ab, :ab);

...

algo = messagePassingAlgorithm(x_t) # Figure out a schedule and compile to Julia code
source_code = algorithmSourceCode(algo)
eval(Meta.parse(source_code));

data = Dict(
  :a =>:a,
  :b=>:b,
  :ab=>:ab
)

# Execute algorithm
marginals = step!(data) #( but execute many times....)

As you can see this is missing large sections. I see that Julia is looking a bit like Theano (and tf1.x) with 'data' being a feed dict while a,b are abstract.

ThijsvdLaar commented 2 years ago

Hi, thanks for giving ForneyLab a spin. I'm not entirely sure what you're asking, as I understand it you want to avoid the use of the @RV macro in your model definition? Because @RV is simply a shorthand for creating variables and factors. For example:

@RV x
@RV y ~ SomeFactorNode(x)

is equivalent to

x = Variable(id=:x)
y = Variable(id=:y)
SomeFactorNode(y, x)

You can find a more extensive example in the introduction.ipynb demo notebook.

oliverdutton commented 2 years ago

I clearly flew straight over paragraph 2 of the introduction notebook. Sorry and thanks.