Daniel@0: function risultati = fhme(net, nodes_info, data, n) Daniel@0: %HMEFWD Forward propagation through an HME model Daniel@0: % Daniel@0: % Each row of the (n x class_num) matrix 'risultati' containes the estimated class posterior prob. Daniel@0: % Daniel@0: % ---------------------------------------------------------------------------------------------------- Daniel@0: % -> pierpaolo_b@hotmail.com or -> pampo@interfree.it Daniel@0: % ---------------------------------------------------------------------------------------------------- Daniel@0: % Daniel@0: ns=net.node_sizes; Daniel@0: if nargin==3 Daniel@0: ndata=n; Daniel@0: else Daniel@0: ndata=size(data, 1); Daniel@0: end Daniel@0: altezza=size(ns,2); Daniel@0: coeff=cell(altezza-1,1); Daniel@0: for m=1:ndata Daniel@0: %- i=2 -------------------------------------------------------------------------------------- Daniel@0: s=struct(net.CPD{2}); Daniel@0: if nodes_info(1,2)==0, Daniel@0: mu=[]; W=[]; predict=[]; Daniel@0: mu=s.mean(:,:); Daniel@0: W=s.weights(:,:,:); Daniel@0: predict=mu(:,:)+W(:,:,:)*data(m,:)'; Daniel@0: coeff{1,1}=predict'; Daniel@0: elseif nodes_info(1,2)==1, Daniel@0: coeff{1,1}=fglm(s.glim{1}, data(m,:)); Daniel@0: else, Daniel@0: coeff{1,1}=fmlp(s.mlp{1}, data(m,:)); Daniel@0: end Daniel@0: %---------------------------------------------------------------------------------------------- Daniel@0: if altezza>3, Daniel@0: for i=3:altezza-1, Daniel@0: s=[]; f=[]; dpsz=[]; Daniel@0: f=family(net.dag,i); f=f(2:end-1); dpsz=prod(ns(f)); Daniel@0: s=struct(net.CPD{i}); Daniel@0: for j=1:dpsz, Daniel@0: if nodes_info(1,i)==1, Daniel@0: coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fglm(s.glim{j}, data(m,:)); Daniel@0: else Daniel@0: coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fmlp(s.mlp{j}, data(m,:)); Daniel@0: end Daniel@0: end Daniel@0: app=cat(2, coeff{i-1,1}(:)); coeff{i-1,1}=app'; clear app; Daniel@0: end Daniel@0: end Daniel@0: %- i=altezza ---------------------------------------------------------------------------------- Daniel@0: if altezza>2, Daniel@0: i=altezza; Daniel@0: s=[]; f=[]; dpsz=[]; Daniel@0: f=family(net.dag,i); f=f(2:end-1); dpsz=prod(ns(f)); Daniel@0: s=struct(net.CPD{i}); Daniel@0: if nodes_info(1,i)==0, Daniel@0: mu=[]; W=[]; Daniel@0: mu=s.mean(:,:); Daniel@0: W=s.weights(:,:,:); Daniel@0: end Daniel@0: for j=1:dpsz, Daniel@0: if nodes_info(1,i)==0, Daniel@0: predict=[]; Daniel@0: predict=mu(:,j)+W(:,:,j)*data(m,:)'; Daniel@0: coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*predict'; Daniel@0: elseif nodes_info(1,i)==1, Daniel@0: coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fglm(s.glim{j}, data(m,:)); Daniel@0: else Daniel@0: coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fmlp(s.mlp{j}, data(m,:)); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: %---------------------------------------------------------------------------------------------- Daniel@0: risultati(m,:)=sum(coeff{altezza-1,1},1); Daniel@0: clear coeff; coeff=cell(altezza-1,1); Daniel@0: end Daniel@0: return Daniel@0: Daniel@0: %------------------------------------------------------------------- Daniel@0: Daniel@0: function [y, a] = fglm(net, x) Daniel@0: %GLMFWD Forward propagation through 1-layer net->GLM statistical model Daniel@0: Daniel@0: ndata = size(x, 1); Daniel@0: Daniel@0: a = x*net.w1 + ones(ndata, 1)*net.b1; Daniel@0: Daniel@0: nout = size(a,2); Daniel@0: % Ensure that sum(exp(a), 2) does not overflow Daniel@0: maxcut = log(realmax) - log(nout); Daniel@0: % Ensure that exp(a) > 0 Daniel@0: mincut = log(realmin); Daniel@0: a = min(a, maxcut); Daniel@0: a = max(a, mincut); Daniel@0: temp = exp(a); Daniel@0: y = temp./(sum(temp, 2)*ones(1,nout)); Daniel@0: Daniel@0: %------------------------------------------------------------------- Daniel@0: Daniel@0: function [y, z, a] = fmlp(net, x) Daniel@0: %MLPFWD Forward propagation through 2-layer network. Daniel@0: Daniel@0: ndata = size(x, 1); Daniel@0: Daniel@0: z = tanh(x*net.w1 + ones(ndata, 1)*net.b1); Daniel@0: a = z*net.w2 + ones(ndata, 1)*net.b2; Daniel@0: temp = exp(a); Daniel@0: nout = size(a,2); Daniel@0: y = temp./(sum(temp,2)*ones(1,nout)); Daniel@0: Daniel@0: %-------------------------------------------------------------------