wolffd@0: % dataset -> (1=>user data) or (2=>toy example) wolffd@0: % type -> (1=> Regression model) or (2=>Classification model) wolffd@0: % num_glevel -> number of hidden nodes in the net (gating levels) wolffd@0: % num_exp -> number of experts in the net wolffd@0: % branch_fact -> dimension of the hidden nodes in the net wolffd@0: % cov_dim -> root node dimension wolffd@0: % res_dim -> output node dimension wolffd@0: % nodes_info -> 4 x num_glevel+2 matrix that contain all the info about the nodes: wolffd@0: % nodes_info(1,:) = nodes type: (0=>gaussian)or(1=>softmax)or(2=>mlp) wolffd@0: % nodes_info(2,:) = nodes size: [cov_dim num_glevel x branch_fact res_dim] wolffd@0: % nodes_info(3,:) = hidden units number (for mlp nodes) wolffd@0: % |- optimizer iteration number (for softmax & mlp CPD) wolffd@0: % nodes_info(4,:) =|- covariance type (for gaussian CPD)-> wolffd@0: % | (1=>Full)or(2=>Diagonal)or(3=>Full&Tied)or(4=>Diagonal&Tied) wolffd@0: % fh1 -> Figure: data & decizion boundaries; fh2 -> confusion matrix; fh3 -> LL trace wolffd@0: % test_data -> test data matrix wolffd@0: % train_data -> training data matrix wolffd@0: % ntrain -> size(train_data,2) wolffd@0: % ntest -> size(test_data,2) wolffd@0: % cases -> (cell array) training data formatted for the learning engine wolffd@0: % bnet -> bayesian net before learning wolffd@0: % bnet2 -> bayesian net after learning wolffd@0: % ll -> log-likelihood before learning wolffd@0: % LL2 -> log-likelihood trace wolffd@0: % onodes -> obs nodes in bnet & bnet2 wolffd@0: % max_em_iter -> maximum number of interations of the EM algorithm wolffd@0: % train_result -> prediction on the training set (as test_result) wolffd@0: % wolffd@0: % IMPORTANT: CHECK the loading path (lines 64 & 364) wolffd@0: % ---------------------------------------------------------------------------------------------------- wolffd@0: % -> pierpaolo_b@hotmail.com or -> pampo@interfree.it wolffd@0: % ---------------------------------------------------------------------------------------------------- wolffd@0: wolffd@0: error('this no longer works with the latest version of BNT') wolffd@0: wolffd@0: clear all; wolffd@0: clc; wolffd@0: disp('---------------------------------------------------'); wolffd@0: disp(' Hierarchical Mixtures of Experts models builder '); wolffd@0: disp('---------------------------------------------------'); wolffd@0: disp(' ') wolffd@0: disp(' Using this script you can build both an HME model') wolffd@0: disp('as in [Wat94] and [Jor94] i.e. with ''softmax'' gating') wolffd@0: disp('nodes and ''gaussian'' ( for regression ) or ''softmax''') wolffd@0: disp('( for classification ) expert node, and its variants') wolffd@0: disp('called ''gated nets'' where we use ''mlp'' models in') wolffd@0: disp('place of a number of ''softmax'' ones [Mor98], [Wei95].') wolffd@0: disp(' You can decide to train and test the model on your') wolffd@0: disp('datasets or to evaluate its performance on a toy') wolffd@0: disp('example.') wolffd@0: disp(' ') wolffd@0: disp('Reference') wolffd@0: disp('[Mor98] P. Moerland (1998):') wolffd@0: disp(' Localized mixtures of experts. (http://www.idiap.ch/~perry/)') wolffd@0: disp('[Jor94] M.I. Jordan, R.A. Jacobs (1994):') wolffd@0: disp(' HME and the EM algorithm. (http://www.cs.berkeley.edu/~jordan/)') wolffd@0: disp('[Wat94] S.R. Waterhouse, A.J. Robinson (1994):') wolffd@0: disp(' Classification using HME. (http://www.oigeeza.com/steve/)') wolffd@0: disp('[Wei95] A.S. Weigend, M. Mangeas (1995):') wolffd@0: disp(' Nonlinear gated experts for time series.') wolffd@0: disp(' ') wolffd@0: wolffd@0: if 0 wolffd@0: disp('(See the figure)') wolffd@0: pause(5); wolffd@0: %%%%%WARNING!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: im_path=which('HMEforMatlab.jpg'); wolffd@0: fig=imread(im_path, 'jpg'); wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: figure('Units','pixels','MenuBar','none','NumberTitle','off', 'Name', 'HME model'); wolffd@0: image(fig); wolffd@0: axis image; wolffd@0: axis off; wolffd@0: clear fig; wolffd@0: set(gca,'Position',[0 0 1 1]) wolffd@0: disp('(Press any key to continue)') wolffd@0: pause wolffd@0: end wolffd@0: wolffd@0: clc wolffd@0: disp('---------------------------------------------------'); wolffd@0: disp(' Specify the Architecture '); wolffd@0: disp('---------------------------------------------------'); wolffd@0: disp(' '); wolffd@0: disp('What kind of model do you need?') wolffd@0: disp(' ') wolffd@0: disp('1) Regression ') wolffd@0: disp('2) Classification') wolffd@0: disp(' ') wolffd@0: type=input('1 or 2?: '); wolffd@0: if (isempty(type)|(~ismember(type,[1 2]))), error('Invalid value'); end wolffd@0: clc wolffd@0: disp('----------------------------------------------------'); wolffd@0: disp(' Specify the Architecture '); wolffd@0: disp('----------------------------------------------------'); wolffd@0: disp(' ') wolffd@0: disp('Now you have to set the number of experts and gating') wolffd@0: disp('levels in the net. This script builds only balanced') wolffd@0: disp('hierarchy with the same branching factor (>1)at each') wolffd@0: disp('(gating) level. So remember that: ') wolffd@0: disp(' ') wolffd@0: disp(' num_exp = branch_fact^num_glevel ') wolffd@0: disp(' ') wolffd@0: disp('with branch_fact >=2.') wolffd@0: disp('You can also set to zeros the number of gating level') wolffd@0: disp('in order to obtain a classical GLM model. ') wolffd@0: disp(' ') wolffd@0: disp('----------------------------------------------------'); wolffd@0: disp(' ') wolffd@0: num_glevel=input('Insert the number of gating levels {0,...,20}: '); wolffd@0: if (isempty(num_glevel)|(~ismember(num_glevel,[0:20]))), error('Invalid value'); end wolffd@0: nodes_info=zeros(4,num_glevel+2); wolffd@0: if num_glevel>0, %------------------------------------------------------------------------------------ wolffd@0: for i=2:num_glevel+1, wolffd@0: clc wolffd@0: disp('----------------------------------------------------'); wolffd@0: disp(' Specify the Architecture '); wolffd@0: disp('----------------------------------------------------'); wolffd@0: disp(' ') wolffd@0: disp(['-> Gating network ', num2str(i-1), ' is a: ']) wolffd@0: disp(' ') wolffd@0: disp(' 1) Softmax model'); wolffd@0: disp(' 2) Two layer perceptron model') wolffd@0: disp(' ') wolffd@0: nodes_info(1,i)=input('1 or 2?: '); wolffd@0: if (isempty(nodes_info(1,i))|(~ismember(nodes_info(1,i),[1 2]))), error('Invalid value'); end wolffd@0: disp(' ') wolffd@0: if nodes_info(1,i)==2, wolffd@0: nodes_info(3,i)=input('Insert the number of units in the hidden layer: '); wolffd@0: if (isempty(nodes_info(3,i))|(floor(nodes_info(3,i))~=nodes_info(3,i))|(nodes_info(3,i)<=0)), wolffd@0: error(['Invalid value: ', num2str(nodes_info(3,i)), ' is not a positive integer!']); wolffd@0: end wolffd@0: disp(' ') wolffd@0: end wolffd@0: nodes_info(4,i)=input('Insert the optimizer iteration number: '); wolffd@0: if (isempty(nodes_info(4,i))|(floor(nodes_info(4,i))~=nodes_info(4,i))|(nodes_info(4,i)<=0)), wolffd@0: error(['Invalid value: ', num2str(nodes_info(4,i)), ' is not a positive integer!']); wolffd@0: end wolffd@0: end wolffd@0: clc wolffd@0: disp('---------------------------------------------------------'); wolffd@0: disp(' Specify the Architecture '); wolffd@0: disp('---------------------------------------------------------'); wolffd@0: disp(' ') wolffd@0: disp('Now you have to set the number of experts in the network'); wolffd@0: disp('The value will be adjusted in order to obtain a hierarchy'); wolffd@0: disp('as said above.') wolffd@0: disp(' '); wolffd@0: num_exp=input(['Insert the approximative number of experts (>=', num2str(2^num_glevel), '): ']); wolffd@0: if (isempty(num_exp)|(num_exp<=0)|(num_exp<2^num_glevel)), wolffd@0: error('Invalid value'); wolffd@0: end wolffd@0: app1=0; base=2; wolffd@0: while app1=(2^num_glevel)&(abs(app2-num_exp)0------------------------------------------------------------------------- wolffd@0: wolffd@0: if type==2, wolffd@0: disp(['-> Expert node is a: ']) wolffd@0: disp(' ') wolffd@0: disp(' 1) Softmax model'); wolffd@0: disp(' 2) Two layer perceptron model') wolffd@0: disp(' ') wolffd@0: nodes_info(1,end)=input('1 or 2?: '); wolffd@0: if (isempty(nodes_info(1,end))|(~ismember(nodes_info(1,end),[1 2]))), wolffd@0: error('Invalid value'); wolffd@0: end wolffd@0: disp(' ') wolffd@0: if nodes_info(1,end)==2, wolffd@0: nodes_info(3,end)=input('Insert the number of units in the hidden layer: '); wolffd@0: if (isempty(nodes_info(3,end))|(floor(nodes_info(3,end))~=nodes_info(3,end))|(nodes_info(3,end)<=0)), wolffd@0: error(['Invalid value: ', num2str(nodes_info(3,end)), ' is not a positive integer!']); wolffd@0: end wolffd@0: disp(' ') wolffd@0: end wolffd@0: nodes_info(4,end)=input('Insert the optimizer iteration number: '); wolffd@0: if (isempty(nodes_info(4,end))|(floor(nodes_info(4,end))~=nodes_info(4,end))|(nodes_info(4,end)<=0)), wolffd@0: error(['Invalid value: ', num2str(nodes_info(4,end)), ' is not a positive integer!']); wolffd@0: end wolffd@0: elseif type==1, wolffd@0: disp('What kind of covariance matrix structure do you want?') wolffd@0: disp(' ') wolffd@0: disp(' 1) Full'); wolffd@0: disp(' 2) Diagonal') wolffd@0: disp(' 3) Full & Tied'); wolffd@0: disp(' 4) Diagonal & Tied') wolffd@0: wolffd@0: disp(' ') wolffd@0: nodes_info(4,end)=input('1, 2, 3 or 4?: '); wolffd@0: if (isempty(nodes_info(4,end))|(~ismember(nodes_info(4,end),[1 2 3 4]))), wolffd@0: error('Invalid value'); wolffd@0: end wolffd@0: end wolffd@0: clc wolffd@0: disp('----------------------------------------------------'); wolffd@0: disp(' Specify the Input '); wolffd@0: disp('----------------------------------------------------'); wolffd@0: disp(' ') wolffd@0: disp('Do you want to...') wolffd@0: disp(' ') wolffd@0: disp('1) ...use your own dataset?') wolffd@0: disp('2) ...apply the model on a toy example?') wolffd@0: disp(' ') wolffd@0: dataset=input('1 or 2?: '); wolffd@0: if (isempty(dataset)|(~ismember(dataset,[1 2]))), error('Invalid value'); end wolffd@0: if dataset==1, wolffd@0: if type==1, wolffd@0: clc wolffd@0: disp('-------------------------------------------------------'); wolffd@0: disp(' Specify the Input - Regression problem '); wolffd@0: disp('-------------------------------------------------------'); wolffd@0: disp(' ') wolffd@0: disp('Be sure that each row of your data matrix is an example'); wolffd@0: disp('with the covariate values that precede the respond ones') wolffd@0: disp(' ') wolffd@0: disp('-------------------------------------------------------'); wolffd@0: disp(' ') wolffd@0: cov_dim=input('Insert the covariate space dimension: '); wolffd@0: if (isempty(cov_dim)|(floor(cov_dim)~=cov_dim)|(cov_dim<=0)), wolffd@0: error(['Invalid value: ', num2str(cov_dim), ' is not a positive integer!']); wolffd@0: end wolffd@0: disp(' ') wolffd@0: res_dim=input('Insert the dimension of the respond variable: '); wolffd@0: if (isempty(res_dim)|(floor(res_dim)~=res_dim)|(res_dim<=0)), wolffd@0: error(['Invalid value: ', num2str(res_dim), ' is not a positive integer!']); wolffd@0: end wolffd@0: disp(' '); wolffd@0: elseif type==2 wolffd@0: clc wolffd@0: disp('-------------------------------------------------------'); wolffd@0: disp(' Specify the Input - Classification problem '); wolffd@0: disp('-------------------------------------------------------'); wolffd@0: disp(' ') wolffd@0: disp('Be sure that each row of your data matrix is an example'); wolffd@0: disp('with the covariate values that precede the class labels'); wolffd@0: disp('(integer value >=1). '); wolffd@0: disp(' ') wolffd@0: disp('-------------------------------------------------------'); wolffd@0: disp(' ') wolffd@0: cov_dim=input('Insert the covariate space dimension: '); wolffd@0: if (isempty(cov_dim)|(floor(cov_dim)~=cov_dim)|(cov_dim<=0)), wolffd@0: error(['Invalid value: ', num2str(cov_dim), ' is not a positive integer!']); wolffd@0: end wolffd@0: disp(' ') wolffd@0: res_dim=input('Insert the number of classes: '); wolffd@0: if (isempty(res_dim)|(floor(res_dim)~=res_dim)|(res_dim<=0)), wolffd@0: error(['Invalid value: ', num2str(res_dim), ' is not a positive integer!']); wolffd@0: end wolffd@0: disp(' ') wolffd@0: end wolffd@0: % ------------------------------------------------------------------------------------------------ wolffd@0: % Loading training data -------------------------------------------------------------------------- wolffd@0: % ------------------------------------------------------------------------------------------------ wolffd@0: train_path=input('Insert the complete (with extension) path of the training data file:\n >> ','s'); wolffd@0: if isempty(train_path), error('You must specify a data set for training!'); end wolffd@0: if ~isempty(findstr('.mat',train_path)), wolffd@0: ap=load(train_path); app=fieldnames(ap); train_data=eval(['ap.', app{1,1}]); wolffd@0: clear ap app; wolffd@0: elseif ~isempty(findstr('.txt',train_path)), wolffd@0: train_data=load(train_path, '-ascii'); wolffd@0: else wolffd@0: error('Invalid data format: not a .mat or a .txt file') wolffd@0: end wolffd@0: if (size(train_data,2)~=cov_dim+res_dim)&(type==1), wolffd@0: error(['Invalid data matrix size: ', num2str(size(train_data,2)), ' columns rather than ',... wolffd@0: num2str(cov_dim+res_dim),'!']); wolffd@0: elseif (size(train_data,2)~=cov_dim+1)&(type==2), wolffd@0: error(['Invalid data matrix size: ', num2str(size(train_data,2)), ' columns rather than ',... wolffd@0: num2str(cov_dim+1),'!']); wolffd@0: elseif (~isempty(find(ismember(intersect([train_data(:,end)' 1:res_dim],... wolffd@0: train_data(:,end)'),[1:res_dim])==0)))&(type==2), wolffd@0: error('Invalid class label'); wolffd@0: end wolffd@0: ntrain=size(train_data,1); wolffd@0: train_d=train_data(:,1:cov_dim); wolffd@0: if type==2, wolffd@0: train_t=zeros(ntrain, res_dim); wolffd@0: for m=1:res_dim, wolffd@0: train_t((find(train_data(:,end)==m))',m)=1; wolffd@0: end wolffd@0: else wolffd@0: train_t=train_data(:,cov_dim+1:end); wolffd@0: end wolffd@0: disp(' ') wolffd@0: % ------------------------------------------------------------------------------------------------ wolffd@0: % Loading test data ------------------------------------------------------------------------------ wolffd@0: % ------------------------------------------------------------------------------------------------ wolffd@0: disp('(If you don''t want to specify a test-set press ''return'' only)'); wolffd@0: test_path=input('Insert the complete (with extension) path of the test data file:\n >> ','s'); wolffd@0: if ~isempty(test_path), wolffd@0: if ~isempty(findstr('.mat',test_path)), wolffd@0: ap=load(test_path); app=fieldnames(ap); test_data=eval(['ap.', app{1,1}]); wolffd@0: clear ap app; wolffd@0: elseif ~isempty(findstr('.txt',test_path)), wolffd@0: test_data=load(test_path, '-ascii'); wolffd@0: else wolffd@0: error('Invalid data format: not a .mat or a .txt file') wolffd@0: end wolffd@0: if (size(test_data,2)~=cov_dim)&(size(test_data,2)~=cov_dim+res_dim)&(type==1), wolffd@0: error(['Invalid data matrix size: ', num2str(size(test_data,2)), ' columns rather than ',... wolffd@0: num2str(cov_dim+res_dim), ' or ', num2str(cov_dim), '!']); wolffd@0: elseif (size(test_data,2)~=cov_dim)&(size(test_data,2)~=cov_dim+1)&(type==2), wolffd@0: error(['Invalid data matrix size: ', num2str(size(test_data,2)), ' columns rather than ',... wolffd@0: num2str(cov_dim+1), ' or ', num2str(cov_dim), '!']); wolffd@0: elseif (~isempty(find(ismember(intersect([test_data(:,end)' 1:res_dim],... wolffd@0: test_data(:,end)'),[1:res_dim])==0)))&(type==2)&(size(test_data,2)==cov_dim+1), wolffd@0: error('Invalid class label'); wolffd@0: end wolffd@0: ntest=size(test_data,1); wolffd@0: test_d=test_data(:,1:cov_dim); wolffd@0: if (type==2)&(size(test_data,2)>cov_dim), wolffd@0: test_t=zeros(ntest, res_dim); wolffd@0: for m=1:res_dim, wolffd@0: test_t((find(test_data(:,end)==m))',m)=1; wolffd@0: end wolffd@0: elseif (type==1)&(size(test_data,2)>cov_dim), wolffd@0: test_t=test_data(:,cov_dim+1:end); wolffd@0: end wolffd@0: disp(' '); wolffd@0: end wolffd@0: else wolffd@0: clc wolffd@0: disp('----------------------------------------------------'); wolffd@0: disp(' Specify the Input '); wolffd@0: disp('----------------------------------------------------'); wolffd@0: disp(' ') wolffd@0: ntrain = input('Insert the number of examples in training (<500): '); wolffd@0: if (isempty(ntrain)|(floor(ntrain)~=ntrain)|(ntrain<=0)|(ntrain>500)), wolffd@0: error(['Invalid value: ', num2str(ntrain), ' is not a positive integer <500!']); wolffd@0: end wolffd@0: disp(' ') wolffd@0: test_path='toy'; wolffd@0: ntest = input('Insert the number of examples in test (<500): '); wolffd@0: if (isempty(ntest)|(floor(ntest)~=ntest)|(ntest<=0)|(ntest>500)), wolffd@0: error(['Invalid value: ', num2str(ntest), ' is not a positive integer <500!']); wolffd@0: end wolffd@0: wolffd@0: if type==2, wolffd@0: cov_dim=2; wolffd@0: res_dim=3; wolffd@0: seed = 42; wolffd@0: [train_d, ntrain1, ntrain2, train_t]=gen_data(ntrain, seed); wolffd@0: for m=1:ntrain wolffd@0: q=[]; q = find(train_t(m,:)==1); wolffd@0: train_data(m,:)=[train_d(m,:) q]; wolffd@0: end wolffd@0: [test_d, ntest1, ntest2, test_t]=gen_data(ntest); wolffd@0: for m=1:ntest wolffd@0: q=[]; q = find(test_t(m,:)==1); wolffd@0: test_data(m,:)=[test_d(m,:) q]; wolffd@0: end wolffd@0: else wolffd@0: cov_dim=1; wolffd@0: res_dim=1; wolffd@0: global HOME wolffd@0: %%%%%WARNING!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: load([HOME '/examples/static/Misc/mixexp_data.txt'], '-ascii'); wolffd@0: %%%%%WARNING!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: train_data = mixexp_data(1:ntrain, :); wolffd@0: train_d=train_data(:,1:cov_dim); train_t=train_data(:,cov_dim+1:end); wolffd@0: test_data = mixexp_data(ntrain+1:ntrain+ntest, :); wolffd@0: test_d=test_data(:,1:cov_dim); wolffd@0: if size(test_data,2)>cov_dim, wolffd@0: test_t=test_data(:,cov_dim+1:end); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: % Set the nodes dimension----------------------------------- wolffd@0: if num_glevel>0, wolffd@0: nodes_info(2,2:num_glevel+1)=branch_fact; wolffd@0: end wolffd@0: nodes_info(2,1)=cov_dim; nodes_info(2,end)=res_dim; wolffd@0: %----------------------------------------------------------- wolffd@0: % Prepare the training data for the learning engine--------- wolffd@0: %----------------------------------------------------------- wolffd@0: cases = cell(size(nodes_info,2), ntrain); wolffd@0: for m=1:ntrain, wolffd@0: cases{1,m}=train_data(m,1:cov_dim)'; wolffd@0: cases{end,m}=train_data(m,cov_dim+1:end)'; wolffd@0: end wolffd@0: %----------------------------------------------------------------------------------------------------- wolffd@0: [bnet onodes]=hme_topobuilder(nodes_info); wolffd@0: engine = jtree_inf_engine(bnet, onodes); wolffd@0: clc wolffd@0: disp('---------------------------------------------------------------------'); wolffd@0: disp(' L E A R N I N G '); wolffd@0: disp('---------------------------------------------------------------------'); wolffd@0: disp(' ') wolffd@0: ll = 0; wolffd@0: for l=1:ntrain wolffd@0: scritta=['example number: ', int2str(l),'---------------------------------------------']; wolffd@0: disp(scritta); wolffd@0: ev = cases(:,l); wolffd@0: [engine, loglik] = enter_evidence(engine, ev); wolffd@0: ll = ll + loglik; wolffd@0: end wolffd@0: disp(' ') wolffd@0: disp(['Log-likelihood before learning: ', num2str(ll)]); wolffd@0: disp(' ') wolffd@0: disp('(Press any key to continue)'); wolffd@0: pause wolffd@0: %----------------------------------------------------------- wolffd@0: clc wolffd@0: disp('---------------------------------------------------------------------'); wolffd@0: disp(' L E A R N I N G '); wolffd@0: disp('---------------------------------------------------------------------'); wolffd@0: disp(' ') wolffd@0: max_em_iter=input('Insert the maximum number of the EM algorithm iterations: '); wolffd@0: if (isempty(max_em_iter)|(floor(max_em_iter)~=max_em_iter)|(max_em_iter<=1)), wolffd@0: error(['Invalid value: ', num2str(ntest), ' is not a positive integer >1!']); wolffd@0: end wolffd@0: disp(' ') wolffd@0: disp(['Log-likelihood before learning: ', num2str(ll)]); wolffd@0: disp(' ') wolffd@0: wolffd@0: [bnet2, LL2] = learn_params_em(engine, cases, max_em_iter); wolffd@0: disp(' ') wolffd@0: fprintf('HME: loglik before learning %f, after %d iters %f\n', ll, length(LL2), LL2(end)); wolffd@0: disp(' ') wolffd@0: disp('(Press any key to continue)'); wolffd@0: pause wolffd@0: %----------------------------------------------------------------------------------- wolffd@0: % Classification problem: plot data & decision boundaries if the input data size = 2 wolffd@0: % Regression problem: plot data & prediction if the input data size = 1 wolffd@0: %----------------------------------------------------------------------------------- wolffd@0: if (type==2)&(nodes_info(2,1)==2)&(~isempty(test_path)), wolffd@0: fh1=hme_class_plot(bnet2, nodes_info, train_data, test_data); wolffd@0: disp(' '); wolffd@0: disp('(See the figure)'); wolffd@0: elseif (type==2)&(nodes_info(2,1)==2)&(isempty(test_path)), wolffd@0: fh1=hme_class_plot(bnet2, nodes_info, train_data); wolffd@0: disp(' '); wolffd@0: disp('(See the figure)'); wolffd@0: elseif (type==1)&(nodes_info(2,1)==1)&(~isempty(test_path)), wolffd@0: fh1=hme_reg_plot(bnet2, nodes_info, train_data, test_data); wolffd@0: disp(' '); wolffd@0: disp('(See the figure)'); wolffd@0: elseif (type==1)&(nodes_info(2,1)==1)&(isempty(test_path)), wolffd@0: fh1=hme_reg_plot(bnet2, nodes_info, train_data); wolffd@0: disp(' ') wolffd@0: disp('(See the figure)'); wolffd@0: end wolffd@0: %----------------------------------------------------------------------------------- wolffd@0: % Classification problem: plot confusion matrix wolffd@0: %----------------------------------------------------------------------------------- wolffd@0: if (type==2) wolffd@0: ztrain=fhme(bnet2, nodes_info, train_d, size(train_d,1)); wolffd@0: [Htrain, trainRate]=confmat(ztrain, train_t); % CM on the training set wolffd@0: fh2=figure('Name','Confusion matrix', 'MenuBar', 'none', 'NumberTitle', 'off'); wolffd@0: if (~isempty(test_path))&(size(test_data,2)>cov_dim), wolffd@0: ztest=fhme(bnet2, nodes_info, test_d, size(test_d,1)); wolffd@0: [Htest, testRate]=confmat(ztest, test_t); % CM on the test set wolffd@0: subplot(1,2,1); wolffd@0: end wolffd@0: plotmat(Htrain,'b','k',12) wolffd@0: tick=[0.5:1:(0.5+nodes_info(2,end)-1)]; wolffd@0: set(gca,'XTick',tick) wolffd@0: set(gca,'YTick',tick) wolffd@0: grid('off') wolffd@0: ylabel('True') wolffd@0: xlabel('Prediction') wolffd@0: title(['Confusion Matrix: training set (' num2str(trainRate(1)) '%)']) wolffd@0: if (~isempty(test_path))&(size(test_data,2)>cov_dim), wolffd@0: subplot(1,2,2) wolffd@0: plotmat(Htest,'b','k',12) wolffd@0: set(gca,'XTick',tick) wolffd@0: set(gca,'YTick',tick) wolffd@0: grid('off') wolffd@0: ylabel('True') wolffd@0: xlabel('Prediction') wolffd@0: title(['Confusion Matrix: test set (' num2str(testRate(1)) '%)']) wolffd@0: end wolffd@0: disp(' ') wolffd@0: disp('(Press any key to continue)'); wolffd@0: pause wolffd@0: end wolffd@0: %----------------------------------------------------------------------------------- wolffd@0: % Regression & Classification problem: calculate the predictions & plot the LL trace wolffd@0: %----------------------------------------------------------------------------------- wolffd@0: train_result=fhme(bnet2,nodes_info,train_d,size(train_d,1)); wolffd@0: if ~isempty(test_path), wolffd@0: test_result=fhme(bnet2,nodes_info,test_d,size(test_d,1)); wolffd@0: end wolffd@0: fh3=figure('Name','Log-likelihood trace', 'MenuBar', 'none', 'NumberTitle', 'off') wolffd@0: plot(LL2,'-ro',... wolffd@0: 'MarkerEdgeColor','k',... wolffd@0: 'MarkerFaceColor',[1 1 0],... wolffd@0: 'MarkerSize',4) wolffd@0: title('Log-likelihood trace') wolffd@0: %----------------------------------------------------------------------------------- wolffd@0: % Regression & Classification problem: save the predictions wolffd@0: %----------------------------------------------------------------------------------- wolffd@0: clc wolffd@0: disp('------------------------------------------------------------------'); wolffd@0: disp(' Save the results '); wolffd@0: disp('------------------------------------------------------------------'); wolffd@0: disp(' ') wolffd@0: %----------------------------------------------------------------------------------- wolffd@0: save_quest_m=input('Do you want to save the HME model (Y/N)? [Y default]: ', 's'); wolffd@0: if isempty(save_quest_m), wolffd@0: save_quest_m='Y'; wolffd@0: end wolffd@0: if ~findstr(save_quest_m, ['Y', 'N']), error('Invalid input'); end wolffd@0: if save_quest_m=='Y', wolffd@0: disp(' '); wolffd@0: m_save=input('Insert the complete path for save the HME model (.mat):\n >> ', 's'); wolffd@0: if isempty(m_save), error('You must specify a path!'); end wolffd@0: save(m_save, 'bnet2'); wolffd@0: end wolffd@0: %----------------------------------------------------------------------------------- wolffd@0: disp(' ') wolffd@0: save_quest=input('Do you want to save the HME predictions (Y/N)? [Y default]: ', 's'); wolffd@0: disp(' ') wolffd@0: if isempty(save_quest), wolffd@0: save_quest='Y'; wolffd@0: end wolffd@0: if ~findstr(save_quest, ['Y', 'N']), error('Invalid input'); end wolffd@0: if save_quest=='Y', wolffd@0: tr_save=input('Insert the complete path for save the training data prediction (.mat):\n >> ', 's'); wolffd@0: if isempty(tr_save), error('You must specify a path!'); end wolffd@0: save(tr_save, 'train_result'); wolffd@0: if ~isempty(test_path), wolffd@0: disp(' ') wolffd@0: te_save=input('Insert the complete path for save the test data prediction (.mat):\n >> ', 's'); wolffd@0: if isempty(te_save), error('You must specify a path!'); end wolffd@0: save(te_save, 'test_result'); wolffd@0: end wolffd@0: end wolffd@0: clc wolffd@0: disp('----------------------------------------------------'); wolffd@0: disp(' B Y E ! '); wolffd@0: disp('----------------------------------------------------'); wolffd@0: pause(2) wolffd@0: %clear wolffd@0: clc