⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s6_3_q4.m

📁 Duda《模式分类》第二版第1、3、5章部分课后习题和上机题的解答和程序代码
💻 M
字号:
clear all;close all;clc
% 输入训练参数 [1 1;1 -1;-1 1;-1 -1]
train_patterns = [0.5 0.7;0.8 -0.5;-0.7 0.8;-0.9 -0.85]';
train_targets = [0 1 1 0]';
params = [2 1e-8 0.3];
% 按照‘从左到右,从下到上’的次序
% 依次将决策区域每个点的坐标输入到坐标序列 test_patterns 中
x1=-1:0.05:1;
x2=x1;
lx=length(x1);
i=1:lx*lx;
ir=mod(i,lx);  ir(find(ir==0))=lx;
ic=ceil(i/lx);
test_patterns = [x1(ir);x2(ic)];
% 利用随机反向传播算法计算网络权值(Wh,Wo)和分类结果(test_targets)
[test_targets, test_hiddens, Wh, Wo, J] = Backpropagation_Stochastic(train_patterns, train_targets, test_patterns, params);
% 显示网络各层的连接权值
disp('           ')
disp('input_to_hidden_unit_weights = ')
disp('           ')
disp('w11          w21           w01')
disp(num2str(Wh(1,:)))
disp('           ')
disp('w12          w22           w02')
disp(num2str(Wh(2,:)))
disp('           ')
disp('hidden_to_output_unit_weights = ')
disp('           ')
disp('w1          w2           w0')
disp(num2str(Wo(1,:)))
disp('           ')
% 显示分类结果
ti = -1:.01:1; 
[cx,cy] = meshgrid(ti,ti);
cd1=griddata(test_patterns(1,:),test_patterns(2,:),test_hiddens(1,:),cx,cy,'linear');
cd2=griddata(test_patterns(1,:),test_patterns(2,:),test_hiddens(2,:),cx,cy,'linear');
cz=griddata(test_patterns(1,:),test_patterns(2,:),test_targets,cx,cy,'linear');

subplot(231);
c1=find(test_hiddens(1,:)==1);
c2=find(test_hiddens(1,:)==0);
plot(test_patterns(1,c1),test_patterns(2,c1),'r.');hold on;
plot(test_patterns(1,c2),test_patterns(2,c2),'b.');
x1=-1:0.1:1;
x2=-(Wh(1,3)+Wh(1,1).*x1)./Wh(1,2);
plot(x1,x2,'y','LineWidth',2);
plot(train_patterns(1,:),train_patterns(2,:),'o','MarkerFaceColor','k','MarkerSize',6);
xlabel('x1');ylabel('x2');title('y1')
axis([-1 1 -1 1]);axis square;

subplot(232);
c1=find(test_hiddens(2,:)==1);
c2=find(test_hiddens(2,:)==0);
plot(test_patterns(1,c1),test_patterns(2,c1),'r.');hold on;
plot(test_patterns(1,c2),test_patterns(2,c2),'b.');
x3=-(Wh(2,3)+Wh(2,1).*x1)./Wh(2,2);
plot(x1,x3,'y','LineWidth',2);
plot(train_patterns(1,:),train_patterns(2,:),'o','MarkerFaceColor','k','MarkerSize',6);
xlabel('x1');ylabel('x2');title('y2')
axis([-1 1 -1 1]);axis square;

subplot(233);
c1=find(test_targets==1);
c2=find(test_targets==0);
plot(test_patterns(1,c1),test_patterns(2,c1),'r.');hold on;
plot(test_patterns(1,c2),test_patterns(2,c2),'b.');
plot(x1,x2,'y','LineWidth',2);
plot(x1,x3,'y','LineWidth',2);
plot(train_patterns(1,:),train_patterns(2,:),'o','MarkerFaceColor','k','MarkerSize',6);
xlabel('x1');ylabel('x2');title('z')
axis([-1 1 -1 1]);axis square;

subplot(234);
mesh(cx,cy,cd1);axis([-1 1 -1 1 0 2]);
xlabel('x1');ylabel('x2');zlabel('y1');
subplot(235);
mesh(cx,cy,cd2);axis([-1 1 -1 1 0 2]);
xlabel('x1');ylabel('x2');zlabel('y2');
subplot(236);
mesh(cx,cy,cz);axis([-1 1 -1 1 0 2]);
xlabel('x1');ylabel('x2');zlabel('z');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -