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