init_msgs.m

来自「Bayes网络工具箱」· M 代码 · 共 56 行

M
56
字号
function msg =  init_msgs(dag, ns, evidence, rand_init)% INIT_MSGS Initialize the lambda/pi message and state vectors% msg =  init_msgs(dag, ns, evidence, rand_init)%% We assume all the nodes are discrete.% The initial values of the msgs don't matter in the distributed version, since they "wash out".% In the centralized version, they should be set to 1s.if nargin < 4, rand_init = 0; endN = length(dag);msg = cell(1,N);observed = ~isemptycell(evidence);for n=1:N  ps = parents(dag, n);  msg{n}.pi_from_parent = cell(1, length(ps));  for i=1:length(ps)    p = ps(i);    if rand_init      msg{n}.pi_from_parent{i} = rand(ns(p), 1);    else      msg{n}.pi_from_parent{i} = ones(ns(p), 1);    end  end    cs = children(dag, n);  msg{n}.lambda_from_child = cell(1, length(cs));  for i=1:length(cs)    c = cs(i);    if rand_init      msg{n}.lambda_from_child{i} = rand(ns(n), 1);    else      msg{n}.lambda_from_child{i} = ones(ns(n), 1);    end  end  if rand_init    msg{n}.lambda = rand(ns(n), 1);    msg{n}.pi = rand(ns(n), 1);  else    msg{n}.lambda = ones(ns(n), 1);    msg{n}.pi = ones(ns(n), 1);  end    % Initialize the lambdas with any evidence  if observed(n)    v = evidence{n};    msg{n}.lambda_from_self = zeros(ns(n), 1);    msg{n}.lambda_from_self(v) = 1; % delta function  else    msg{n}.lambda_from_self = ones(ns(n), 1);  endend

⌨️ 快捷键说明

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