To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / _FullBNT / BNT / CPDs / @mlp_CPD / convert_to_table.m @ 8:b5b38998ef3b
History | View | Annotate | Download (3.46 KB)
| 1 |
function T = convert_to_table(CPD, domain, evidence) |
|---|---|
| 2 |
% CONVERT_TO_TABLE Convert a mlp CPD to a table, incorporating any evidence |
| 3 |
% T = convert_to_table(CPD, domain, evidence) |
| 4 |
|
| 5 |
self = domain(end); |
| 6 |
ps = domain(1:end-1); % self' parents |
| 7 |
%cps = myintersect(ps, cnodes); % self' continous parents |
| 8 |
cnodes = domain(CPD.cpndx); |
| 9 |
cps = myintersect(ps, cnodes); |
| 10 |
odom = domain(~isemptycell(evidence(domain))); % obs nodes in the net |
| 11 |
assert(myismember(cps, odom)); % !ALL the CTS parents must be observed! |
| 12 |
ns(cps)=1; |
| 13 |
dps = mysetdiff(ps, cps); % self' discrete parents |
| 14 |
dobs = myintersect(dps, odom); % discrete obs parents |
| 15 |
|
| 16 |
% Extract the params compatible with the observations (if any) on the discrete parents (if any) |
| 17 |
|
| 18 |
if ~isempty(dobs), |
| 19 |
dvals = cat(1, evidence{dobs});
|
| 20 |
ns_eff= CPD.sizes; % effective node sizes |
| 21 |
ens=ns_eff; |
| 22 |
ens(dobs) = 1; |
| 23 |
S=prod(ens(dps)); |
| 24 |
subs = ind2subv(ens(dps), 1:S); |
| 25 |
mask = find_equiv_posns(dobs, dps); |
| 26 |
for i=1:length(mask), |
| 27 |
subs(:,mask(i)) = dvals(i); |
| 28 |
end |
| 29 |
support = subv2ind(ns_eff(dps), subs)'; |
| 30 |
else |
| 31 |
ns_eff= CPD.sizes; |
| 32 |
support=[1:prod(ns_eff(dps))]; |
| 33 |
end |
| 34 |
|
| 35 |
W1=[]; b1=[]; W2=[]; b2=[]; |
| 36 |
|
| 37 |
W1 = CPD.W1(:,:,support); |
| 38 |
b1= CPD.b1(support,:); |
| 39 |
W2 = CPD.W2(:,:,support); |
| 40 |
b2= CPD.b2(support,:); |
| 41 |
ns(odom) = 1; |
| 42 |
dpsize = prod(ns(dps)); % overall size of the self' discrete parents |
| 43 |
|
| 44 |
x = cat(1, evidence{cps});
|
| 45 |
ndata=size(x,2); |
| 46 |
|
| 47 |
if ~isempty(evidence{self}) %
|
| 48 |
app=struct(CPD); % |
| 49 |
ns(self)=app.mlp{1}.nout; % pump up self to the original dimension if observed
|
| 50 |
clear app; % |
| 51 |
end % |
| 52 |
|
| 53 |
T =zeros(dpsize, ns(self)); % |
| 54 |
for i=1:dpsize % |
| 55 |
W1app = W1(:,:,i); % |
| 56 |
b1app = b1(i,:); % |
| 57 |
W2app = W2(:,:,i); % |
| 58 |
b2app = b2(i,:); % for each of the dpsize combinations of self'parents values |
| 59 |
z = tanh(x(:)'*W1app + ones(ndata, 1)*b1app); % we tabulate the corrisponding glm model |
| 60 |
a = z*W2app + ones(ndata, 1)*b2app; % (element of the cell array CPD.glim) |
| 61 |
appoggio = normalise(exp(a)); % |
| 62 |
T(i,:)=appoggio; % |
| 63 |
W1app=[]; W2app=[]; b1app=[]; b2app=[]; % |
| 64 |
z=[]; a=[]; appoggio=[]; % |
| 65 |
end % |
| 66 |
|
| 67 |
if ~isempty(evidence{self})
|
| 68 |
appoggio=[]; % |
| 69 |
appoggio=zeros(1,ns(self)); % |
| 70 |
r = evidence{self}; %...if self is observed => in output there's only the probability of the 'true' class
|
| 71 |
for i=1:dpsize % |
| 72 |
appoggio(i)=T(i,r); % |
| 73 |
end |
| 74 |
T=zeros(dpsize,1); |
| 75 |
for i=1:dpsize |
| 76 |
T(i,1)=appoggio(i); |
| 77 |
end |
| 78 |
clear appoggio; |
| 79 |
ns(self) = 1; |
| 80 |
end |