具有模糊字母识别能力的bp网络实现

字母识别程序,采用了bp网络,具有一定的模糊识别能力:

 

%recognize.m用于字母识别 [alphabet,targets] = prodat; P =alphabet; % alphabet = [ letterA, letterB, ..., letterZ ]; T = targets; % targets = eye(26); [R,Q] = size(P); % 神经元个数 S1 = 10; % 求T的大小,此时他的列数与P的列数为一致 [S2,Q] = size(T); %创建网络 net = newff(minmax(alphabet),[S1 S2],{'logsig' 'logsig'},'traingdx'); net.LW{2,1} = net.LW{2,1}*0.01; net.b{2} = net.b{2}*0.01; % 无噪声字母识别网络的训练 net.performFcn = 'sse'; %执行函数为误差平方和函数 net.trainParam.epochs=800; %最大训练步长 net.trainParam.lr=0.01; %学习率 net.trainParam.lr_inc =1.05; %增长的学习率 net.trainParam.lr_dec =0.7; net.trainParam.goal=0.1; %执行函数目标值 net.trainParam.mc =0.9; %附加动量因子 net.trainParam.min_grad=1e-10; %最小执行梯度 net.trainParam.show = 50; % P = alphabet; % T = targets; [net,tr] = train(net,P,T); noisyJ = alphabet(:,10)+randn(35,1) * 0.2; A2 = sim(net,noisyJ); A2 = compet(A2); % compet is a transfer function. Transfer functions calculate a layer's output from its net input. % compet(N) takes one input argument, % N - S x Q matrix of net input (column) vectors. % and returns output vectors with 1 where each net input vector has its maximum value, and 0 elsewhere answer = find(compet(A2) == 1); % indices = find(X) returns the linear indices corresponding to the nonzero entries of the array X. % If none are found, find returns an empty, 0-by-1 matrix. % In general, find(X) regards X as X(:), % which is the long column vector formed by concatenating the columns of X. % Some operations on a vector x = [11 0 33 0 55]'; % find(x) % % ans = % % 1 % 3 % 5 % % find(x == 0) % % ans = % % 2 % 4 % % find(0 < x & x < 10*pi) % % ans = % % 1 figure(2); plotchar(alphabet(:,answer)); figure(3); plotchar(noisyJ); % %%%%%%%%%%%%%%%========================================================= %%%%%%%%%%% 具有噪声的输入识别网络的训练,共循环10次,开始 netn = net; netn.trainParam.goal = 0.1; %目标误差. netn.trainParam.epochs = 300; %最大训练步长. for pass=1:10 fprintf('Pass = %.0f/n',pass); T = [targets targets targets targets]; P = [alphabet, alphabet, ... (alphabet + randn(R,Q)*0.1), ... (alphabet + randn(R,Q)*0.2)]; [netn,tr] = train(netn,P,T); end %%%%%%%%%%% 具有噪声的输入识别网络的训练,共循环10次,结束 noisyJ = alphabet(:,10)+randn(35,1) * 0.2; B2 = sim(netn,noisyJ); B2 = compet(B2); answer = find(compet(B2) == 1); figure(4); plotchar(alphabet(:,answer)); figure(5); plotchar(noisyJ); %%%%%%% 再次用无噪声字母训练识别网络,开始 netn.trainParam.goal = 0.1; % 均方误差目标. netn.trainParam.epochs = 500; % 最大训练步长 net.trainParam.show = 5; % 训练中显示的频率 P = alphabet; T = targets; [netn,tr] = train(netn,P,T); %%%%%%% 再次用无噪声字母训练识别网络,结束

 

 

prodat.m程序用来产生26个字母的矩阵形式,及其所对应的目标矩阵表示形式,具体如下:

 

% prodat.m recognize.m调用的字母产生程序 function[alphabet,targets] = prodat() % Returns: % ALPHABET - 35x26 matrix of 5x7 bit maps for each letter. % TARGETS - 26x26 target vectors. letterA = [0 0 1 0 0 ... 0 1 0 1 0 ... 0 1 0 1 0 ... 1 0 0 0 1 ... 1 1 1 1 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ]'; letterB = [1 1 1 1 0 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 1 1 1 0 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 1 1 1 0 ]'; letterC = [0 1 1 1 0 ... 1 0 0 0 1 ... 1 0 0 0 0 ... 1 0 0 0 0 ... 1 0 0 0 0 ... 1 0 0 0 1 ... 0 1 1 1 0 ]'; letterD = [1 1 1 1 0 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 1 1 1 0 ]'; letterE = [1 1 1 1 1 ... 1 0 0 0 0 ... 1 0 0 0 0 ... 1 1 1 1 0 ... 1 0 0 0 0 ... 1 0 0 0 0 ... 1 1 1 1 1 ]'; letterF = [1 1 1 1 1 ... 1 0 0 0 0 ... 1 0 0 0 0 ... 1 1 1 1 0 ... 1 0 0 0 0 ... 1 0 0 0 0 ... 1 0 0 0 0 ]'; letterG = [0 1 1 1 0 ... 1 0 0 0 1 ... 1 0 0 0 0 ... 1 0 0 0 0 ... 1 0 0 1 1 ... 1 0 0 0 1 ... 0 1 1 1 0 ]'; letterH = [1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 1 1 1 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ]'; letterI = [0 1 1 1 0 ... 0 0 1 0 0 ... 0 0 1 0 0 ... 0 0 1 0 0 ... 0 0 1 0 0 ... 0 0 1 0 0 ... 0 1 1 1 0 ]'; letterJ = [1 1 1 1 1 ... 0 0 1 0 0 ... 0 0 1 0 0 ... 0 0 1 0 0 ... 0 0 1 0 0 ... 1 0 1 0 0 ... 0 1 0 0 0 ]'; letterK = [1 0 0 0 1 ... 1 0 0 1 0 ... 1 0 1 0 0 ... 1 1 0 0 0 ... 1 0 1 0 0 ... 1 0 0 1 0 ... 1 0 0 0 1 ]'; letterL = [1 0 0 0 0 ... 1 0 0 0 0 ... 1 0 0 0 0 ... 1 0 0 0 0 ... 1 0 0 0 0 ... 1 0 0 0 0 ... 1 1 1 1 1 ]'; letterM = [1 0 0 0 1 ... 1 1 0 1 1 ... 1 0 1 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ]'; letterN = [1 0 0 0 1 ... 1 1 0 0 1 ... 1 1 0 0 1 ... 1 0 1 0 1 ... 1 0 0 1 1 ... 1 0 0 1 1 ... 1 0 0 0 1 ]'; letterO = [0 1 1 1 0 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 0 1 1 1 0 ]'; letterP = [1 1 1 1 0 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 1 1 1 0 ... 1 0 0 0 0 ... 1 0 0 0 0 ... 1 0 0 0 0 ]'; letterQ = [0 1 1 1 0 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 1 0 1 ... 1 0 0 1 0 ... 0 1 1 0 1 ]'; letterR = [1 1 1 1 0 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 1 1 1 0 ... 1 0 1 0 0 ... 1 0 0 1 0 ... 1 0 0 0 1 ]'; letterS = [0 1 1 1 0 ... 1 0 0 0 1 ... 0 1 0 0 0 ... 0 0 1 0 0 ... 0 0 0 1 0 ... 1 0 0 0 1 ... 0 1 1 1 0 ]'; letterT = [1 1 1 1 1 ... 0 0 1 0 0 ... 0 0 1 0 0 ... 0 0 1 0 0 ... 0 0 1 0 0 ... 0 0 1 0 0 ... 0 0 1 0 0 ]'; letterU = [1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 0 1 1 1 0 ]'; letterV = [1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 0 1 0 1 0 ... 0 0 1 0 0 ]'; letterW = [1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 0 0 1 ... 1 0 1 0 1 ... 1 1 0 1 1 ... 1 0 0 0 1 ]'; letterX = [1 0 0 0 1 ... 1 0 0 0 1 ... 0 1 0 1 0 ... 0 0 1 0 0 ... 0 1 0 1 0 ... 1 0 0 0 1 ... 1 0 0 0 1 ]'; letterY = [1 0 0 0 1 ... 1 0 0 0 1 ... 0 1 0 1 0 ... 0 0 1 0 0 ... 0 0 1 0 0 ... 0 0 1 0 0 ... 0 0 1 0 0 ]'; letterZ = [1 1 1 1 1 ... 0 0 0 0 1 ... 0 0 0 1 0 ... 0 0 1 0 0 ... 0 1 0 0 0 ... 1 0 0 0 0 ... 1 1 1 1 1 ]'; alphabet = [letterA,letterB,letterC,letterD,letterE,letterF,letterG,letterH,... letterI,letterJ,letterK,letterL,letterM,letterN,letterO,letterP,... letterQ,letterR,letterS,letterT,letterU,letterV,letterW,letterX,... letterY,letterZ]; targets = eye(26); % Y = eye(n) returns the n-by-n identity matrix. % Y = eye(m,n) or eye([m n]) returns an m-by-n matrix with 1's on the diagonal and 0's elsewhere. % Y = eye(size(A)) returns an identity matrix the same size as A. % eye(m, n, classname) or eye([m,n],classname) is an m-by-n matrix % with 1's of class classname on the diagonal and zeros of class classname elsewhere. % classname is a string specifying the data type of the output. % classname can have the following values: % 'double', 'single', 'int8', 'uint8', 'int16', 'uint16', 'int32', or 'uint32'.

你可能感兴趣的:(网络,vector,Class,input,Matrix,output)