diff options
| author | ziejd2 | 2017-09-28 15:04:40 -0500 |
|---|---|---|
| committer | ziejd2 | 2017-09-28 15:04:40 -0500 |
| commit | 8070dc963753142bb86c4ed698d91fd623ed28e7 (patch) | |
| tree | d0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old | |
| parent | 7cc31810d53176e805532b2789955f4eedbce6bb (diff) | |
| download | BNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz | |
BNW using Octave instead of Matlab.
This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning. I am calling this BNW_1.02. It can be accessed at: compbio.uthsc.edu/BNW_1.02
Diffstat (limited to 'sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old')
8 files changed, 285 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Entries new file mode 100644 index 00000000..06598b7b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Entries @@ -0,0 +1,6 @@ +/belprop_gdl_inf_engine.m/1.1.1.1/Wed May 29 15:59:56 2002// +/belprop_inf_engine_nostr.m/1.1.1.1/Wed May 29 15:59:56 2002// +/enter_evidence.m/1.1.1.1/Wed May 29 15:59:56 2002// +/enter_evidence1.m/1.1.1.1/Wed May 29 15:59:56 2002// +/marginal_domain.m/1.1.1.1/Wed May 29 15:59:56 2002// +D diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Repository new file mode 100644 index 00000000..f6b12595 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/inference/static/@belprop_inf_engine/Old diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Root b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/belprop_gdl_inf_engine.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/belprop_gdl_inf_engine.m new file mode 100644 index 00000000..f3b84925 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/belprop_gdl_inf_engine.m @@ -0,0 +1,67 @@ +function engine = belprop_gdl_inf_engine(gdl, varargin) +% BELPROP_GDL_INF_ENGINE Make a belief propagation inference engine for a GDL graph +% engine = belprop_gdl_inf_engine(gdl_graph, ...) +% +% If the GDL graph is a tree, this will give exact results. +% +% The following optional arguments can be specified in the form of name/value pairs: +% [default in brackets] +% e.g., engine = belprop_inf_engine(gdl, 'tol', 1e-2, 'max_iter', 10) +% +% protocol - 'tree' means send messages up then down the tree, +% 'parallel' means use synchronous updates ['parallel'] +% max_iter - max. num. iterations [ 2*num_nodes ] +% momentum - weight assigned to old message in convex combination (useful for damping oscillations) [0] +% tol - tolerance used to assess convergence [1e-3] +% maximize - 1 means use max-product, 0 means use sum-product [0] + + +engine = init_fields; +engine = class(engine, 'belprop_gdl_inf_engine'); + +% set default params +N = length(gdl.G); +engine.protocol = 'parallel'; +engine.max_iter = 2*N; +engine.momentum = 0; +engine.tol = 1e-3; +engine.maximize = 0; + +engine = set_params(engine, varargin); + +engine.gdl = gdl; + +if strcmp(engine.protocol, 'tree') + % Make a rooted tree, so there is a fixed message passing order. + root = N; + [engine.tree, engine.preorder, engine.postorder, height, cyclic] = mk_rooted_tree(gdl.G, root); + assert(~cyclic); +end + +% store results computed by enter_evidence here +ndoms = length(gdl.doms); +nvars = length(gdl.vars); +engine.marginal_domains = cell(1, ndoms); + +% to compute the marginal on each variable, we need to know which domain to marginalize +% and we want to choose the lightest. We compute the weight once we have seen the evidence. +engine.dom_weight = []; +engine.evidence = []; + + +%%%%%%%%% + +function engine = init_fields() + +engine.protocol = []; +engine.gdl = []; +engine.max_iter = []; +engine.momentum = []; +engine.tol = []; +engine.maximize = []; +engine.marginal_domains = []; +engine.evidence = []; +engine.tree = []; +engine.preorder = []; +engine.postorder = []; +engine.dom_weight = []; diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/belprop_inf_engine_nostr.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/belprop_inf_engine_nostr.m new file mode 100644 index 00000000..8219a868 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/belprop_inf_engine_nostr.m @@ -0,0 +1,31 @@ +function engine = belprop_inf_engine(fg, max_iter, momentum, tol, maximize) + +if nargin < 2, max_iter = length(fg.G); end +if nargin < 3, momentum = 0; end +if nargin < 4, tol = 1e-3; end +if nargin < 5, maximize = 0; end + +engine.fgraph = fg; +engine.max_iter = max_iter; +engine.momentum = momentum; +engine.tol = tol; +engine.maximize = maximize; + +% store results computed by enter_evidence here +ndoms = length(fg.doms); +nvars = length(fg.vars); +engine.marginal_domains = cell(1, ndoms); + +% to compute the marginal on each variable, we need to know which domain to marginalize +% so we represent each domain as a bit vector, and compute its (pre-evidence) weight +engine.dom_weight = []; + +% engine.dom_bitv = sparse(ndoms, nvars); +% ns = fg.node_sizes; +% for i=1:ndoms +% engine.dom_bitv(i, fg.doms{i}) = 1; +% engine.dom_weight(i) = prod(ns(fg.doms{i})); +% end + + +engine = class(engine, 'belprop_inf_engine'); diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/enter_evidence.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/enter_evidence.m new file mode 100644 index 00000000..54649557 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/enter_evidence.m @@ -0,0 +1,80 @@ +function engine = enter_evidence(engine, evidence) + +doms = engine.fg.doms; +ndoms = length(doms); +ns = engine.fg.node_sizes; +obs = find(~isemptycell(evidence)); +cobs = myintersect(obs, engine.fg.cnodes); +dobs = myintersect(obs, engine.fg.dnodes); +ns(cobs) = 0; +ns(dobs) = 1; + +% prime each local kernel with evidence (if any) +local_kernel = cell(1, ndoms); +for i=1:length(engine.fg.kernels_of_type) + u = engine.fg.kernels_of_type{i}; + local_kernel(u) = kernel_to_dpots(engine.fg.kernels{i}, evidence, engine.fg.domains_of_type{i}); +end + +% initialise all msgs to 1s +nedges = engine.fg.nedges; +msg = cell(1, nedges); +for i=1:nedges + msg{i} = dpot(engine.fg.sepset{i}, ns(engine.fg.sepset{i})); +end + +prod_of_msg = cell(1, ndoms); +bel = cell(1, ndoms); +old_bel = cell(1, ndoms); + +converged = 0; +iter = 1; +while ~converged & (iter <= engine.max_iter) + + % each node multiplies all its incoming msgs + for i=1:ndoms + prod_of_msg{i} = dpot(doms{i}, ns(doms{i})); + nbrs = engine.fg.nbrs{i}; + for j=1:length(nbrs) + ndx = engine.fg.edge_ndx(j,i); + prod_of_msg{i} = multiply_by_pot(prod_of_msg{i}, msg{ndx}); + end + end + old_msg = msg; + + % each node computes its local belief + for i=1:ndoms + bel{i} = normalize_pot(multiply_pots(prod_of_msg{i}, local_kernel{i})); + end + + % converged? + converged = 1; + for i=1:ndoms + if ~approxeq(bel{i}, old_bel{i}, engine.tol) + converged = 0; + break; + end + end + + if ~converged + % each node sends a msg to each of its neighbors + for i=1:ndoms + nbrs = engine.fg.nbrs{i}; + for j=1:length(nbrs) + % multiply all incoming msgs except from j + temp = prod_of_msg{i}; + ndx = engine.fg.edge_ndx(j,i); + temp = divide_by_pot(temp, old_msg{ndx}); + % send msg from i to j + temp = multiply_by_pot(temp, local_kernel{i}); + ndx = engine.fg.edge_ndx(i,j); + msg{ndx} = normalize_pot(marginalize_pot(temp, engine.fg.sepset{ndx})); + end + end + end + + iter = iter + 1; +end + + +engine.marginal = bel; diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/enter_evidence1.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/enter_evidence1.m new file mode 100644 index 00000000..b38cd3cb --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/enter_evidence1.m @@ -0,0 +1,94 @@ +function engine = enter_evidence(engine, evidence) + +doms = engine.fgraph.doms; +ndoms = length(doms); +ns = engine.fgraph.node_sizes; +obs = find(~isemptycell(evidence)); +cobs = myintersect(obs, engine.fgraph.cnodes); +dobs = myintersect(obs, engine.fgraph.dnodes); +ns(cobs) = 0; +ns(dobs) = 1; + +% recompute the weight of each domain now that we know what nodes are observed +for i=1:ndoms + engine.dom_weight(i) = prod(ns(engine.fgraph.doms{i})); +end + +% prime each local kernel with evidence (if any) +local_kernel = cell(1, ndoms); +for i=1:length(engine.fgraph.kernels_of_type) + u = engine.fgraph.kernels_of_type{i}; + local_kernel(u) = kernel_to_dpots(engine.fgraph.kernels{i}, evidence, engine.fgraph.domains_of_type{i}); +end + +% initialise all msgs to 1s +msg = cell(ndoms, ndoms); +for i=1:ndoms + nbrs = engine.fgraph.nbrs{i}; + for j=nbrs(:)' + dom = engine.fgraph.sepset{i,j}; + msg{i,j} = dpot(dom, ns(dom)); + end +end + +prod_of_msg = cell(1, ndoms); +bel = cell(1, ndoms); +old_bel = cell(1, ndoms); + +converged = 0; +iter = 1; +while ~converged & (iter <= engine.max_iter) + + % each node multiplies all its incoming msgs + for i=1:ndoms + prod_of_msg{i} = dpot(doms{i}, ns(doms{i})); + nbrs = engine.fgraph.nbrs{i}; + for j=nbrs(:)' + prod_of_msg{i} = multiply_by_pot(prod_of_msg{i}, msg{j,i}); + end + end + + % each node computes its local belief + old_bel = bel; + for i=1:ndoms + bel{i} = normalize_pot(multiply_pots(prod_of_msg{i}, local_kernel{i})); + end + + % converged? + if iter==1 + converged = 0; + else + converged = 1; + for i=1:ndoms + belT = get_params(bel{i}, 'table'); + old_belT = get_params(old_bel{i}, 'table'); + if ~approxeq(belT, old_belT, engine.tol) + converged = 0; + break; + end + end + end + + if ~converged + old_msg = msg; + % each node sends a msg to each of its neighbors + for i=1:ndoms + nbrs = engine.fgraph.nbrs{i}; + for j=nbrs(:)' + % multiply all incoming msgs except from j + temp = prod_of_msg{i}; + temp = divide_by_pot(temp, old_msg{j,i}); + % send msg from i to j + temp = multiply_by_pot(temp, local_kernel{i}); + msg{i,j} = normalize_pot(marginalize_pot(temp, engine.fgraph.sepset{i,j})); + end + end + end + + iter = iter + 1 +end + +engine.marginal_domains = bel; +%for i=1:ndoms + %engine.marginal_domains{i} = get_params(bel{i}, 'table'); +%end diff --git a/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/marginal_domain.m b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/marginal_domain.m new file mode 100644 index 00000000..49ad94c5 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@belprop_inf_engine/Old/marginal_domain.m @@ -0,0 +1,5 @@ +function marginal = marginal_domain(engine, i) +% MARGINAL_DOMAIN Return the marginal on the specified domain (belprop) +% marginal = marginal_domain(engine, i) + +marginal = pot_to_marginal(engine.marginal_domains{i}); |
