%Multilayer Perceptron implementation Example 
% 
%Neural Nets, Spring 00, S. Mandayam 

clear;close all; 
%Define a 2-3-1 MLP with Sigmoidal Neurons at the Hidden 
%and output layers; train using gradient descent backprop 
net=newff([0 5; 0 5],[3,1],{'logsig','logsig'},'traingd'); 

%Show training progress every 50 epochs 
net.trainParam.show=50; 

%Set learning rate to 0.5 
net.trainParam.lr=0.5; 

%Set maximum epochs to 1000 
net.trainParam.epochs=1000; 

%Set error goal to 0.01 
net.trainparam.goal=1e-2; 

%Define training input and target data vectors 
p=[[1;1],[1;2],[2;1],[1.5;3.0],[2.0;2.0],[3.0;1.7]]; 
t=[0 0 0 1 1 1]; 
figure; plotpv(p,t); 

pause;

figure;
%train the network 
net=train(net,p,t); 

%Validate Traning
t_pred=sim(net,p);

%Test network wth new data
p1=[[1.5;1.5],[2.5;2.5]];
t1=[0 1];
t1_pred=sim(net,p1);

%Display training and testing results
figure;
plot3(p(1,1:3),p(2,1:3),t(1,1:3),'o');
hold;
plot3(p(1,4:6),p(2,4:6),t(1,4:6),'+');
plot3(p(1,1:3),p(2,1:3),t_pred(1,1:3),'ro');
plot3(p(1,4:6),p(2,4:6),t_pred(1,4:6),'r+');
plot3(p1(1,1),p(2,1),t1(1,1),'go');
plot3(p1(1,2),p(2,2),t1(1,2),'g+');
plot3(p1(1,1),p(2,1),t1_pred(1,1),'ko');
plot3(p1(1,2),p(2,2),t1_pred(1,2),'k+');

legend('Training: Desired, Class 1', 'Training: Desired, Class 2', 'Training: Predicted, Class 1','Training: Predicted, Class 2', 'Testing: Desired, Class 1', 'Testing: Desired, Class 2', 'Testing: Predicted, Class 1', 'Testing: Predicted, Class 2');
xlabel('Input 1');
ylabel('Input 2');
zlabel('Output');
title('MLP Results');
rotate3d on;

