about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/BNT/examples/static/HME/fhme.m
diff options
context:
space:
mode:
authorziejd22017-09-28 15:04:40 -0500
committerziejd22017-09-28 15:04:40 -0500
commit8070dc963753142bb86c4ed698d91fd623ed28e7 (patch)
treed0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/bnt-master/BNT/examples/static/HME/fhme.m
parent7cc31810d53176e805532b2789955f4eedbce6bb (diff)
downloadBNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz
BNW using Octave instead of Matlab.
This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning.

I am calling this BNW_1.02. It can be accessed at:
compbio.uthsc.edu/BNW_1.02
Diffstat (limited to 'sourcecodes/bnt-master/BNT/examples/static/HME/fhme.m')
-rw-r--r--sourcecodes/bnt-master/BNT/examples/static/HME/fhme.m109
1 files changed, 109 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/examples/static/HME/fhme.m b/sourcecodes/bnt-master/BNT/examples/static/HME/fhme.m
new file mode 100644
index 00000000..6aeb7196
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/static/HME/fhme.m
@@ -0,0 +1,109 @@
+function risultati = fhme(net, nodes_info, data, n)
+%HMEFWD	Forward propagation through an HME model
+%
+% Each row of the (n x class_num) matrix 'risultati' containes the estimated class posterior prob.
+%
+% ----------------------------------------------------------------------------------------------------
+% -> pierpaolo_b@hotmail.com   or   -> pampo@interfree.it
+% ----------------------------------------------------------------------------------------------------
+%
+ns=net.node_sizes;
+if nargin==3
+    ndata=n;
+else
+    ndata=size(data, 1);
+end
+altezza=size(ns,2);
+coeff=cell(altezza-1,1);
+for m=1:ndata
+    %- i=2 --------------------------------------------------------------------------------------
+    s=struct(net.CPD{2});    
+    if nodes_info(1,2)==0,
+        mu=[]; W=[]; predict=[];
+        mu=s.mean(:,:);
+        W=s.weights(:,:,:);
+        predict=mu(:,:)+W(:,:,:)*data(m,:)';            
+        coeff{1,1}=predict';            
+    elseif nodes_info(1,2)==1,
+        coeff{1,1}=fglm(s.glim{1}, data(m,:));
+    else,
+        coeff{1,1}=fmlp(s.mlp{1}, data(m,:));
+    end
+    %----------------------------------------------------------------------------------------------
+    if altezza>3,
+        for i=3:altezza-1,
+            s=[]; f=[]; dpsz=[];
+            f=family(net.dag,i); f=f(2:end-1); dpsz=prod(ns(f));
+            s=struct(net.CPD{i});
+            for j=1:dpsz,
+                if nodes_info(1,i)==1,
+                    coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fglm(s.glim{j}, data(m,:));
+                else
+                    coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fmlp(s.mlp{j}, data(m,:));
+                end
+            end       
+            app=cat(2, coeff{i-1,1}(:)); coeff{i-1,1}=app'; clear app;
+        end
+    end
+    %- i=altezza ----------------------------------------------------------------------------------
+    if altezza>2,
+        i=altezza;
+        s=[]; f=[]; dpsz=[];
+        f=family(net.dag,i); f=f(2:end-1); dpsz=prod(ns(f));
+        s=struct(net.CPD{i});
+        if nodes_info(1,i)==0,            
+            mu=[]; W=[];
+            mu=s.mean(:,:);
+            W=s.weights(:,:,:);
+        end
+        for j=1:dpsz,
+            if nodes_info(1,i)==0,            
+                predict=[];
+                predict=mu(:,j)+W(:,:,j)*data(m,:)';            
+                coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*predict';            
+            elseif nodes_info(1,i)==1,
+                coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fglm(s.glim{j}, data(m,:));
+            else
+                coeff{i-1,1}(j,:)=coeff{i-2,1}(1,j)*fmlp(s.mlp{j}, data(m,:));
+            end
+        end
+    end
+    %----------------------------------------------------------------------------------------------
+    risultati(m,:)=sum(coeff{altezza-1,1},1);
+    clear coeff; coeff=cell(altezza-1,1);
+end
+return
+
+%-------------------------------------------------------------------
+
+function [y, a] = fglm(net, x)
+%GLMFWD	Forward propagation through 1-layer net->GLM statistical model
+
+ndata = size(x, 1);
+
+a = x*net.w1 + ones(ndata, 1)*net.b1;
+
+nout = size(a,2);
+% Ensure that sum(exp(a), 2) does not overflow
+maxcut = log(realmax) - log(nout);
+% Ensure that exp(a) > 0
+mincut = log(realmin);
+a = min(a, maxcut);
+a = max(a, mincut);
+temp = exp(a);
+y = temp./(sum(temp, 2)*ones(1,nout));
+
+%-------------------------------------------------------------------
+
+function [y, z, a] = fmlp(net, x)
+%MLPFWD	Forward propagation through 2-layer network.
+
+ndata = size(x, 1);
+
+z = tanh(x*net.w1 + ones(ndata, 1)*net.b1);
+a = z*net.w2 + ones(ndata, 1)*net.b2;  
+temp = exp(a);
+nout = size(a,2);
+y = temp./(sum(temp,2)*ones(1,nout));
+
+%-------------------------------------------------------------------