annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/HME/fhme.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 risultati = fhme(net, nodes_info, data, n)
Daniel@0 2 %HMEFWD Forward propagation through an HME model
Daniel@0 3 %
Daniel@0 4 % Each row of the (n x class_num) matrix 'risultati' containes the estimated class posterior prob.
Daniel@0 5 %
Daniel@0 6 % ----------------------------------------------------------------------------------------------------
Daniel@0 7 % -> pierpaolo_b@hotmail.com or -> pampo@interfree.it
Daniel@0 8 % ----------------------------------------------------------------------------------------------------
Daniel@0 9 %
Daniel@0 10 ns=net.node_sizes;
Daniel@0 11 if nargin==3
Daniel@0 12 ndata=n;
Daniel@0 13 else
Daniel@0 14 ndata=size(data, 1);
Daniel@0 15 end
Daniel@0 16 altezza=size(ns,2);
Daniel@0 17 coeff=cell(altezza-1,1);
Daniel@0 18 for m=1:ndata
Daniel@0 19 %- i=2 --------------------------------------------------------------------------------------
Daniel@0 20 s=struct(net.CPD{2});
Daniel@0 21 if nodes_info(1,2)==0,
Daniel@0 22 mu=[]; W=[]; predict=[];
Daniel@0 23 mu=s.mean(:,:);
Daniel@0 24 W=s.weights(:,:,:);
Daniel@0 25 predict=mu(:,:)+W(:,:,:)*data(m,:)';
Daniel@0 26 coeff{1,1}=predict';
Daniel@0 27 elseif nodes_info(1,2)==1,
Daniel@0 28 coeff{1,1}=fglm(s.glim{1}, data(m,:));
Daniel@0 29 else,
Daniel@0 30 coeff{1,1}=fmlp(s.mlp{1}, data(m,:));
Daniel@0 31 end
Daniel@0 32 %----------------------------------------------------------------------------------------------
Daniel@0 33 if altezza>3,
Daniel@0 34 for i=3:altezza-1,
Daniel@0 35 s=[]; f=[]; dpsz=[];
Daniel@0 36 f=family(net.dag,i); f=f(2:end-1); dpsz=prod(ns(f));
Daniel@0 37 s=struct(net.CPD{i});
Daniel@0 38 for j=1:dpsz,
Daniel@0 39 if nodes_info(1,i)==1,
Daniel@0 40 coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fglm(s.glim{j}, data(m,:));
Daniel@0 41 else
Daniel@0 42 coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fmlp(s.mlp{j}, data(m,:));
Daniel@0 43 end
Daniel@0 44 end
Daniel@0 45 app=cat(2, coeff{i-1,1}(:)); coeff{i-1,1}=app'; clear app;
Daniel@0 46 end
Daniel@0 47 end
Daniel@0 48 %- i=altezza ----------------------------------------------------------------------------------
Daniel@0 49 if altezza>2,
Daniel@0 50 i=altezza;
Daniel@0 51 s=[]; f=[]; dpsz=[];
Daniel@0 52 f=family(net.dag,i); f=f(2:end-1); dpsz=prod(ns(f));
Daniel@0 53 s=struct(net.CPD{i});
Daniel@0 54 if nodes_info(1,i)==0,
Daniel@0 55 mu=[]; W=[];
Daniel@0 56 mu=s.mean(:,:);
Daniel@0 57 W=s.weights(:,:,:);
Daniel@0 58 end
Daniel@0 59 for j=1:dpsz,
Daniel@0 60 if nodes_info(1,i)==0,
Daniel@0 61 predict=[];
Daniel@0 62 predict=mu(:,j)+W(:,:,j)*data(m,:)';
Daniel@0 63 coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*predict';
Daniel@0 64 elseif nodes_info(1,i)==1,
Daniel@0 65 coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fglm(s.glim{j}, data(m,:));
Daniel@0 66 else
Daniel@0 67 coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fmlp(s.mlp{j}, data(m,:));
Daniel@0 68 end
Daniel@0 69 end
Daniel@0 70 end
Daniel@0 71 %----------------------------------------------------------------------------------------------
Daniel@0 72 risultati(m,:)=sum(coeff{altezza-1,1},1);
Daniel@0 73 clear coeff; coeff=cell(altezza-1,1);
Daniel@0 74 end
Daniel@0 75 return
Daniel@0 76
Daniel@0 77 %-------------------------------------------------------------------
Daniel@0 78
Daniel@0 79 function [y, a] = fglm(net, x)
Daniel@0 80 %GLMFWD Forward propagation through 1-layer net->GLM statistical model
Daniel@0 81
Daniel@0 82 ndata = size(x, 1);
Daniel@0 83
Daniel@0 84 a = x*net.w1 + ones(ndata, 1)*net.b1;
Daniel@0 85
Daniel@0 86 nout = size(a,2);
Daniel@0 87 % Ensure that sum(exp(a), 2) does not overflow
Daniel@0 88 maxcut = log(realmax) - log(nout);
Daniel@0 89 % Ensure that exp(a) > 0
Daniel@0 90 mincut = log(realmin);
Daniel@0 91 a = min(a, maxcut);
Daniel@0 92 a = max(a, mincut);
Daniel@0 93 temp = exp(a);
Daniel@0 94 y = temp./(sum(temp, 2)*ones(1,nout));
Daniel@0 95
Daniel@0 96 %-------------------------------------------------------------------
Daniel@0 97
Daniel@0 98 function [y, z, a] = fmlp(net, x)
Daniel@0 99 %MLPFWD Forward propagation through 2-layer network.
Daniel@0 100
Daniel@0 101 ndata = size(x, 1);
Daniel@0 102
Daniel@0 103 z = tanh(x*net.w1 + ones(ndata, 1)*net.b1);
Daniel@0 104 a = z*net.w2 + ones(ndata, 1)*net.b2;
Daniel@0 105 temp = exp(a);
Daniel@0 106 nout = size(a,2);
Daniel@0 107 y = temp./(sum(temp,2)*ones(1,nout));
Daniel@0 108
Daniel@0 109 %-------------------------------------------------------------------