[神经网络]Matlab神经网络原理5.6.2节 - 线性感知器实现异或

版权声明:转载请说明Zhonglihao原创 https://blog.csdn.net/xeonmm1/article/details/83628808
%% 清理
close all;
clear,clc;

%% 定义变量
P1 = [0,0,1,1;0,1,0,1]; % 4点坐标
p2 = P1(1,:).^2;        % x轴平方扩展
p3 = P1(1,:).*P1(2,:);  % 两轴相乘扩展
p4 = P1(2,:).^2;        % y轴平方扩展
P = [P1(1,:);p2;p3;p4;P1(2,:)] % 汇合输入向量
d = [0,1,1,0]           % 期望的异或输出

lr = maxlinlr(P,'bias'); % 求出最大学习率

%% 线性网络实现
net = linearlayer(0,lr);
net = train(net,P,d);

%% 显示
disp('网络输出');
Y1 = sim(net,P)
disp('网络二值输出');
YY1 = Y1>=0.5
disp('最终权值');
w1 = [net.iw{1,1},net.b{1,1}]

plot([0,1],[0,1],'o','LineWidth',2);
hold on;
plot([0,1],[1,0],'d','LineWidth',2);
axis([-0.1,1.1,-0.1,1.1]);
xlabel('x');ylabel('y');
hold on;
title('线性神经网络求解异或逻辑');
x = -0.1:.1:1.1;y = -0.1:.1:1.1;

N = length(x);
X = repmat(x,1,N);
Y = repmat(y,N,1);Y = Y(:);Y = Y';

P = [X;X.^2;X.*Y;Y.^2;Y];
yy = net(P);
y1 = reshape(yy,N,N);
[C,h] = contour(x,y,y1,1);% 等高线详情请参考matlab官方文档
clabel(C,h);
legend('0','1','线性神经网络分类面');

猜你喜欢

转载自blog.csdn.net/xeonmm1/article/details/83628808