From: Dalmar on

I have written this NN to classify different cases. Simulation is
excellent but I could not get the required target during the testing.
Please let me know your suggestion. I am desperate to solve this
problem due to submission of my postgraduate final year.
Regards
This is the Matlab code
Inline Attachment Follows: matrixdatatraining2.m

%% Neural Network Classification
% training Matrix data
load P0;load P1;load P2;load P3;load P4; % training data (Inputs)
size(Pi=909x3)
[r0 c0] = size(P0);
[r1 c1] = size(P1);
[r2 c2] = size(P2);
[r3 c3] = size(P3);
[r4 c4] = size(P4);
t0=zeros(r0,1); t1=ones(r1,1)*-2; t2=ones(r2,1)*2 ;t3=ones(r3,1)*-5;
t4=ones(r4,1)*5;

%% Taking Input
P=[P0; P1; P2; P3; P4];
T = [zeros(r0,1); ones(r1,1)*-2; ones(r2,1)*2 ;ones(r3,1)*-5;
ones(r4,1)*5]; %training data(Tatgets)

% %T=[t0, t1, t2, t3, t4];
% % T = [zeros(r0,c0,1); ones(r1,c1,1);ones(r2,c2,1)*2;
ones(r3,c3,1)*3; ones(r4,c4,1)*4];
% % T=T(1,:);
P = transpose(P);
T = transpose(T);
% [rP cP] = size(P); % 4x1058

% [rT cT] = size(T); % 1x1058
load P2a;%load P2a;load P3a;load P4a; % Testing data size(Pi=909x3)


[Pn,minp,maxp,Tn,mint,maxt]=premnmx(P,T);
[an,mina,maxa,sn,mins,maxs]=premnmx(P1a',t2');% normalization

%% Creating and training of the Neural Network
%We would like to create two layers neural net with 5 nodes in hidden
%layer.
%net = newff(minmax(P),[15 1],{'tansig','purelin'},'traingdm');

net = newff(minmax(Pn),[8,12,8,1],{'tansig','purelin','tansig',
'purelin'},'trainbr');%'trainbfg');%trainbr', trainlm);
%net = newff(P,T,[20 1],{'tansig' 'purelin'},'traingdx');
%net = newff(minmax(P),[15 1],{'tansig' 'logsig'},'trainbr');%scg
%net=newff(minmax(p),[15 1],{'tansig' 'purelin'},’traingdm’);

net.trainParam.show = 100%inf;
net.trainParam.goal = 1e-5;
net.trainParam.lr = 0.01;
net.trainparam.lr_inc = 1.05;
net.trainparam.lr_dec = 0.7;
net.trainparam.max_perfect_inc = 1.04;
net.trainparam.mc = 0.9;
net.trainparam.min_grad = 1e-10;
net.trainParam.epochs = 500;

[net,tr] = train(net,Pn,Tn);
%% Save the Network

save matrixdatatraining, net;
%% simulatation
%Y1 = sim(net,Pn);
s=t1;
y = sim(net,an);
%un-ormalization
%[a] = postmnmx(Y1,mint,maxt);
%[a1] = postmnmx(y,mins,maxs);
%a1=a1';
tt=postmnmx(y',mins,maxs);
% if you like plot the output
figure(1)
plot(tt,'r')
hold
%Current plot held
plot(t2)
title('Comparison between actual targets and predictions')



% [m,b,r] = postreg(a,T);

%% Plotting the training
%Here the network is simulated and its output plotted against
% the targets.
figure(2)

plot(s')
hold on
plot(T', 'r')
title('Comparison between actual targets and predictions')
hold off
xlabel('Epoches')
% Here the network is trained for 50 epochs. Again the network's
% output is plotted.
% figure(2)
% % net.trainParam.epochs = 50;
% % net = train(net,P,T);
% % Y = sim(net,P);
% %plot(P,T,P,Y,'o')
% plot(P,Y,'r')
% %plot(p,t,'o',p,y1,'x',p,y2,'*')
%%
%% Testing the Network
%load getmatrixdata Ptesti;
load Ptest3
PP=Ptest3;
PP = transpose(PP);
load matrixdatatraining, net;
ytest=sim(net,PP);

figure(3)
err=y-t2;
plot(err)
title('eroor between actual targets and predictions')
xlabel('Epoches')


figure(4)

x=ytest(200:900);
plot(x')

h=mean(x)
pi=mean(ytest)
%% Trials
% [net,tr]=train(net,Pn,Tn,P1,[],[],test);
% plot(tr.epoch,tr.perf,tr.epoch,tr.vperf,tr.epoch,tr.tperf)
% legend('Training','Validation','Test',-1);
% ylabel('Squared Error'); xlabel('Epoch');
%
% an = sim(net,ptrans);
% a = poststd(an,meant,stdt);
% for i=1:3
% figure(i)
% [m(i),b(i),r(i)] = postreg(a(i,:),t(i,:));
% end

% p = [-0.92 0.73 -0.47 0.74 0.29; -0.08 0.86 -0.67 -0.52 0.93];
% t = [-0.08 3.4 -0.82 0.69 3.1];
% [pn,minp,maxp,tn,mint,maxt] = premnmx(p,t);
% net = newff(minmax(pn),[5 1],{'tansig' 'purelin'},'trainlm');
% net = train(net,pn,tn);
% an = sim(net,pn);
% [a] = postmnmx(an,mint,maxt);
% [m,b,r] = postreg(a,t);
From: Dalmar on
On 4 June, 10:39, Dalmar <ybul...(a)googlemail.com> wrote:
> I have written this NN to classify different cases. Simulation is
> excellent but I could not get the required target during the testing.
> Please let me know your suggestion. I am desperate to solve this
> problem due to submission of  my postgraduate final year.
> Regards
> This is the Matlab code
> Inline Attachment Follows: matrixdatatraining2.m
>
> %% Neural Network Classification
> % training Matrix data
> load P0;load P1;load P2;load P3;load P4; % training data (Inputs)
> size(Pi=909x3)
> [r0 c0] = size(P0);
> [r1 c1] = size(P1);
> [r2 c2] = size(P2);
> [r3 c3] = size(P3);
> [r4 c4] = size(P4);
> t0=zeros(r0,1); t1=ones(r1,1)*-2; t2=ones(r2,1)*2 ;t3=ones(r3,1)*-5;
> t4=ones(r4,1)*5;
>
> %% Taking Input
> P=[P0; P1; P2; P3; P4];
> T = [zeros(r0,1); ones(r1,1)*-2; ones(r2,1)*2 ;ones(r3,1)*-5;
> ones(r4,1)*5]; %training data(Tatgets)
>
> % %T=[t0, t1, t2, t3, t4];
> % % T = [zeros(r0,c0,1); ones(r1,c1,1);ones(r2,c2,1)*2;
> ones(r3,c3,1)*3; ones(r4,c4,1)*4];
> % % T=T(1,:);
> P = transpose(P);
> T = transpose(T);
> % [rP cP] = size(P); % 4x1058
>
> % [rT cT] = size(T); % 1x1058
> load P2a;%load P2a;load P3a;load P4a; %  Testing data  size(Pi=909x3)
>
> [Pn,minp,maxp,Tn,mint,maxt]=premnmx(P,T);
> [an,mina,maxa,sn,mins,maxs]=premnmx(P1a',t2');% normalization
>
> %% Creating and training of the Neural Network
> %We would like to create two layers neural net with 5 nodes in hidden
> %layer.
> %net = newff(minmax(P),[15 1],{'tansig','purelin'},'traingdm');
>
> net = newff(minmax(Pn),[8,12,8,1],{'tansig','purelin','tansig',
> 'purelin'},'trainbr');%'trainbfg');%trainbr', trainlm);
> %net = newff(P,T,[20 1],{'tansig' 'purelin'},'traingdx');
> %net = newff(minmax(P),[15 1],{'tansig' 'logsig'},'trainbr');%scg
> %net=newff(minmax(p),[15 1],{'tansig' 'purelin'},’traingdm’);
>
> net.trainParam.show = 100%inf;
> net.trainParam.goal = 1e-5;
> net.trainParam.lr = 0.01;
> net.trainparam.lr_inc = 1.05;
> net.trainparam.lr_dec = 0.7;
> net.trainparam.max_perfect_inc = 1.04;
> net.trainparam.mc = 0.9;
> net.trainparam.min_grad = 1e-10;
> net.trainParam.epochs = 500;
>
> [net,tr] = train(net,Pn,Tn);
> %% Save the Network
>
> save matrixdatatraining, net;
> %% simulatation
> %Y1 = sim(net,Pn);
> s=t1;
> y = sim(net,an);
> %un-ormalization
> %[a] = postmnmx(Y1,mint,maxt);
> %[a1] = postmnmx(y,mins,maxs);
> %a1=a1';
> tt=postmnmx(y',mins,maxs);
> % if you like plot the output
> figure(1)
> plot(tt,'r')
> hold
> %Current plot held
> plot(t2)
> title('Comparison between actual targets and predictions')
>
> % [m,b,r] = postreg(a,T);
>
> %% Plotting the training
> %Here the network is simulated and its output plotted against
>     % the targets.
> figure(2)
>
> plot(s')
> hold on
> plot(T', 'r')
> title('Comparison between actual targets and predictions')
> hold off
>   xlabel('Epoches')
>   %  Here the network is trained for 50 epochs.  Again the network's
>   %    output is plotted.
> %  figure(2)
> % %        net.trainParam.epochs = 50;
> % %        net = train(net,P,T);
> %      % Y = sim(net,P);
> %        %plot(P,T,P,Y,'o')
> %        plot(P,Y,'r')
> %        %plot(p,t,'o',p,y1,'x',p,y2,'*')
> %%
> %% Testing the Network
> %load getmatrixdata Ptesti;
> load Ptest3
> PP=Ptest3;
> PP = transpose(PP);
> load matrixdatatraining, net;
> ytest=sim(net,PP);
>
> figure(3)
> err=y-t2;
> plot(err)
> title('eroor between actual targets and predictions')
> xlabel('Epoches')
>
> figure(4)
>
> x=ytest(200:900);
> plot(x')
>
> h=mean(x)
> pi=mean(ytest)

Sorry the program is until here..
Dalmar

From: Greg Heath on
On Jun 10, 4:42 pm, Dalmar <ybul...(a)googlemail.com> wrote:
> On Jun 5, 10:15 am, Greg Heath <he...(a)alumni.brown.edu> wrote:
> > On Jun 4, 5:43 am,Dalmar<ybul...(a)googlemail.com> wrote:
>
> > > I have written this NN to classify different cases. Simulation is
> > > excellent but I could not get the required target during the testing.
> > > Please let me know your suggestion. I am desperate to solve this
> > > problem due to submission of my postgraduate final year.
> > > Regards
> > > This is the Matlab code
-----SNIP
>
> I should thank you Mr Greg for your continuous support.
> This is the NN code
> clc, clear all, close all;
> % classification neural network
> % Taking Input data(Pi=909x3)
>
> load P0;load P1;load P2;load P3;load P4;
> [r0 c0] = size(P0);
> [r1 c1] = size(P1);
> [r2 c2] = size(P2);
> [r3 c3] = size(P3);
> [r4 c4] = size(P4);
> t0=zeros(r0,1); t1=ones(r1,1)*-2; t2=ones(r2,1)*2 ;t3=ones(r3,1)*-5;
> t4=ones(r4,1)*5;
>
> P=[P0; P1; P2; P3; P4];
> %training data(Targets) {909 x 1}
> T = [zeros(r0,1); ones(r1,1)*-2; ones(r2,1)*2 ;ones(r3,1)*-5;
> ones(r4,1)*5];
> t0=zeros(r0,1); t1=ones(r1,1)*-2; t2=ones(r2,1)*2 ;t3=ones(r3,1)*-5;
> t4=ones(r4,1)*5;
>
> P = transpose(P);
> T = transpose(T);

If this is a classification network for 5 classes, the output
should be 5 dimensional with targets that are columns of
the 5-dimensional unit matrix eye(5). Use LOGSIG as the
output activation function. Inputs will be assigned to the
class corresponding to the maximum output.

Output values are estimates of class posterior probabilities
conditional on the input P(i|x), i=1:5.

> [I N] = size(P)
> [O N] = size(T)

size(P) = [3 4545]
size(T) = [1 4545] with the current format
size(T) = [5 4545] with the standard classification format.

> %You should always design constant and linear models to understand
> %the results before designing a neural net with a hidden layer.
>
> % Constant Model
>
> y00 = repmat(mean(T,2),1,N) ;% output
> e00 = T-y00;% error
> MSE00 = mse(e00); % mean-squared -error
>
> %Linear Model
> W = T/[ones(1,N);P] % weights
> y0 = W*[ones(1,N);P]; % output
> e0 = T-y0; % error
> MSE0 = mse(e0); % mean-square error
> NMSE0 = MSE0/MSE00;
> R20 = 1-NMSE0; % 0.13618
>
> %R20 = 1 - MSE0/MSE00; % R-squared statistic
> %'The Linear Model only accounts for ~ 13.6% of the output'
> %'variance!'
> %' ==> try a Neural Net'
>
> %Creating and training of the Neural Network
>
> rand('state',0)
> Hmax = 10

How did you come up with Hmax = 10?

Neq = prod(size(T)) = 5*4545 = 22725 % Equations
Nw = (I+1)*H+(H+1)*O = O+(I+O+1)*H % Unknowns

for Neq > ~ 10*Nw

Hmax = floor( (Neq/10 -O)/(I+O+1) )
= floor( 2267.5/9 ) = 251

Meaning 1: Your training set is so large you can
afford to have H = 251 hidden nodes without
worrying about overtraining an overfit net.
However, since the input is only 3-D, that would
be a ridiculous choice.

Meaning 2: H = 1:1:10 is probably not a good search
strategy. You can easily afford to investigate a much
larger net.

I would probably start out by only using 1/3 of the
training set with Neq > ~20*Nw. Then

Hmax = floor(377.92/9) = 41 leading to a

H = 1: 5 : 41 INITIAL search strategy.
Once a narrower range is identified you
can step with dH = 1.

> ntrial = 0
> for H = 1:Hmax
> for weighttrial = 1:10
> ntrial = ntrial + 1
> net = newff(minmax(P),[H 1]); % help newff

for a classifier I recommend { 'tansig' 'logsig'}

> net.trainParam.goal = MSE00/100;
> net.trainParam.show = inf;
> [net,tr] = train(net,P,T); % help trainlm
> y = sim(net,P);
>
> e = T-y;
> MSE(ntrial) = mse(e);
> NMSE(ntrial) = MSE(ntrial)/MSE00;
> R2(ntrial) = 1-NMSE(ntrial);
> end
> end
> figure(1)
> plot(y); hold on; plot(T); hold off;

What is the significance of plotting the output
of the100th trial ? ?

Why not plot R2 vs ntrial and/or R2 vs H ???

> ylabel('simulation of the neural network')
> xlabel('Epochs')

No, not epochs. You are plotting output of the 100th
trial vs input vector index... which is probably not very
informative.

Find the H and weight values that yield the best
performance. Then plot those output performances
vs input vector index.

> [ R2max ntrialopt] = max(R2)
>
> rawsummary = [(1:ntrial)' R2'];

Plot this!

> sortedsummary = [ (1:ntrial)' -sortrows(-rawsummary,2)]

Plot this!

> %'53% of the nets are better than the Linear Model'
> %'15% of the nets have R2 >~ 85% '

Which 15 ntrial and H values?
Now you can use the ntrial values to determine
the corresponding state of the RNG for the
random initial weights and recreate those nets for
the purpose of testing.

However, if those H values are near Hmax = 10,
I suggest, at least, exploring much higher values.

> %% Creating and training of the Neural Network
> k=k+1,figure(k), hold on
> plot((1:ntrial),R2,'.')
> plot(100*R20*ones(1,ntrial),'r')
> xlabel('Trial')
> ylabel('Per Cent of Output Variance Modeled by the NNET')
> ylim([0 1])
> title('Neural Network Performance')
> return
> % Testing the Neural Network
> Yntrn = sim(net,Pn);
> MSEntrn = mse(Tn-Yntrn)
> R2ntrn = 1- MSEntrn/MSETn00
> Yntst = sim(net,an);
> MSEtst = mse(sn-Yntst)
> R2ntst = 1- MSEntst/MSEsn00

1. Use the standard classification format using
columns of eye(5) as training targets.
2. Investigate higher values of H
3. Find the best group of trained nets to recreate
and test. You have only tested the last of 100.
4. The ultimate performance criterion for a classifier
is the percent error rate. with a 5-dimensional output
find the class assignment corresponding to the maximum
then determine if it is correct or not.
Report
a. The percent error rate for each class
b. The percent error rate for the 5-class mixture.

Unfortunately, there is no good way to train a MLP
using error rate as a performance measure.

Hope this helps.

Greg

P.S. You probably shoold also calculate the per cent
error rates for the constant and linear classifiers.
 | 
Pages: 1
Prev: sum rows by attributes
Next: Data points from line