jtree_c_dbn_inf_engine.m
来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· M 代码 · 共 106 行
M
106 行
function engine = jtree_C_dbn_inf_engine(bnet, varargin)
% JTREE_DBN_INF_ENGINE Junction tree inference algorithm for DBNs.
% engine = jtree_inf_engine(bnet, ...)
%
% The following optional arguments can be specified in the form of name/value pairs:
% [default value in brackets]
%
% clusters - specifies variables that must be grouped in the 1.5 slice DBN
%
% e.g., engine = jtree_dbn_inf_engine(dbn, 'clusters', {[2 3]});
%
% See "Applying the jtree algorithm to variable-length DBNs", K. Murphy, tech report oct 2001.
%
% This function was written by Wei Hu (wei.hu@intel.com)
%
% Differences between jtree_dbn and jtree_C_dbn:
% 1. jtree_C_dbn use table to reprensent potential, while jtree_dbn use potential object.
% 2. different ways to deal with "phi" variables. In jtree_dbn, phi is an object; while in jtree_C_dbn,
% phi is a table, and call "dpot" to package into an object. And so on for vaiable "phiC" and "phiD"
ss = length(bnet.intra);
clusters = {};
if nargin >= 2
args = varargin;
nargs = length(args);
for i=1:2:nargs
switch args{i},
case 'clusters', clusters = args{i+1};
end
end
end
engine.evidence = [];
engine.node_sizes = [];
[int, engine.persist, engine.transient] = compute_interface_nodes(bnet.intra, bnet.inter);
%engine.interface = engine.persist; % WRONG!
engine.interface = int;
engine.nonint = mysetdiff(1:ss, int);
onodes = bnet.observed;
% Create a "1.5 slice" jtree, containing slice 1 and the interface nodes of slice 2
% To keep the node numbering the same, we simply disconnect the non-interface nodes
% from slice 2, and set their size to 1.
% We do this to speed things up, and so that the likelihood is computed correctly - we do not need to do
% this if we just want to compute marginals.
intra15 = bnet.intra;
for i=engine.nonint(:)'
intra15(i,:) = 0;
intra15(:,i) = 0;
end
dag15 = [bnet.intra bnet.inter;
zeros(ss) intra15];
ns = bnet.node_sizes(:);
ns(engine.nonint+ss) = 1; % disconnected nodes get size 1
obs_nodes = [onodes(:) onodes(:)+ss];
bnet15 = mk_bnet(dag15, ns, 'discrete', bnet.dnodes, 'equiv_class', bnet.equiv_class(:), ...
'observed', obs_nodes(:));
%bnet15 = mk_dbn(intra15, bnet.inter, bnet.node_sizes_slice, bnet.dnodes_slice, ...
% bnet.equiv_class(:,1), bnet.equiv_class(:,2), bnet.intra);
% with the dbn, we can't independently control the sizes of slice 2 nodes
if 1
% use unconstrained elimination,
% but force there to be a clique containing both interfaces
clusters(end+1:end+2) = {int, int+ss};
engine.jtree_engine = jtree_C_inf_engine(bnet15, 'clusters', clusters, 'root', int+ss);
else
% use constrained elimination - this induces a clique that contain the 2nd interface,
% but not the first
stages = {1:ss, [1:ss]+ss};
engine.jtree_engine = jtree_C_inf_engine(bnet15, 'stages', stages, 'root', int+ss);
end
engine.in_clq = clq_containing_nodes(engine.jtree_engine, int);
engine.out_clq = clq_containing_nodes(engine.jtree_engine, int+ss);
engine.jtree_struct = struct(engine.jtree_engine); % violate object privacy
% Also create an engine just for slice 1
bnet1 = mk_bnet(bnet.intra1, bnet.node_sizes_slice, 'discrete', myintersect(bnet.dnodes, 1:ss),...
'equiv_class', bnet.equiv_class(:,1), 'observed', onodes);
for i=1:max(bnet1.equiv_class)
bnet1.CPD{i} = bnet.CPD{i};
end
engine.jtree_engine1 = jtree_C_inf_engine(bnet1, 'clusters', {int}, 'root', int);
engine.in_clq1 = clq_containing_nodes(engine.jtree_engine1, int);
engine.jtree_struct1 = struct(engine.jtree_engine1); % violate object privacy
% stuff needed by marginal_nodes
engine.clpot = [];
engine.T = [];
engine.maximize = [];
engine = class(engine, 'jtree_C_dbn_inf_engine', inf_engine(bnet));
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?