Can anyboby help me to decode the following code and tell waht the code is actually doing?

3 vues (au cours des 30 derniers jours)
%routine 1
%For feed-forward neural networks and genetics
clear all;
load file.mat
transfer_function=4; % 1 for log-sigmoid, 2 for tangent hyperbolic, 3 for radbas and 4 for linear
if transfer_function==1 % 1 for log-sigmoid, 2 for tangent hyperbolic, 3 for radbas and 4 for linear
transfer_delta_function=1;
elseif transfer_function==2
transfer_delta_function=2;
elseif transfer_function==3
transfer_delta_function=3;
elseif transfer_function==4
transfer_delta_function=4;
end
transfer_function_O=4; % 1 for log-sigmoid, 2 for tangent hyperbolic, 3 for radbas and 4 for linear
constant=0;
pc=0.2; % Crossover rate
pm=0.005; % Mutation rate
nforecast=7;
y=file(1:end-nforecast,2)
x=file(1:end-nforecast,3)
%y = file(1:28,1);
disp(y)
%x = file(1:28,2);
disp(x)
if constant == 0
%x=x;
elseif constant==1
x=[ones(nk,1) x];
end
[nk ni]=size(x);
n_outputs = 1;
n_inputs = ni;
num_hidden = ni;
chrom =num_hidden*n_inputs + num_hidden*n_outputs;
chrom =num_hidden*(n_inputs+1) + (num_hidden + 1)*n_outputs;
lb= min(min(x)); % Lower bound of the parameters to be optimized
ub=max(max(x)); %Upper bound of the parameters to be optimized
popsize=30;
chromlength=chrom;
Range=repmat((ub-lb),[popsize chrom]);
Lower = repmat(lb, [popsize chrom]);
pop=rand(popsize, chromlength).*Range+ Lower;
f=x;
for iterations =1:20
% chromosoms' fitness evaluation
[px,py] = size(pop);
Chromosome = pop;
for i = 1:px
for j = 1:num_hidden*ni
w1(j) = Chromosome (i,j);
end
w1 = reshape(w1,num_hidden,ni);
k=1;
for j = n_inputs*num_hidden+1:(num_hidden*ni + n_outputs*num_hidden)
w2(k)= Chromosome (i,j);
%w2=w2'
k=k+1;
end
[X Y]=meshgrid(w2);
w2=Y(:,1);
k=1;
for j = (num_hidden*n_inputs + n_outputs*num_hidden + 1):(num_hidden*n_inputs + n_outputs*num_hidden + num_hidden)input_hidden_Bias(k)= Chromosome (i,j);
k=k+1;
end
k=1;
for j = (num_hidden*n_inputs + n_outputs*num_hidden + num_hidden + 1):(num_hidden*n_inputs + n_outputs*num_hidden + num_hidden +n_outputs)
hidden_output_Bias(k)= Chromosome (i,j);
k=k+1;
end
if transfer_function==1
h=logsig(f*w1');
elseif transfer_function==2
h=tansig(f*w1');
elseif transfer_function==3
h=radbas(f*w1');
elseif transfer_function==4
h=purelin(f*w1');
end
h1 = h;
if transfer_function_O==1;
y1=logsig(h1*w2);
elseif transfer_function_O==2;
y1=tansig(h1*w2);
elseif transfer_function_O==3;
y1=radbas(h1*w2);
elseif transfer_function_O==4;
y1=purelin(h1*w2);
end
err = (y-y1);
err = reshape(err,nk*n_outputs,1);
object_value(i)=1/2*mse(err);
end
fitvalue = object_value;
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
end
% crossover between chromosoms
pop = newpop;
length_chrom = size(pop,2);
c_point = ceil(rand(size(pop,1)/2,1)*(length_chrom-1));
c_point = c_point.*(rand(size(c_point))<pc);
for i = 1:length(c_point);
newpop([2*i-1 2*i],:) = [pop([2*i-1 2*i],1:c_point(i)) pop([2*i
2*i-1],c_point(i)+1:length_chrom)];
end
% mutation of chromosoms
pop = newpop;
mutated = find(rand(size(pop))<pm);
newpop = pop;
newpop(mutated) = 1-pop(mutated);
% 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
end
best_gen=bestindividual;
for j = 1:num_hidden*ni
w1(j) = best_gen (1,j);
end
k=1;
for j = ni*num_hidden+1:(num_hidden*ni + n_outputs*num_hidden)
w2(k)= best_gen (1,j);
k=k+1;
end
iterations=iterations+1;
arraygbest ( iterations )= bestfit;
indexiter ( iterations ) = iterations;
end
%end
test_sample=file(29:35,1);
%test_sample=file(end-nforecast:end-1,2);
%test_y=file(end-nforecast+1:end,1);
test_y=file(29:35,2);
tt=length(test_sample);
if constant==0
test_sample=test_sample;
elseif constant==1
test_sample=[ones(size(test_y)) test_sample];
end
if transfer_function==1
yfore=logsig(test_sample*w1);
yfore=logsig(yfore*w2);
elseif transfer_function==2
yfore=tansig(test_sample*w1);
yfore=tansig(yfore*w2);
elseif transfer_function==3
yfore=radbas(test_sample*w1);
yfore=radbas(yfore*w2);
elseif transfer_function==4
yfore=test_sample*w1;
yfore=yfore*w2;
end
figure, plot(y,'-r'); hold on; plot(y1,'-b');
xlabel('Water activity')
ylabel('EMC')
%title('In_sample forecasts')
h1 = legend('Actual','forecasts',1);
figure, plot(test_y,'-r'); hold on; plot(yfore,'-b');
xlabel('Water activity')
ylabel('EMC')
%title('Out_of_sample forecasts')
h = legend('Actual','forecasts',1);
figure, plot (indexiter , test_y);
xlabel('epochs')
ylabel('Error')
title('Number of Epochs')
  3 commentaires
Greg Heath
Greg Heath le 1 Mar 2015
Now that it is formatted. It is still too painful to follow. Obviously it is a program that trains using a genetic algorithm. It's been more than 10 years for me. So I suggest:
1. Search the NEWSGROUP and ANSWERS for other genetic training NN code that is easier to understand.
2. Then, if needed, come back here.
3. Start with
neural ga
and
neural genetic
Hope this helps.
Greg
Walter Roberson
Walter Roberson le 1 Mar 2015
I wouldn't even consider answering this question without the reference to the paper that the code is from.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Sequence and Numeric Feature Data Workflows dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by