%     _\/_
%      /\
%      /\
%     /  \
%     /~~\o
%    /o   \
%   /~~*~~~\
%  o/    o \
%  /~~~~~~~~\~`
% /__*_______\
%      ||
%    \====/
%     \__/
% Merry Christmas

%     Ergasia 3
%     Alexandros Rasoulis, 2015010123
    %% 

    load('MNISTdataset.mat');
    %% Erwthma 5 & 6

%   dataX - input data.
%   dataY - target data.

x = dataX';
t = dataY';

%  Training Function, Gradient descent backpropagation.
trainFcn = 'traingd';  

% Create a Pattern Recognition Network
hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize, trainFcn);

net.performFcn = 'mse';

net.trainParam.epochs = 1000;
net.trainParam.lr = 0.25;
net.trainParam.goal = 1e-8;
net.trainParam.max_fail = 10;

net.layers{1}.transferFcn = 'tansig';
net.layers{2}.transferFcn = 'logsig';

% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 60/100;
net.divideParam.valRatio = 10/100;
net.divideParam.testRatio = 30/100;

% Train the Network
[net,tr] = train(net,x,t);

% Test the Network
y = net(x);
tind = vec2ind(t);
yind = vec2ind(y);
percentErrors = sum(tind ~= yind)/numel(tind);

% Accuracy
acc = 1 - percentErrors;

%% Erwthma 7

%   dataX - input data.
%   dataY - target data.

x = dataX';
t = dataY';

%  Training Function, Gradient descent backpropagation.
trainFcn = 'traingd';  % Gradient descent backpropagation.

% Create a Pattern Recognition Network
hiddenLayerSize = [110 80];
net = patternnet(hiddenLayerSize, trainFcn);

net.performFcn = 'mse';

net.trainParam.epochs = 2000;
net.trainParam.lr = 0.2;
net.trainParam.goal = 1e-6;
net.trainParam.max_fail = 6;

net.layers{1}.transferFcn = 'tansig';
net.layers{2}.transferFcn = 'tansig';
net.layers{3}.transferFcn = 'logsig';

% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 0/100;
net.divideParam.testRatio = 30/100;

% Train the Network
[net,tr] = train(net,x,t);

% Test the Network
y = net(x);
e = gsubtract(t,y);
performance = perform(net,t,y)
tind = vec2ind(t);
yind = vec2ind(y);
percentErrors = sum(tind ~= yind)/numel(tind);

% Add the right path.
img = imgPreprocessing('C:\Users\.....\3h ergasia\photo\img3.jpg');

array = net(img);
[v number] = max(array);
number