利用MATLAB中gatool快速实现训练神经网络的遗传算法程序
Deng Da-Peng
Email:
Genetic Algorithm,as an famous intelligent algorithm based on evolutionary thoughts, has been widely used to weights training and parameters optimization of neural networks. Essentially,GA is a global stochastic searching algorithm, which approximating global minima through Selection、Crossover and Mutation operators. It is difficult for many researchers to utilize advanced programming languages to implement GA. Actually, MATLAB provide us a very good graphic user interface of GA, named gatool, in GADS toolbox.
Below contents illustrate how to use this GUI tool to implement combination of GA and NN. In this case, I construct a feed forward network, which topological structure is5-3-1, transfer functions are tansig and purelin for hidden and output layer, respectively.
The key step is write a function to calculate fitness of chromosomes in GA population. Below code is implement this fitness calculating function in this case.
function netout = netcal(pm)
iN=5;
hN=3;
oN=1;
% add your training setshere
P=[ ];
T=[];
% Pre-processing data sets
[Pn,minP,maxP,Tn,minT,maxT] = premnmx(P,T);
net=newff(minmax(Pn),[hN,oN],{'tansig','purelin'});
[x,y]=size(pm);
for j=1:hN
x2iw(j,:)=pm(1,((j-1)*iN+1):j*iN);
end
for k=1:oN
x2lw(k,:)=pm(1,(iN*hN+1):(iN*hN+hN));
end
x2b=pm(1,((iN+1)*hN+1):y);
x2b1=x2b(1:hN).';
x2b2=x2b(hN+1:hN+oN).';
net.IW{1,1}=x2iw;
net.LW{2,1}=x2lw;
net.b{1}=x2b1;
net.b{2}=x2b2;
netout=mse(sim(net,Pn)-Tn); % this error function provides fitness for chromosome
OK, save this function with a name, i.e., netcal.m. Then, let's start gatool in MATLAB command line. The GUI of gatool is below.
Fill name of fitness calculating function in fitness function textbox, but note that add '@' before function name. Calculate numbers of weights of network, in this case is 22. Then, you need set parameters of GA in right. This step need you understand GA. If any question, you can see help.
Complete all these steps, click start button and launch training. When training process is end ,you will see a best chromosome in lower corner of left. This final result is best weight array of NN trained by GA. Change it to weight matrix and transfer to network according to fitness function code, then simulation with working sets and observe network performance.
You can generate a m files through "generate M-file…" in "file" menu. In this case, the M-file code is showed below. You may add some code in the end of this function for convenience.
OK, it is end. Thanks for your reading and hope for your reviews and comments.
function [X,FVAL,REASON,OUTPUT,POPULATION,SCORES] = untitled
%% This is an auto generated M file to do optimization with the Genetic Algorithm and
% Direct Search Toolbox. Use GAOPTIMSET for default GA options structure.
%%Fitness function
fitnessFunction = @netcal;
%%Number of Variables
nvars = 22;
%Start with default options
options = gaoptimset;
%%Modify some parameters
options = gaoptimset(options,'PopInitRange' ,[-0.5 ; 0.5 ]);
options = gaoptimset(options,'StallGenLimit' ,100);
options = gaoptimset(options,'CrossoverFcn' ,{ @crossoverheuristic 1.2 });
options = gaoptimset(options,'MutationFcn' ,{ @mutationgaussian 1 1 });
options = gaoptimset(options,'Display' ,'off');
options = gaoptimset(options,'PlotFcns' ,{ @gaplotbestindiv @gaplotscorediversity });
%%Run GA
[X,FVAL,REASON,OUTPUT,POPULATION,SCORES] = ga(fitnessFunction,nvars,options);