inferlo.interop.LibDaiInterop
- class inferlo.interop.LibDaiInterop[source]
Interoperation with libDAI.
See https://staff.fnwi.uva.nl/j.m.mooij/libDAI/
Interoperation uses file interface. We created a simple C++ file which reads model from file, solves problem for it using libDAI and writes output to another file. This Python file is reponsible for converting model to libDAI format, writing input file, invoking libDAI and reading the output file.
To use it, you will have to install libDAI on your own:
1. Get libDAI:
git clone https://bitbucket.org/jorism/libdai.git
.Build libDAI following instructions in README.
Specify path to libdai dir in
bin/build.sh
.Run
build.sh
.
This works on Ubuntu, wasn’t checked for other platforms.
We use libDAI only for verification and benchmarking of inferlo’s own functionality. We don’t use in any of inferlo’s algorithms.
- Algorithms
libDAI supports several inference algorithms, identified by codename. For full list of algorithms, see LibDaiInterop.ALL_ALGORITHMS.
List of supported algorithms with their full names:
BP - (Loopy) Belief Propagation;
CBP - Conditioned Belief Propagation;
DECMAP - Decimation algorithm;
EXACT - Exact inference by brute force enumeration;
FBP - Fractional Belief Propagation;
GIBBS - Gibbs sampler;
GLC - Generalized Loop Corrected Belief Propagation;
HAK - Double-loop Generalized Belief Propagation;
JTREE - Exact inference by junction-tree methods;
LC - Loop Corrected Belief Propagation;
MF - Mean Field;
MR - Approximate inference algorithm by Montanari and Rizzo;
TREEEP - Tree Expectation Propagation;
TRWBP - Tree-Reweighted Belief Propagation;
All algorithms support calculating marginal probabilties, not all of them support calculating logZ or most likely state. If algorithm doesn’t support calculating logZ, you’ll get 0. If algorithms doesn’t support calculating most likely state, exception will be thrown.
For documentation on particular algorithms, refer to libDAI documentation: https://staff.fnwi.uva.nl/j.m.mooij/libDAI/doc/.
- Options
Almost every algorithm requires some parameters. Parameters are passed to algorithm as dict, where keys are string parameter names. Value can be number, enum value (represented by its name), or nested set of parameters - then it’s encoded in format “[key1=val1,key2=val2]”.
For documentation on options for every particular algorithm, refer to libDAI documentation and look for Properties class nested in class implementing that inference algorithm.
If not specified, default options will be used. You can access default options for all algorithms as LibDaiInterop.DEFAULT_OPTIONS. These options aren’t “recommended”, they just specify minimal set of options which makes all algorithms work.
Methods
__init__
()infer
(model, algorithm[, options])Inference with libDAI.
Checks whether LibDAI is ready to work.
max_likelihood
(model, algorithm[, options])Calculates most likely state with libDAI.
write_fg_file
(model, file_path)Writes model to FG file.
Attributes
All algorithms.
Default options for all algorithms.
Algorithms which can be used for Maximum Likelihood problem.
- ALL_ALGORITHMS = ['BP', 'CBP', 'DECMAP', 'EXACT', 'FBP', 'GIBBS', 'GLC', 'HAK', 'JTREE', 'LC', 'MF', 'MR', 'TREEEP', 'TRWBP']
All algorithms.
- DEFAULT_OPTIONS = {'BP': {'logdomain': 0, 'tol': 1e-09, 'updates': 'SEQRND'}, 'CBP': {'bbp_cfn': 'CFN_GIBBS_B', 'bbp_props': '[]', 'choose': 'CHOOSE_RANDOM', 'clamp': 'CLAMP_VAR', 'maxiter': 10000, 'min_max_adj': 123, 'rec_tol': 1e-09, 'recursion': 'REC_FIXED', 'tol': 1e-09, 'updates': 'SEQFIX'}, 'DECMAP': {'ianame': 'BP', 'iaopts': '[tol=1e-9,logdomain=0,updates=SEQRND]'}, 'EXACT': {}, 'FBP': {'logdomain': 0, 'tol': 1e-09, 'updates': 'SEQFIX'}, 'GIBBS': {'maxiter': 10000}, 'GLC': {'cavainame': 'BP', 'cavaiopts': '[tol=1e-9,logdomain=0,updates=SEQRND]', 'cavity': 'FULL', 'inainame': 'BP', 'inaiopts': '[tol=1e-9,logdomain=0,updates=SEQRND]', 'maxiter': 10000, 'rgntype': 'SINGLE', 'tol': 1e-09, 'updates': 'SEQFIX'}, 'HAK': {'clusters': 'BETHE', 'doubleloop': 0, 'maxiter': 10000, 'tol': 1e-09}, 'JTREE': {'updates': 'HUGIN'}, 'LC': {'cavainame': 'BP', 'cavaiopts': '[tol=1e-9,logdomain=0,updates=SEQRND]', 'cavity': 'FULL', 'maxiter': 10000, 'tol': 1e-09, 'updates': 'SEQRND'}, 'MF': {'maxiter': 100, 'tol': 1e-09}, 'MR': {'inits': 'RESPPROP', 'tol': 1e-09, 'updates': 'FULL'}, 'TREEEP': {'tol': 1e-09, 'type': 'ORG'}, 'TRWBP': {'logdomain': 0, 'tol': 1e-09, 'updates': 'SEQFIX'}}
Default options for all algorithms.
- ML_ALGORITHMS = ['BP', 'DECMAP', 'EXACT', 'FBP', 'GIBBS', 'JTREE', 'TREEEP', 'TRWBP']
Algorithms which can be used for Maximum Likelihood problem.
- infer(model: GraphModel, algorithm, options: Dict[str, str] = None) InferenceResult [source]
Inference with libDAI.
Calculates logarithm of partition function and marginal probabilities.
- Parameters:
model – Model, for which to perform inference. All factors must be discrete.
model – Inference algorithm. Must be one of
LibDaiInterop.ALL_ALGORITHMS
.options – libDAI options.
- Returns:
InferenceResult containing log partition function and marginal probabilities.
- is_libdai_ready()[source]
Checks whether LibDAI is ready to work.
This means run_libdai executable is built and can be executed by OS.
- max_likelihood(model: GraphModel, algorithm, options: Dict[str, str] = None) np.ndarray [source]
Calculates most likely state with libDAI.
- Parameters:
model – Model, for which to perform inference. All factors must be discrete.
model – Inference algorithm. Must be one of
LibDaiInterop.ML_ALGORITHMS
.options – libDAI options.
- Returns:
Marginal probabilities. Array of shape (number of variables, number of variables). If variables are from different domains, second dimension will be equal to maximal domain size, and for variables having less possible values, probabilities for impossible values will be padded with zeroes.
- static write_fg_file(model: GraphModel, file_path: str)[source]
Writes model to FG file.
See format definition at https://staff.fnwi.uva.nl/j.m.mooij/libDAI/doc/fileformats.html