% 数字识别
number0=[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]';
number1=[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....
0 0 1 0 0]';
number2=[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]';
number3=[1 1 1 1 1....
0 0 0 0 1....
0 0 0 0 1....
1 1 1 1 1....
0 0 0 0 1....
0 0 0 0 1....
1 1 1 1 1]';
number4=[1 0 0 0 1....
1 0 0 0 1....
1 0 0 0 1....
1 1 1 1 1....
0 0 0 0 1....
0 0 0 0 1....
0 0 0 0 1]';
number5=[1 1 1 1 1....
1 0 0 0 0....
1 0 0 0 0....
1 1 1 1 1....
0 0 0 0 1....
0 0 0 0 1....
1 1 1 1 1]';
number6=[1 1 1 1 1....
1 0 0 0 0....
1 0 0 0 0....
1 1 1 1 1....
1 0 0 0 1....
1 0 0 0 1....
1 1 1 1 1]';
number7=[1 1 1 1 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]';
number8=[1 1 1 1 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 1 1 1 1]';
number9=[1 1 1 1 1....
1 0 0 0 1....
1 0 0 0 1....
1 1 1 1 1....
0 0 0 0 1....
0 0 0 0 1....
1 1 1 1 1]';
alphabet=[number0,number1,number2,number3,number4,number5,number6,number7,number8,number9];
targets = eye(10);
%[alphabet,targets] = date;
P =alphabet; %alphabet=[number0,number1,number2,number3,number4,number5,number6,number7,number8,number9];
T = targets; %targets = eye(10);
[R,Q] = size(P);
S1 = 5;
[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);
pause % 按任意键继续
% 具有噪声的输入矢量识别网络的训练
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
pause % 按任意键继续
% 再次用无噪声字母训练识别网络
netn.trainParam.goal = 0.1; % 均方误差目标.
netn.trainParam.epochs = 500; % 最大训练步长
net.trainParam.show = 5; % 训练中显示的频率
P = alphabet;
T = targets;
[netn,tr] = train(netn,P,T);
pause % 按任意键测试所训练的网络性能
% 测试所训练的网络性能
% ====================
noise_range = 0:.02:0.2;
max_test = 100;
network1 = [];
network2 = [];
T = targets;
% PERFORM THE TEST
for noiselevel = noise_range
fprintf('Testing networks with noise level of %.2f.\n',noiselevel);
errors1 = 0;
errors2 = 0;
for i=1:max_test
P = alphabet + randn(35,10)*noiselevel;
% TEST NETWORK 1
A = sim(net,P);
AA = compet(A);
errors1 = errors1 + sum(sum(abs(AA-T)))/2;
% fprintf('%.2f %.2f\n',AA,errors1);
%fprintf('AA is %6.2f and errors1 is %8.3f \n', AA, errors1)
% TEST NETWORK 2
An = sim(netn,P);
AAn = compet(An);
errors2 = errors2 + sum(sum(abs(AAn-T)))/2;
echo off
end
% 100组10个矢量的平均误差
network1 = [network1 errors1/10/100];
network2 = [network2 errors2/10/100];
end
echo on
pause % 按任意键显示结果
% 显示结果
% ========
clf
plot(noise_range,network1*100,'--',noise_range,network2*100);
grid
xlabel('Noise Level');
ylabel('Percentage of Recognition');
echo off
%三种情况下数字"8"的表示情况(改变alphabet(:,9)中的数字9为8.7...可以看到不同的数字情况)
%加入0.1的噪声
noisyY1 = alphabet(:,6)+randn(35,1) * 0.1;
figure(2);
plotchar(noisyY1);
A2=sim(net,noisyY1);
A2=compet(A2);
answer=find(compet(A2)==1);
figure(3);
%加入0.1的噪声后的识别结果
plotchar(alphabet(:,answer));
%noiseY2=alphabet(:,10)+randn(35,1)*0.1;
%figure(3);
%plotchar(noiseY2);
% %idealY=alphabet(:,10);
% figure(4)
% plotchar(idealY);
%加入0.2的噪声
% noisyY11 = alphabet(:,6)+randn(35,1) * 0.2;
% figure(4);
% plotchar(noisyY1);
% A21=sim(net,noisyY1);
% A21=compet(A21);
% answer=find(compet(A21)==1);
% figure(5);
% plotchar(alphabet(:,answer));
- 1
- 2
- 3
前往页