Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Research on Optimization of BP Neural Network by Genetic Algorithm
Research on Optimization of BP Neural Network by Genetic Algorithm
Procedure:

1, BP neural network modeling without genetic algorithm optimization

Clear;

clc

%%%%%%% Input parameter%%%%%

N = percentage of the total data of 2000.

m = 1500; % training data

%%%%%%% Training data%%%%%

For i= 1:N

Input (I,1) =-5+rand *10;

Input (I, 2) =-5+rand *10;

end

Output = input (:,1). ^2+input(:,2).^2;

Save data input and output

Load data. mat

% Random sorting from 1 to n

K = rand (1, n);

[m,n]= sort(k);

% found training data and prediction data.

input_train=input(n( 1:M),:)’;

output _ train = output(n( 1:M),:)’;

input _ test = input(N((M+ 1):N),:)';

output _ test = output(N((M+ 1):N),:)';

Data standardization percentage

[inputn,inputs]= mapminmax(input _ train);

[outputn,outputs]= mapminmax(output _ train);

% establish BP neural network

net=newff(inputn,outputn,5);

net . train param . epochs = 100;

net . train param . lr = 0. 1;

net . train param . goal = 0.0000004;

%BP neural network training

net=train(net,inputn,outputn);

Standardized percentage of test samples

inputn_test=mapminmax('apply ',input_test,inputs);

%BP neural network prediction

an=sim(net,inputn _ test);

The%% network acquisition data is denormalized.

BPoutput=mapminmax('reverse ',an,outputs);

Figure (1)

%plot(BPoutput,':og ');

scatter( 1:(N-M),BPoutput,' rx ');

Hold on;

%plot(output_test,'-* ');

scatter( 1:(N-M),output_test,' o ');

Legend ('predicted output',' expected output',' fontsize',12);

Title('BP network prediction output',' fontsize',12);

Xlabel ('sample ',' fontsize ', 12);

Xlabel ('Error in output before optimization',' fontsize',12);

Figure (2)

error = BP output-output _ test;

Plot( 1:(N-M), error);

Xlabel ('sample ',' fontsize ', 12);

Ylabel ('Error in output before optimization',' fontsize',12);

% saving net input and net output

2. BP neural network modeling optimized by genetic algorithm.

(1) main program

% clear environment variables

Chromaticity control center

clear

% read data

Load data. mat

Percentage of node number

input num = 2;

hiddennum = 5;

output num = 1;

% training data and forecast data

input _ train = input( 1: 1500,)';

input _ test = input( 150 1:2000,)';

output _ train = output( 1: 1500)';

output _ test = output( 150 1:2000)';

Standardization of input and output data of% connection samples

[inputn,inputps]= mapminmax(input _ train);

[outputn,output PS]= mapminmax(output _ train);

% to build a network

net=newff(inputn,outputn,hiddennum);

%% Genetic Algorithm Parameter Initialization

maxgen = 10; % evolutionary algebra, that is, the number of iterations

Sizepop = 30% population size

pcross =[0.3]; % crossover probability selection, between 0 and 1.

p mutation =[0. 1]; % mutation probability selection, between 0 and 1.

Percentage of the total number of nodes

numsum = input num * hiddennum+hiddennum+hiddennum * output num+output num;

lenchrom=ones( 1,numsum);

bound=[-3*ones(numsum, 1) 3*ones(numsum, 1)]; % data range

%-Populate initialization.

-

individuals=struct('fitness ',zeros( 1,sizepop),' chrom ',[]); % defines population information as a structure.

% avg fitness =[]; The average fitness of each generation is%

best fitness =[]; The optimal fitness of each generation population is 90%.

best chrom =[]; Percentage of chromosomes with the best fitness

Initial population percentage

For i= 1:sizepop

% randomly generate a group.

individuals.chrom(i,)=Code(lenchrom,bound); % coding

x=individuals.chrom(i,);

% calculation fitness

individuals.fitness(i)=fun(x,inputnum,hiddennum,outputnum,net,inputn,outputn); Fitness of% chromosome

end

% looking for the best chromosome

[bestfitness bestindex]=min (personal fitness);

best chrom = individuals . chrom(best index,); % of the best chromosomes

% avg fitness = sum(individuals . fitness)/size pop; Average fitness of% chromosome

% record the best fitness and average fitness in each generation evolution.

% trace =[avg fitness best fitness];

%% Iteratively solve the optimal initial threshold and weight.

% evolution begins

For i= 1:maxgen

I

% selection

Individual =Select (individual, size pop);

% avg fitness = sum(individuals . fitness)/size pop;

Cross percentage

individuals.chrom=Cross(pcross,lenchrom,individuals.chrom,sizepop,bound);

Percentage change

individuals.chrom = Mutation(p Mutation,lenchrom,individuals . chrom,sizepop,I,maxgen,bound);

% calculation fitness

For j= 1:sizepop

x=individuals.chrom(j,); % decoding

individuals.fitness(j)=fun(x,inputnum,hiddennum,outputnum,net,inputn,outputn);

end

% found the minimum and maximum fitness chromosomes and their positions in the population.

[newbestfitness, newbestindex]=min (individual. fitness);

[worestfitness,worest index]= max(individuals . fitness);

% instead of the best chromosome in the last evolution

If the best fitness & gt new best fitness

bestfitness = newbestfitness

best chrom = individuals . chrom(new bestindex,);

end

individuals.chrom(worestindex,)= bestchrom

personals . fitness(worestindex)= best fitness;

% avg fitness = sum(individuals . fitness)/size pop;

% trace =[trace; avg fitness best fitness]; % record the best fitness and average fitness in each generation evolution.

end

Analysis of%% Genetic Algorithm Results

% Figure (3)

%[r c]= size (tracked);

%plot([ 1:r]',trace(:,2),' b-');

%title([' fitness curve'' termination algebra =' num2str (maxgen)]);

%xlabel ('evolutionary algebra'); Ylabel ('fitness');

%legend ('general fitness',' best fitness');

Disp ('fitness variable');

x = bestchrom

%% provides the best initial threshold weight for network prediction.

The numerical prediction adopts %% BP network optimized by genetic algorithm.

w 1 = x( 1:input num * hiddennum);

b 1 = x(input num * hiddennum+ 1:input num * hiddennum+hiddennum);

w2 = x(input num * hiddennum+hiddennum+ 1:input num * hiddennum+hiddennum+hiddennum * output num);

B2=x

(input num * hiddennum+hiddennum+hiddennum * output num+ 1:input num * hiddennum+hiddennum+hiddennum * output num+output num);

net.iw{ 1, 1 } = shape(w 1,hiddennum,input num);

net.lw{2, 1 } = shape(w2,outputnum,hiddennum);

net . b { 1 } = shape(b 1,hiddennum, 1);

net . b { 2 } = B2;

%% BP network training

% network evolution parameters

net . train param . epochs = 100;

net . train param . lr = 0. 1;

% net . train param . goal = 0.0000 1;

% online training

[net,per2]=train(net,inputn,outputn);

%% BP network prediction

Data standardization percentage

inputn_test=mapminmax('apply ',input_test,input PS);

an=sim(net,inputn _ test);

test_simu=mapminmax('reverse ',an,output PS);

error = test _ simu-output _ test;

% Figure (4);

Hold on; Plot( 1:500, error,' r');

Legend ('Error before optimization',' Error after optimization',' fontsize', 12)

(2) coding subroutine code.m

Function ret=Code(lenchrom, bound)

% This function encodes variables into chromosomes and is used to randomly initialize the population.

% lenchrom Input: Chromosome Length

% bound input: the value range of the variable.

% ret output: the coded value of chromosome.

flag = 0;

While flag ==0

Pick=rand( 1, length (lenchrom)););

ret=bound(:, 1)'+(bound(:,2)-bound(:, 1))'。 * pick% linear interpolation, and the coding result is stored as a real vector in ret.

flag=test(lenchrom,bound,ret); % test the feasibility of chromosome

end

(3) fitness fun.m

Function error = fun(x, inputnum, hiddennum, outputnum, net, inputn, outputn)

% This function is used to calculate the fitness value.

%x Enter an individual

%inputnum Input number of nodes in the input layer

Number of hidden layer nodes in %outputnum input

% net input network

% inputn input training input data

% output input training output data

% error output personal fitness value

Extraction percentage

w 1 = x( 1:input num * hiddennum);

b 1 = x(input num * hiddennum+ 1:input num * hiddennum+hiddennum);

w2 = x(input num * hiddennum+hiddennum+ 1:input num * hiddennum+hiddennum+hiddennum * output num);

B2 = x(input num * hiddennum+hiddennum+hiddennum * output num+ 1:input num * hiddennum+hiddennum+hiddennum * output num+output num);

net=newff(inputn,outputn,hiddennum);

% network evolution parameters

net . train param . epochs = 20;

net . train param . lr = 0. 1;

net . train param . goal = 0.0000 1;

net . train param . show = 100;

net . train param . show window = 0;

Weight allocation of% network

net.iw{ 1, 1 } = shape(w 1,hiddennum,input num);

net.lw{2, 1 } = shape(w2,outputnum,hiddennum);

net . b { 1 } = shape(b 1,hiddennum, 1);

net . b { 2 } = B2;

% online training

net=train(net,inputn,outputn);

an=sim(net,inputn);

Error = sum (ABS (an-output));

(4) select the operation Select.m

Function ret=select (individual, size popup)

% This function is used to select an operation.

% of individuals input population information

Population size entered by% sizepop

Press% ret to output the selected new population.

% Find the reciprocal of the fitness value

[abesch] = min (personal fitness);

% b = individuals . chrom(best ch);

% c = individuals . fitness(best ch);

Fitting degree 1= 10. /personals . fitness; % individualities. fitness is an %individuals.fitness value.

% individual selection probability

sum fitness = sum(fitness 1);

sumf=fitness 1。 /sumfitness;

% new individuals through roulette wheel selection.

index =[];

For i= 1:sizepop %sizepop is the population.

Pick = rand;

When pick==0

Pick = rand;

end

For i= 1:sizepop

pick = pick-sumf(I);

If<0 is selected

index =[index I];

Break;

end

end

end

% index =[index best ch];

Percentage of new population

individuals . chrom = individuals . chrom(index,); %individuals.chrom is an individual in the population.

personals . fitness = personals . fitness(index);

% individuals . chrom =[individuals . chrom; b];

% personals . fitness =[personals . fitness; c];

Ret = individual;

(5) crossover operation

Function ret=Cross(pcross, lenchrom, chrom, sizepop, bound)

% This function completes the crossover operation.

% pcorss input: crossover probability

% lenchrom Input: Length of chromosome.

% chrom input: genome

% sizepop input: population size

% ret output: cross chromosome

For i= 1:sizepop%, there may be crossover operation in each round of for loop, the chromosomes are randomly selected, and the crossover position is also randomly selected,% but whether crossover operation is carried out in this round of for loop is determined by crossover probability (continuous control).

% randomly selected two chromosomes for hybridization.

pick=rand( 1,2);

And production (selection) ==0.

pick=rand( 1,2);

end

Index = upper limit (select. * size pop);

% crossover probability determines whether to cross.

Pick = rand;

When pick==0

Pick = rand;

end

If pick & gtpcross

Continue;

end

flag = 0;

While flag ==0

% randomly select crossover bits

Pick = rand;

When pick==0

Pick = rand;

end

pos=ceil(pick。 * sum(len chrom)); % randomly choose the crossover position, that is, choose which variable to cross. Note: The crossing position of the two chromosomes is the same.

Pick = rand; Cross start percentage

v 1=chrom(index( 1),pos);

V2=chrom (index (2), position);

chrom(index( 1),pos)= pick * v2+( 1-pick)* v 1;

chrom(index(2),pos)= pick * v 1+( 1-pick)* v2; Cross percentage

flag 1=test(lenchrom,bound,chrom(index( 1),:)); % test the feasibility of chromosome 1

flag2=test(lenchrom,bound,chrom(index(2),:)); % test the feasibility of chromosome 2

If flag 1*flag2==0.

flag = 0;

else flag = 1;

End% If both chromosomes are not feasible, cross again.

end

end

ret = chrom

(6) Mutation

function ret=Mutation(pmutation,lenchrom,chrom,sizepop,num,maxgen,bound)

% This function completes the mutation operation.

% pcorss input: mutation probability

% lenchrom Input: Chromosome Length

% chrom input: genome

% sizepop input: population size

% opts input: select mutation method

% pop input: the evolutionary algebra and maximum evolutionary algebra information of the current population.

% binding input: everyone's last and next session.

% maxgen input: maximum number of iterations.

% num input: current number of iterations

% ret output: mutated chromosome

For i= 1:sizepop%, there may be a mutation operation in each round of for cycle, the chromosomes are randomly selected, and the mutation position is also randomly selected.

However, whether this round of for loop mutates or not is determined by the mutation probability (continuous control).

% randomly selected a chromosome for mutation.

Pick = rand;

When pick==0

Pick = rand;

end

index = ceil(pick * size pop);

The mutation probability of% determines whether the cycle mutates or not.

Pick = rand;

If pick & gtp mutates,

Continue;

end

flag = 0;

While flag ==0

% mutation position

Pick = rand;

When pick==0

Pick = rand;

end

pos = ceil(pick * sum(len chrom)); % randomly choose the position of chromosome variation, that is, choose the first pos variable for variation.

Pick = rand; % mutation begins

fg=(rand*( 1-num/maxgen))^2;

If pick & gt0.5

chrom(i,pos)=chrom(i,pos)+(bound(pos,2)-chrom(i,pos))* fg;

other

chrom(i,pos)=chrom(i,pos)-(chrom(i,pos)-bound(pos, 1))* fg;

End% Mutation End

flag=test(lenchrom,bound,chrom(i,); % test the feasibility of chromosome

end

end

ret = chrom