# 具有模糊字母识别能力的bp网络性能分析

xiaoxiao2021-03-01  22

% charRecogonize.m clc; clear; %调用prodat产生alpabet字母表和targets，target为eye(26); [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; % 无噪声字母识别网络的训练 fprintf('================================================\n'); fprintf('There are first train,begin\n'); 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); fprintf('There are first train,end\n'); fprintf('================================================\n'); % noisyJ = alphabet(:,25)+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 % plotchar(alphabet(:,answer)); % figure(2); % plotchar(noisyJ); %%%%%%%%%%%%%%%========================================================= %%%%%%%%%%% 具有噪声的输入识别网络的训练，共循环10次，开始 fprintf('================================================\n'); fprintf('There are 10 times,For noise,begin\n'); 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 fprintf('There are 10 times,For noise,end\n'); fprintf('================================================\n'); %%%%%%%%%%% 具有噪声的输入识别网络的训练，共循环10次，结束 %%%%%%% 再次用无噪声字母训练识别网络，开始 fprintf('================================================\n'); fprintf('There are no noise,begin\n'); netn.trainParam.goal = 0.1; % 均方误差目标. netn.trainParam.epochs = 500; % 最大训练步长 net.trainParam.show = 5; % 训练中显示的频率 P = alphabet; T = targets; [netn,tr] = train(netn,P,T); %%%%%%% 再次用无噪声字母训练识别网络，结束 fprintf('There are no noise,end\n'); fprintf('================================================\n'); %%%% 测试所训练的网络性能 noise_range = 0:.02:0.2; max_test = 100; network1 = []; network2 = []; T = targets; % PERFORM THE TEST for noiselevel = noise_range fprintf('================================================\n'); fprintf('Testing networks with noise level of %.2f.\n',noiselevel); errors1 = 0; errors2 = 0; for i=1:max_test P = alphabet + randn(35,26)*noiselevel; % TEST NETWORK 1 A = sim(net,P); AA = compet(A); errors1 = errors1 + sum(sum(abs(AA-T)))/2; % TEST NETWORK 2 An = sim(netn,P); AAn = compet(An); errors2 = errors2 + sum(sum(abs(AAn-T)))/2; echo off end % 100组26个矢量的平均误差 network1 = [network1 errors1/26/100]; network2 = [network2 errors2/26/100]; end fprintf('================================================\n'); % echo on % % The echo command controls the echoing of M-files during execution. % Normally, the commands in M-files are not displayed on the screen % during execution. Command echoing is useful for debugging % or for demonstrations, allowing the commands to be viewed as they execute. % The echo command behaves in a slightly different manner for script files % and function files. For script files, the use of echo is simple; % echoing can be either on or off, in which case any script used is affected. % echo onTurns on the echoing of commands in all script filesecho offTurns off the echoing of commands in all script filesechoToggles the echo state % pause % 按任意键显示结果 % 显示结果 % ======== clf % clf deletes from the current figure all graphics objects % whose handles are not hidden (i.e., their HandleVisibility property is set to on). % clf('reset') deletes from the current figure all graphics objects regardless of % the setting of their HandleVisibility property and resets all figure properties except Position, % Units, PaperPosition, and PaperUnits to their default values. % figure_handle = clf(...) return the handle of the figure. % This is useful when the figure IntegerHandle property is off % since the noninteger handle becomes invalid when the reset option is used % (i.e., IntegerHandle is reset to on, which is the default). plot(noise_range,network1*100,'r*',noise_range,network2*100,'g*'); grid xlabel('Noise Level'); ylabel('Percentage of Recognition'); % echo off %三种情况下字母Y的表示情况 noisyY1 = alphabet(:,25)+randn(35,1) * 0.2; figure(2); plotchar(noisyY1); noiseY2=alphabet(:,25)+randn(35,1)*0.1; figure(3); plotchar(noiseY2); idealY=alphabet(:,25); figure(4) plotchar(idealY); %%%%%%%%%%%%%%%========================================================= % figure(5); % sp=sim(net,noisyY1); % plotchar(sp); % figure(6); % sp2=sim(net,noisyY1); % plotchar(sp2);

% 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'.