%% matlab_demo_15.m
%%
%% CSS 490/590, Introduction to Machine Learning, Winter 2012
%% Computing & Software Systems, University of Washington Bothell
%% written by J. Jeffry Howbert, 2012-03-04

%% demo of neural network classification of spoken vowels

%%*************** WARNING ***************
%%** All matrices and vectors need to be transposed for
%%** neural network toolbox!  Samples are in columns and
%%** features are in rows!
%%***************************************

clear all;
close all;
format compact;

load vowel-context.data;
data = vowel_context( :, 4 : 13 );
label = vowel_context( :, 14 );
% convert class labels 0-10 to group labels 1-11 for later ease of indexing
label = grp2idx( label );
nClass = max( label );
trainData = data( vowel_context( :, 1 ) == 0, : );
trainLabel = label( vowel_context( :, 1 ) == 0, : );
testData = data( vowel_context( :, 1 ) == 1, : );
testLabel = label( vowel_context( :, 1 ) == 1, : );
nTrain = length( trainLabel );
nTest = length( testLabel );
% convert group labels to dummy variable arrays
trainLabelDum = dummyvar( trainLabel );
testLabelDum = dummyvar( testLabel );

%% All matrices and vectors need to be transposed for
%%  neural network toolbox!
trainData = trainData';
testData = testData';
trainLabel = trainLabel';
testLabel = testLabel';
trainLabelDum = trainLabelDum';
testLabelDum = testLabelDum';

nRand = 30;
nHid = 5;
hiddenTransferFcn = 'tansig';   % 'tansig' or 'logsig'
outputTransferFcn = 'tansig';   % 'purelin' or 'tansig' or 'logsig'
testPred = zeros( nClass, nTest );
for ra = 1 : nRand
    RandStream.setDefaultStream( RandStream( 'mt19937ar', 'seed', ra ) );
    net = newff( trainData, trainLabelDum, nHid );
    net.layers{ 1 }.transferFcn = hiddenTransferFcn;
    net.layers{ 2 }.transferFcn = outputTransferFcn;
    net.trainParam.showWindow = false;
    net.trainParam.showCommandLine = false;
    net = train( net, trainData, trainLabelDum );
    testPred = testPred + sim( net, testData );
end
[ ~, testLabelPred ] = max( testPred );
nTestPredCorrect = sum( testLabelPred == testLabel );
fprintf( 1, 'test set correct predictions = %d / %d\n\n', nTestPredCorrect, nTest );

[ mat, order ] = confusionmat( testLabel, testLabelPred );
disp( order' );
disp( ' ' );
disp( mat );
