global net N_num dimension all_p all_t telno right_num wrong_num;
% N Application M-file for n.fig
% FIG = N launch n GUI.
% N('callback_name', ...) invoke the named callback.
% Last Modified by GUIDE v2.0 16-Jan-2003 16:31:59
%开始初始化网络
dimension=3032; %在网络中向量的维数
N_num=134; %神经元个数
mc=6; %网络输出向量的维数
charset='ABCDEFG';
rng=[];
n=0;
right_num=0;
wrong_num=0;
for n=1:dimension
rng=[rng ; [0 1]]; %每个向量的输入范围
end
net.inputs{1}.range=rng;
net=newff(rng,[N_num mc],{'logsig' 'logsig' });%建立一个网络
%net=newff(rng,[N_num mc],{'logsig' 'logsig' });%建立一个网络
net.inputs{1}.size=dimension;
init(net);
net.trainFcn='traingda';
net.trainParam.goal=0.0000001;
net.trainParam.epochs=600;
net.trainParam.Gradient=1e-7;
net.trainParam.show=5;
net.trainParam.lr=0.05;
net.trainParam.lr_inc=1.33;
net.trainParam.lr_dec=0.5;
%生成训练用的数据(2类)
load sp.dat sp;
load qt.dat qt;
load sy.dat sy;
load sh.dat sh;
load ss.dat ss;
load zp.dat zp;
[sp_row,colum]=size(sp);
[qt_row,colum]=size(qt);
[sy_row,colum]=size(sy);
[sh_row,colum]=size(sh);
[ss_row,colum]=size(ss);
[zp_row,colum]=size(zp);
same_num=53;
if sp_row>same_num
sp_row=same_num;
end
if qt_row>same_num
qt_row=same_num;
end
if sy_row>same_num
sy_row=same_num;
end
if sh_row>same_num
sh_row=same_num;
end
if ss_row>same_num
ss_row=same_num;
end
if zp_row>same_num
sp_row=same_num;
end
getrow=[sp_row qt_row sy_row sh_row ss_row zp_row];
minrow=min(getrow);
qt(:,colum+1)=1;
qt(:,colum+2)=0;
qt(:,colum+3)=0;
qt(:,colum+4)=0;
qt(:,colum+5)=0;
qt(:,colum+6)=0;
sh(:,colum+1)=0;
sh(:,colum+2)=1;
sh(:,colum+3)=0;
sh(:,colum+4)=0;
sh(:,colum+5)=0;
sh(:,colum+6)=0;
sp(:,colum+1)=0;
sp(:,colum+2)=0;
sp(:,colum+3)=1;
sp(:,colum+4)=0;
sp(:,colum+5)=0;
sp(:,colum+6)=0;
ss(:,colum+1)=0;
ss(:,colum+2)=0;
ss(:,colum+3)=0;
ss(:,colum+4)=1;
ss(:,colum+5)=0;
ss(:,colum+6)=0;
sy(:,colum+1)=0;
sy(:,colum+2)=0;
sy(:,colum+3)=0;
sy(:,colum+4)=0;
sy(:,colum+5)=1;
sy(:,colum+6)=0;
zp(:,colum+1)=0;
zp(:,colum+2)=0;
zp(:,colum+3)=0;
zp(:,colum+4)=0;
zp(:,colum+5)=0;
zp(:,colum+6)=1;
sp=sp';
qt=qt';
sy=sy';
sh=sh';
ss=ss';
zp=zp';
all_p=[];
dj=0;
for a1=1:1:minrow
dj=dj+1;
all_p=[all_p qt(:,dj)];
all_p=[all_p sh(:,dj)];
all_p=[all_p sp(:,dj)];
all_p=[all_p ss(:,dj)];
all_p=[all_p sy(:,dj)];
all_p=[all_p zp(:,dj)];
end
dj=minrow;
for a1=1:1:qt_row-minrow
dj=dj+1;
all_p=[all_p qt(:,dj)];
end
dj=minrow;
for a1=1:1:sh_row-minrow
dj=dj+1;
all_p=[all_p sh(:,dj)];
end
dj=minrow;
for a1=1:1:sp_row-minrow
dj=dj+1;
all_p=[all_p sp(:,dj)];
end
dj=minrow;
for a1=1:1:ss_row-minrow
dj=dj+1;
all_p=[all_p ss(:,dj)];
end
dj=minrow;
for a1=1:1:sy_row-minrow
dj=dj+1;
all_p=[all_p sy(:,dj)];
end
dj=minrow;
for a1=1:1:zp_row-minrow
dj=dj+1;
all_p=[all_p zp(:,dj)];
end
[p_row,p_col]=size(all_p);
%telno=all_p(1:2,:);
%all_p(1:2,:)=[];
%all_t=all_p(colum-6:colum-5:colum-4:colum-3:colum-2:colum-1:colum,:);
all_t=[];
all_t=[all_t all_p(colum+1,:)'];
all_t=[all_t all_p(colum+2,:)'];
all_t=[all_t all_p(colum+3,:)'];
all_t=[all_t all_p(colum+4,:)'];
all_t=[all_t all_p(colum+5,:)'];
all_t=[all_t all_p(colum+6,:)'];
all_t=all_t';
%all_t=all_p(colum+1:colum+2:colum+3:colum+4:colum+5:colum+6:colum+7,:)
%all_p(colum+1 colum+2 colum+3 colum+4 colum+5 colum+6 colum+7,:)=[];
all_p(colum+6,:)=[];
all_p(colum+5,:)=[];
all_p(colum+4,:)=[];
all_p(colum+3,:)=[];
all_p(colum+2,:)=[];
all_p(colum+1,:)=[];
[net tr]=train(net,all_p,all_t); %训练网络
%save net.mat net;
fid=fopen('netpar.dat','w');
fwrite(fid,net.inputs{1}.size,'int');
fwrite(fid,net.layers{1}.size,'int');
fwrite(fid,net.layers{2}.size,'int');
fwrite(fid,net.IW{1,1},'double');
fwrite(fid,net.LW{2,1},'double');
fwrite(fid,net.b{1},'double');
fwrite(fid,net.b{2},'double');
fclose(fid);
评论0