enter_soft_evidence.m

来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· M 代码 · 共 138 行

M
138
字号
function [clpot, loglik] = enter_soft_evidence(engine, CPDpot)% ENTER_SOFT_EVIDENCE Add the specified soft evidence to the network (jtree_ndx_dbn)% [clpot, loglik] = enter_soft_evidence(engine, CPDpot)verbose = 0;[ss T] = size(CPDpot);Q = length(engine.jtree_struct.cliques);clpot = cell(Q,T); % clpot{t} contains evidence from slices (t-1, t) seppot = cell(Q,Q,T);ll = zeros(1,Q);logscale = zeros(1,T);root = engine.jtree_struct.root_clq;% Forwards pass.% Compute distribution on clq C,% where C is the out interface to (t-1,t).% Then pass this to clq D, where D is the in inferface to (t+1,t).% Then propagate from D to later slices.cliques = engine.jtree_struct.cliques;cliques1 = engine.jtree_struct1.cliques;ns = engine.eff_node_sizes;ndx_type = engine.ndx_type;maximize = engine.maximize;marg_ndx = get_ndx(engine.marg_root_onto_int2_ndx_id, ndx_type);slice1 = 1:ss;slice2 = slice1 + ss;transient = engine.transient;persist = engine.persist;if verbose, fprintf('forward pass\n'); endfor t=1:T  if verbose, fprintf('%d ', t); end  if t==1    nodes = [slice1 persist+ss];    CPDs = [CPDpot(:,1); CPDpot(persist, 2)];    clqs = []; pots = {}; ndx = {};  elseif t==T    nodes = transient;    CPDs = CPDpot(transient, t);    clqs = engine.in_clq1;    pots = {phi};    ndx = {get_ndx(engine.mult_int_onto_inclq1_ndx_id, ndx_type)};  elseif t==2     CPDs = [CPDpot(transient, t); CPDpot(persist, t+1)];    pots = {phi};    nodes = [transient persist+ss];    clqs = engine.in_clq;    ndx = {get_ndx(engine.mult_int_onto_inclq_ndx_id, ndx_type)};  else    CPDs = [CPDpot(transient, t); CPDpot(persist, t+1)];    pots = {phi};    % Calling get_ndx is slow, so we use the same values as for t=2    %nodes = [transient persist+ss];    %clqs = engine.in_clq;    %ndx = {get_ndx(engine.mult_int_onto_inclq_ndx_id, ndx_type)};  end  if t < T    [clpot(1:Q,t), seppot(1:Q,1:Q,t)] =  init_pot(engine.jtree_engine, nodes, CPDs, clqs, pots, ndx);    [clpot(1:Q,t), seppot(1:Q,1:Q,t)] = collect_evidence(engine.jtree_engine, clpot(1:Q,t), seppot(1:Q,1:Q,t));  else    Q = length(engine.jtree_struct1.cliques);    root = engine.jtree_struct1.root_clq;    [clpot(1:Q,t), seppot(1:Q,1:Q,t)] =  init_pot(engine.jtree_engine1, nodes, CPDs, clqs, pots, ndx);    [clpot(1:Q,t), seppot(1:Q,1:Q,t)] = collect_evidence(engine.jtree_engine1, clpot(1:Q,t), seppot(1:Q,1:Q,t));  end    for c=1:Q    [clpot{c,t}, lik] = normalise(clpot{c,t});    ll(c) = log(lik + (lik==0)*eps);   end  logscale(t) = ll(root);  if t < T    phi = marg_table_ndx(clpot{root,t}, maximize, marg_ndx, ndx_type);  endendloglik = sum(logscale);ndx1 = get_ndx(engine.marg_outclq_onto_int2_ndx_id, ndx_type);ndx2 = get_ndx(engine.mult_int_onto_int_ndx_id, ndx_type);ndx3 = get_ndx(engine.mult_int2_onto_outclq_ndx_id, ndx_type);marg_ndxT = get_ndx(engine.marg_inclq1_onto_int_ndx_id, ndx_type);marg_ndx = get_ndx(engine.marg_inclq_onto_int_ndx_id, ndx_type);% Backwards pass.% Pass evidence from clq C to clq D,% where C is the in interface to (t,t+1) and D is the out inferface to (t-1,t)% Then propagate evidence from D to earlier slices.% (C and D are reversed names from the tech report!)D = engine.out_clq;if verbose, fprintf('\nbackwards pass\n'); endfor t=T:-1:1  if verbose, fprintf('%d ', t); end    if t == T    Q = length(engine.jtree_struct1.cliques);    C = engine.in_clq1;    [clpot(1:Q,t), seppot(1:Q,1:Q,t)] = distribute_evidence(engine.jtree_engine1, clpot(1:Q,t), seppot(1:Q,1:Q,t));  else    Q = length(engine.jtree_struct.cliques);    C = engine.in_clq;    [clpot(1:Q,t), seppot(1:Q,1:Q,t)] = distribute_evidence(engine.jtree_engine, clpot(1:Q,t), seppot(1:Q,1:Q,t));  end  for c=1:Q    [clpot{c,t}, lik] = normalise(clpot{c,t});  end  if  t >= 2    %phiC = marginalize_pot(clpot{C,t}, engine.interface, maximize);    if t==T      phiC = marg_table_ndx(clpot{C,t}, maximize, marg_ndxT, ndx_type);    else      phiC = marg_table_ndx(clpot{C,t}, maximize, marg_ndx, ndx_type);    end        %phiD = marginalize_pot(clpot{D,t-1}, engine.interface+ss, maximize);    phiD = marg_table_ndx(clpot{D,t-1}, maximize, ndx1, ndx_type);        %ratio = divide_by_pot(phiC, phiD);    ratio = divide_by_table_ndx(phiC, phiD, ndx2, ndx_type);        %clpot{D,t-1} = multiply_by_pot(clpot{D,t-1}, ratio); % where ratio is in slice 2!    clpot{D,t-1} = mult_by_table_ndx(clpot{D,t-1}, ratio, ndx3, ndx_type);  endendif verbose, fprintf('\n'); end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?