📄 ganeural.m
字号:
%
% ganeural.m
%
% ga trained neural network
%
%
% Get the parameters from the previous screen
%
popsize=get(sl1,'value');
mutprob=get(sl2,'value');
croprob=get(sl3,'value');
maxgen=get(sl4,'value');
hidden1_neurons=str2num(get(text23,'string'));
splt=str2num(get(text25,'string'));
d=[str2num(get(text11,'string')) str2num(get(text13,'string')) str2num(get(text15,'string'))];
dfc=[str2num(get(text17,'string')) str2num(get(text19,'string')) str2num(get(text21,'string'))];
%
% Initialise the data sets
%
if sum(output_var) > 1
[D L]=size(output_var);
for i = 1 : L
if output_var(i) == 1
y=data(:,i);
break;
end
end
else
y=data(:,output_var);
end
x=pcincl(data,include_var,output_var);
%
% Split this into training and testing
%
[x y xtest ytest]=psplit(x,y,splt);
%
% Scale all the data
%
maxx=max([x ; xtest]);
minx=min([x ; xtest]);
maxy=max([y ; ytest]);
miny=min([y ; ytest]);
[D L]=size(x);
for i=1:L
xtest(:,i)=(xtest(:,i)-minx(i))./(maxx(i)-minx(i));
x(:,i)=(x(:,i)-minx(i))./(maxx(i)-minx(i));
end
ytest(:,1)=(ytest(:,1)-miny(1))./(maxy(1)-miny(1));
y(:,1)=(y(:,1)-miny(1))./(maxy(1)-miny(1));
%
% Clear the screen
%
da_front;
drawnow;
set(w1,'NumberTitle','off','Name','Training dynamic neural network');
%
% Plot a couple of axes
%
ax1=axes(...
'Units','pixels',...
'Position',[60 270 500 130],...
'Box','on',...
'Color',[0 0 0],...
'Visible','on');
ax2=axes(...
'Units','pixels',...
'Position',[60 125 230 100],...
'Box','on',...
'Color',[0 0 0],...
'Visible','on');
but1=uicontrol(w1,...
'style','push',...
'position',[350 100 75 30],...
'string','Save...',...
'callback','svneural('''',include_var,maxx,minx,maxy,miny,input_neurons,hidden1_neurons,output_neurons,wt1,w2,b1,b2,f1,f2,f3)');
%
% Initialise parameter description
% variables
%
%d=[-2 0.01 2];
%
% Define network size.
% Defined here for: 1 linear input layer
% 1 sigmoidal hidden layer
% 1 linear output layer
%
%
% Weights for input layer to hidden layer #1
%
[D L]=size(x);
input_neurons=L;
%hidden1_neurons=2;
W1=zeros(input_neurons,hidden1_neurons);
B1=zeros(hidden1_neurons,1);
%
% Weights for hidden layer #1 to output layer
% Bias terms are included
%
output_neurons=1;
W2=zeros(hidden1_neurons,output_neurons);
B2=zeros(output_neurons);
%
% Calculate the total number of parameters
%
total_params=(input_neurons*hidden1_neurons)+(hidden1_neurons)+(hidden1_neurons*output_neurons)+(output_neurons);
%
% Now build up the GA description matrix for
% this size network
%
net_description=[];
for i=1:total_params
net_description=[net_description ; d];
end
%
% Append the description for the filter constants
%
%dfc=[0 0.1 1];
nt=[];
for i=1:(input_neurons+hidden1_neurons+output_neurons)
net_description=[net_description ; dfc];
nt=[nt ; dfc];
end
%
% Caclulate the number of bits required
%
[dummy net_description bits]=decode(0,net_description,0);
bts=bits;
bits=sum(bits);
[d n b]=decode(0,nt,0);
%
% Generate the initial random population with 20 members
%
chrom=newpop(20,bits);
%
% Set all the filter constants to zero
%
c2=zeros(20,b);
chrom=[chrom(:,1:bits-b) c2];
%
% Initialise the fast binary decoding
%
dvec=initubin(100);
%
% Main Breeding loop
%
results=[];
best=[];
summin=10000;
bestpin=1000;
old_net_1=[];
old_net_2=[];
disp('Running...');
for i=1:maxgen
%
% Go through and perfom mutation
% and crossover
%
[D L]=size(chrom);
for j = 1 : 2 :D
chrom(j,:)=mutate(chrom(j,:),mutprob);
chrom(j+1,:)=mutate(chrom(j+1,:),mutprob);
[chrom(j,:) chrom(j+1,:)]=crover(chrom(j,:),chrom(j+1,:),croprob);
end
%
% Evaluate and perform selection
%
[chrom coefs pin best bestpin]=select6(chrom,net_description,x,y,input_neurons,hidden1_neurons,output_neurons,best,bestpin);
%
% Plot the minimum error
%
%
% Plot the actual and predicted
%
[m ind]=min(pin);
mb=decodall(chrom(ind,:),net_description);
[wt1 b1 w2 b2 f1 f2 f3]=exnet(mb,input_neurons,hidden1_neurons,output_neurons);
yest=fil(x,f1);
yest=prop1(yest,wt1,b1,2,f2);
yest=prop1(yest,w2,b2,1,f3);
%
% Plot the results of the summer
%
yest2=fil(xtest,f1);
yest2=prop1(yest2,wt1,b1,2,f2);
yest2=prop1(yest2,w2,b2,1,f3);
%
% Plot them both
%
[D L]=size(yest);
x1=1:D;
[D L]=size(yest2);
x2=(max(x1)+1):(D+max(x1));
axes(ax1);
cla;
hold on;
plot(x1,y,'y');
plot(x1,yest,'b');
plot(x2,ytest,'g');
plot(x2,yest2,'r');
hold off;
drawnow;
[D1 L1]=size(y);
[D2 L2]=size(ytest);
results=[results ; [min(pin)/D1 sse(yest2,ytest)/D2]];
axes(ax2);
cla;
plot(results);
drawnow;
%
% Save the workspace if things improve
%
if (sse(yest2,ytest)/D2)<summin
summin=sse(yest2,ytest)/D2;
save best;
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -