性能分析代码如下:
% 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,预处理数据用于产生字母序列表和eye矩阵,具体程序实现如下:
% 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'.
程序输出如下:
================================================ There are first train,begin TRAINGDX, Epoch 0/800, SSE 170.085/0.1, Gradient 55.6444/1e-010 TRAINGDX, Epoch 50/800, SSE 24.781/0.1, Gradient 0.497832/1e-010 TRAINGDX, Epoch 100/800, SSE 17.3254/0.1, Gradient 0.796657/1e-010 TRAINGDX, Epoch 150/800, SSE 0.153578/0.1, Gradient 0.0354647/1e-010 TRAINGDX, Epoch 155/800, SSE 0.0997842/0.1, Gradient 0.0224737/1e-010 TRAINGDX, Performance goal met. There are first train,end ================================================ ================================================ There are 10 times,For noise,begin Pass = 1 TRAINGDX, Epoch 0/300, SSE 4.0244/0.1, Gradient 2.94032/1e-010 TRAINGDX, Epoch 50/300, SSE 1.26719/0.1, Gradient 0.491154/1e-010 TRAINGDX, Epoch 100/300, SSE 0.549706/0.1, Gradient 0.10225/1e-010 TRAINGDX, Epoch 150/300, SSE 0.12426/0.1, Gradient 0.0197092/1e-010 TRAINGDX, Epoch 155/300, SSE 0.0999154/0.1, Gradient 0.0158492/1e-010 TRAINGDX, Performance goal met. Pass = 2 TRAINGDX, Epoch 0/300, SSE 3.99564/0.1, Gradient 4.43371/1e-010 TRAINGDX, Epoch 50/300, SSE 0.531749/0.1, Gradient 0.3353/1e-010 TRAINGDX, Epoch 100/300, SSE 0.217027/0.1, Gradient 0.0652191/1e-010 TRAINGDX, Epoch 135/300, SSE 0.0982281/0.1, Gradient 0.0200328/1e-010 TRAINGDX, Performance goal met. Pass = 3 TRAINGDX, Epoch 0/300, SSE 1.32628/0.1, Gradient 3.27334/1e-010 TRAINGDX, Epoch 50/300, SSE 0.302672/0.1, Gradient 0.255984/1e-010 TRAINGDX, Epoch 100/300, SSE 0.126619/0.1, Gradient 0.0412811/1e-010 TRAINGDX, Epoch 116/300, SSE 0.0998277/0.1, Gradient 0.0253413/1e-010 TRAINGDX, Performance goal met. Pass = 4 TRAINGDX, Epoch 0/300, SSE 0.79332/0.1, Gradient 2.1395/1e-010 TRAINGDX, Epoch 50/300, SSE 0.255685/0.1, Gradient 0.180019/1e-010 TRAINGDX, Epoch 100/300, SSE 0.132418/0.1, Gradient 0.0417362/1e-010 TRAINGDX, Epoch 119/300, SSE 0.099968/0.1, Gradient 0.0240296/1e-010 TRAINGDX, Performance goal met. Pass = 5 TRAINGDX, Epoch 0/300, SSE 1.55642/0.1, Gradient 3.23532/1e-010 TRAINGDX, Epoch 50/300, SSE 0.212807/0.1, Gradient 0.193534/1e-010 TRAINGDX, Epoch 100/300, SSE 0.103347/0.1, Gradient 0.0388252/1e-010 TRAINGDX, Epoch 103/300, SSE 0.0985598/0.1, Gradient 0.0353377/1e-010 TRAINGDX, Performance goal met. Pass = 6 TRAINGDX, Epoch 0/300, SSE 2.64645/0.1, Gradient 5.82475/1e-010 TRAINGDX, Epoch 50/300, SSE 0.201319/0.1, Gradient 0.207158/1e-010 TRAINGDX, Epoch 95/300, SSE 0.0995744/0.1, Gradient 0.037957/1e-010 TRAINGDX, Performance goal met. Pass = 7 TRAINGDX, Epoch 0/300, SSE 0.873476/0.1, Gradient 2.42803/1e-010 TRAINGDX, Epoch 50/300, SSE 0.260369/0.1, Gradient 0.205566/1e-010 TRAINGDX, Epoch 100/300, SSE 0.116383/0.1, Gradient 0.0404004/1e-010 TRAINGDX, Epoch 110/300, SSE 0.09979/0.1, Gradient 0.0296191/1e-010 TRAINGDX, Performance goal met. Pass = 8 TRAINGDX, Epoch 0/300, SSE 0.964115/0.1, Gradient 1.98958/1e-010 TRAINGDX, Epoch 50/300, SSE 0.228885/0.1, Gradient 0.208873/1e-010 TRAINGDX, Epoch 100/300, SSE 0.0987516/0.1, Gradient 0.0377656/1e-010 TRAINGDX, Performance goal met. Pass = 9 TRAINGDX, Epoch 0/300, SSE 0.431969/0.1, Gradient 1.24053/1e-010 TRAINGDX, Epoch 50/300, SSE 0.150932/0.1, Gradient 0.129991/1e-010 TRAINGDX, Epoch 93/300, SSE 0.0990331/0.1, Gradient 0.0385203/1e-010 TRAINGDX, Performance goal met. Pass = 10 TRAINGDX, Epoch 0/300, SSE 1.58001/0.1, Gradient 4.84543/1e-010 TRAINGDX, Epoch 50/300, SSE 0.235781/0.1, Gradient 0.223338/1e-010 TRAINGDX, Epoch 100/300, SSE 0.103898/0.1, Gradient 0.0375891/1e-010 TRAINGDX, Epoch 103/300, SSE 0.0993984/0.1, Gradient 0.0344036/1e-010 TRAINGDX, Performance goal met. There are 10 times,For noise,end ================================================ ================================================ There are no noise,begin TRAINGDX, Epoch 0/500, SSE 0.013799/0.1, Gradient 0.013598/1e-010 TRAINGDX, Performance goal met. There are no noise,end ================================================ ================================================ Testing networks with noise level of 0.00. ================================================ Testing networks with noise level of 0.02. ================================================ Testing networks with noise level of 0.04. ================================================ Testing networks with noise level of 0.06. ================================================ Testing networks with noise level of 0.08. ================================================ Testing networks with noise level of 0.10. ================================================ Testing networks with noise level of 0.12. ================================================ Testing networks with noise level of 0.14. ================================================ Testing networks with noise level of 0.16. ================================================ Testing networks with noise level of 0.18. ================================================ Testing networks with noise level of 0.20. ================================================
运行时,多次运行显示进行模糊处理后效果更好,但是也出现了模糊处理后效果不好的情况。