annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@mlp_CPD/convert_to_table.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function T = convert_to_table(CPD, domain, evidence)
Daniel@0 2 % CONVERT_TO_TABLE Convert a mlp CPD to a table, incorporating any evidence
Daniel@0 3 % T = convert_to_table(CPD, domain, evidence)
Daniel@0 4
Daniel@0 5 self = domain(end);
Daniel@0 6 ps = domain(1:end-1); % self' parents
Daniel@0 7 %cps = myintersect(ps, cnodes); % self' continous parents
Daniel@0 8 cnodes = domain(CPD.cpndx);
Daniel@0 9 cps = myintersect(ps, cnodes);
Daniel@0 10 odom = domain(~isemptycell(evidence(domain))); % obs nodes in the net
Daniel@0 11 assert(myismember(cps, odom)); % !ALL the CTS parents must be observed!
Daniel@0 12 ns(cps)=1;
Daniel@0 13 dps = mysetdiff(ps, cps); % self' discrete parents
Daniel@0 14 dobs = myintersect(dps, odom); % discrete obs parents
Daniel@0 15
Daniel@0 16 % Extract the params compatible with the observations (if any) on the discrete parents (if any)
Daniel@0 17
Daniel@0 18 if ~isempty(dobs),
Daniel@0 19 dvals = cat(1, evidence{dobs});
Daniel@0 20 ns_eff= CPD.sizes; % effective node sizes
Daniel@0 21 ens=ns_eff;
Daniel@0 22 ens(dobs) = 1;
Daniel@0 23 S=prod(ens(dps));
Daniel@0 24 subs = ind2subv(ens(dps), 1:S);
Daniel@0 25 mask = find_equiv_posns(dobs, dps);
Daniel@0 26 for i=1:length(mask),
Daniel@0 27 subs(:,mask(i)) = dvals(i);
Daniel@0 28 end
Daniel@0 29 support = subv2ind(ns_eff(dps), subs)';
Daniel@0 30 else
Daniel@0 31 ns_eff= CPD.sizes;
Daniel@0 32 support=[1:prod(ns_eff(dps))];
Daniel@0 33 end
Daniel@0 34
Daniel@0 35 W1=[]; b1=[]; W2=[]; b2=[];
Daniel@0 36
Daniel@0 37 W1 = CPD.W1(:,:,support);
Daniel@0 38 b1= CPD.b1(support,:);
Daniel@0 39 W2 = CPD.W2(:,:,support);
Daniel@0 40 b2= CPD.b2(support,:);
Daniel@0 41 ns(odom) = 1;
Daniel@0 42 dpsize = prod(ns(dps)); % overall size of the self' discrete parents
Daniel@0 43
Daniel@0 44 x = cat(1, evidence{cps});
Daniel@0 45 ndata=size(x,2);
Daniel@0 46
Daniel@0 47 if ~isempty(evidence{self}) %
Daniel@0 48 app=struct(CPD); %
Daniel@0 49 ns(self)=app.mlp{1}.nout; % pump up self to the original dimension if observed
Daniel@0 50 clear app; %
Daniel@0 51 end %
Daniel@0 52
Daniel@0 53 T =zeros(dpsize, ns(self)); %
Daniel@0 54 for i=1:dpsize %
Daniel@0 55 W1app = W1(:,:,i); %
Daniel@0 56 b1app = b1(i,:); %
Daniel@0 57 W2app = W2(:,:,i); %
Daniel@0 58 b2app = b2(i,:); % for each of the dpsize combinations of self'parents values
Daniel@0 59 z = tanh(x(:)'*W1app + ones(ndata, 1)*b1app); % we tabulate the corrisponding glm model
Daniel@0 60 a = z*W2app + ones(ndata, 1)*b2app; % (element of the cell array CPD.glim)
Daniel@0 61 appoggio = normalise(exp(a)); %
Daniel@0 62 T(i,:)=appoggio; %
Daniel@0 63 W1app=[]; W2app=[]; b1app=[]; b2app=[]; %
Daniel@0 64 z=[]; a=[]; appoggio=[]; %
Daniel@0 65 end %
Daniel@0 66
Daniel@0 67 if ~isempty(evidence{self})
Daniel@0 68 appoggio=[]; %
Daniel@0 69 appoggio=zeros(1,ns(self)); %
Daniel@0 70 r = evidence{self}; %...if self is observed => in output there's only the probability of the 'true' class
Daniel@0 71 for i=1:dpsize %
Daniel@0 72 appoggio(i)=T(i,r); %
Daniel@0 73 end
Daniel@0 74 T=zeros(dpsize,1);
Daniel@0 75 for i=1:dpsize
Daniel@0 76 T(i,1)=appoggio(i);
Daniel@0 77 end
Daniel@0 78 clear appoggio;
Daniel@0 79 ns(self) = 1;
Daniel@0 80 end