我正在尝试使用Matlab中的贝叶斯网络工具箱(BNT)创建动态贝叶斯网络。我已经仔细阅读了本教程,最后得到了以下代码:
T=2;
names = {'X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9', 'X10', 'X11', 'X12'};
ss = length(names);
onodes=[1 2 3 4 5 6 7 8 9 10 11 12];
intrac = {
'X1', 'X2';
'X3', 'X4';
'X6', 'X7';
'X7', 'X8';
'X10', 'X11';
'X10', 'X12';
'X11', 'X12';
};
[intra, names] = mk_adj_mat(intrac, names, 1);
interc = {
'X1', 'X1';
'X2', 'X2';
'X3', 'X3';
'X4', 'X4';
'X5', 'X5';
'X6', 'X6';
'X7', 'X7';
'X8', 'X8';
'X9', 'X9';
'X10', 'X10';
'X11', 'X11';
'X12', 'X12';
};
inter = mk_adj_mat(interc, names, 0);
dnodes = 1:ss;
ns = [3 3 3 2 4 5 2 2 4 5 4 5];
ns(stringmatch('X1',names))=3;
ns(stringmatch('X2',names))=3;
ns(stringmatch('X3',names))=3;
ns(stringmatch('X4',names))=2;
ns(stringmatch('X5',names))=4;
ns(stringmatch('X6',names))=5;
ns(stringmatch('X7',names))=2;
ns(stringmatch('X8',names))=2;
ns(stringmatch('X9',names))=4;
ns(stringmatch('X10',names))=5;
ns(stringmatch('X11',names))=4;
ns(stringmatch('X12',names))=5;
bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes);
for i=1:2*ss
bnet.CPD{i} = tabular_CPD(bnet, i);
end
allcases=importdata('Myfile.txt',',');
ncases = size(allcases.data,1);
cases = cell(1, ncases);
for i=1:ncases
cases{i} = cell(ss,T);
dataentry=allcases.data(i,:);
ev=uint8([dataentry(1:12)' dataentry(13:24)']);
cases{i} = num2cell(ev);
end
engine2 = smoother_engine(jtree_2TBN_inf_engine(bnet));
%engine2 = jtree_2TBN_inf_engine(bnet);
[bnt, LL, engine2] = learn_params_dbn_em(engine2, cases, 'max_iter', 20);
当我尝试运行它时,会出现以下错误:
Subscript indices must either be real positive integers or logicals.
Error in discrete_CPD/convert_to_table (line 14)
T = CPT(index{:});
Error in discrete_CPD/convert_to_pot (line 20)
T = convert_to_table(CPD, domain, evidence);
Error in jtree_2TBN_inf_engine/fwd1 (line 11)
CPDpot{n} = convert_to_pot(bnet.CPD{e}, engine.pot_type, fam(:), ev);
Error in smoother_engine/enter_evidence (line 12)
[f{1}, ll(1)] = fwd1(engine.tbn_engine, ev(:,1), 1);
Error in learn_params_dbn_em>EM_step (line 131)
[engine, ll] = enter_evidence(engine, evidence);
Error in learn_params_dbn_em (line 82)
[engine, loglik, logpost] = EM_step(engine, evidence, temperature);
有人可以帮我解决这个错误吗?我认为这与我创建表格CPD的方式有关,因为当我查看bnet中的CPD时,它们都是1 * 1表格。我的CPD创建方式与教程相同。 提前谢谢!
答案 0 :(得分:0)
问题解决了,数据集有问题。在我的数据集中,我使用一个单元格表示所有数据条目,而不是每个数据条目使用一个单元格。 现在代码工作正常。