MAKHFI.COM
Fascinating World of Neural Nets  
Neural Network Forums
Home PageHome Page : Neural Networks using MATLAB : character recognition using neural network trained by genetic algorithm
  You are currently not logged in. You can view the forums, but cannot post messages. | HOME | Log In | Register | Search | Help
Post a Reply on This Topic Post a Reply on This Topic

Author Topic: character recognition using neural network trained by genetic algorithm
catherine Posted: 12-Oct-09 11:32
  Edit Edit
 
Email the Author Mail   View Author's Profile Profile  
Dear all
The following is the used matlab code to solve the XOR problem using
a feed forward network trained by a genetic algorithm but it often gives wrong result.

Can any expert tell me where is the error

Thanks in advance and best wishes

clc;
clear all;
close all;
cd c:\matlab7\work\GaBp\

P=[0 0;0 1; 1 0; 1 1]';
T = [ 0 ;1 ;1 ; 0 ]';
nInputs = 2;
nHidden = 6;
nOutputs = 1;

patterns = P;
nPats = size(patterns,1);
nTrainingPats = 4;
nTestPats = 1; %%% reserve last 10 patterns for testing generalization
input = patterns;

Dim =nHidden*(nInputs+1) + (nHidden + 1)*nOutputs;
net = newff(minmax(P),[nHidden nOutputs],{'logsig' 'purelin' });

lb= min(min(P)); % Lower bound of the parameters to be optimized
ub=max(max(P));; %Upper bound of the parameters to be optimized

popsize=20;
chromlength=Dim;
pc=0.2;
pm=0.01;

Range = repmat((ub-lb),[popsize Dim]);
Lower = repmat(lb, [popsize Dim]);
pop= rand(popsize,Dim) .* Range + Lower;

for iterations =1:100

% chromosoms' fitness evaluation
[px,py] = size(pop);
Chrom = pop;
for i = 1:px
for j = 1: nHidden*nInputs
hiddenWeights(j) = Chrom (i,j);
end

k=1;
for j = (nHidden*nInputs + 1):(nHidden*nInputs + nOutputs*nHidden)
outputWeights(k)= Chrom (i,j);
k=k+1;
end

k=1;
for j = (nHidden*nInputs + nOutputs*nHidden + 1):(nHidden*nInputs + nOutputs*nHidden + nHidden)
hiddenBias(k)= Chrom (i,j);
k=k+1;
end

k=1;
for j = (nHidden*nInputs + nOutputs*nHidden + nHidden + 1):(nHidden*nInputs + nOutputs*nHidden + nHidden +nOutputs)
outputBias(k)= Chrom (i,j);
k=k+1;
end

hiddenBias1 = hiddenBias';
outputBias1 = outputBias';
hiddenWeights = reshape (hiddenWeights, nHidden, nInputs);
outputWeights = reshape (outputWeights, nOutputs,nHidden);

% *********************************************************************
****
% *********************************************************************
****
net.iw{1,1}=hiddenWeights;
net.lw{2,1}=outputWeights;
net.b{1,1}=hiddenBias1;
net.b{2,1}=outputBias1;

error=sim(net,P)-T;
objvalue(i)=1/2*mse(error);
end


% selection of the chromosoms
fitvalue = objvalue;
totalfit=sum(fitvalue);
fitvalue=fitvalue/totalfit;
fitvalue=cumsum(fitvalue);
[px,py]=size(pop);
ms=sort(rand(px,1));
fitin=1;
newin=1;
while newin<=px
if(ms(newin))<fitvalue(fitin)
newpop(newin,:)=pop(fitin,:);
newin=newin+1;
else
fitin=fitin+1;
end

% crossover between chromosoms
pop = newpop;

[px,py]=size(pop);
newpop=ones(size(pop));
for i=1:2:px-1
if(rand<pc)
cpoint=round(rand*py);
newpop(i,:)=[pop(i,1:cpoint) pop(i+1,cpoint+1:py)];
newpop(i+1,:)=[pop(i+1,1:cpoint) pop(i,cpoint+1:py)];
else
newpop(i,:)=pop(i,:);
newpop(i+1,:)=pop(i+1,:);
end

% mutation of chromosoms
pop = newpop;

newpop=ones(size(pop));
for i=1:px
if(rand<pm)
mpoint=round(rand*py);
if mpoint<=0
mpoint=1;
end
newpop(i,:)=pop(i,:);
if any(newpop(i,mpoint))==0
newpop(i,mpoint)=1;
else
newpop(i,mpoint)=0;
end
else
newpop(i,:)=pop(i,:);
end

% finding the best individual
pop = newpop;

bestindividual=pop(1,:);
bestfit=fitvalue(1);
for i=2:px
if fitvalue(i)<bestfit
bestindividual=pop(i,:);
bestfit=fitvalue(i);
end

iterations
arraygbest ( iterations )= bestfit;
indexiter ( iterations ) = iterations;
end

meilleure = bestindividual;

for j = 1: nHidden*nInputs
hiddenWeights(j) = meilleure (1,j);
end

k=1;
for j = (nHidden*nInputs + 1):(nHidden*nInputs + nOutputs*nHidden)
outputWeights(k)= meilleure (1,j);
k=k+1;
end

k=1;
for j = (nHidden*nInputs + nOutputs*nHidden + 1):(nHidden*nInputs + nOutputs*nHidden + nHidden)
hiddenBias(k)= meilleure (1,j);
k=k+1;
end

k=1;
for j = (nHidden*nInputs + nOutputs*nHidden + nHidden + 1):(nHidden*nInputs + nOutputs*nHidden + nHidden +nOutputs)
outputBias(k)= meilleure (1,j);
k=k+1;
end

hiddenBias1 = hiddenBias';
outputBias1 = outputBias';
hiddenWeights = reshape (hiddenWeights, nHidden, nInputs);
outputWeights = reshape (outputWeights, nOutputs,nHidden);

% *********************************************************************
****
% *********************************************************************
****
net.iw{1,1}=hiddenWeights;
net.lw{2,1}=outputWeights;
net.b{1,1}=hiddenBias1;
net.b{2,1}=outputBias1;

figure, plot (indexiter , arraygbest );
Ptest = [ 0 1]';

a = sim(net,Ptest);
round(a)
 

Post a Reply on This Topic Post a Reply on This Topic
 

Copyright © 2001-2003 Pejman Makhfi. All rights Reserved.