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