本文章参考机器学习周志华 基本原理
单层感知机模型
clear;clc;
%% 单层感知机
D = [0,0,0;0,1,0;1,0,0;1,1,1];
numinput = 2;
numoutput = 1;
eta = 0.1;
w = randn(1,numinput);
theta = randn(1,numoutput);
ITER = 100;
Text = [0.45,0.77;0.22,0.31];
figure(1)
axis equal
for j = 1:ITER
for i = 1:size(D,1)
output = D(i,1:2)*w';
output = 1./(1+exp(-(output-theta)));
% output(output<0.5) = 0;
% output(output>=0.5) = 1;
dw = eta.*(D(i,3)-output)*D(i,1:2);
theta = theta - eta.*(D(i,3)-output);
w = w+dw;
end
scatter(D(1:3,1),D(1:3,2),'r')
scatter(D(4,1),D(4,2),'b')
scatter(Text(:,1),Text(:,2),'k')
axis equal
pause(0.1)
hold off
axis equal
plot([0,(theta)/(w(1))],[(theta)/(w(2)),0],'g')
hold on
axis equal
end
单隐层前馈神经网络
clear;clc;
%% 数据集
Data.D = [0,0,1;1,0,0;1,1,0;0,2,0;2,2,0];
Data.D0 = Data.D(1,:);
Data.D1 = Data.D(2:end,:);
%数据展示
figure(1)
hold on
scatter(Data.D0(:,1),Data.D0(:,2),'r');
scatter(Data.D1(:,1),Data.D1(:,2),'b');
%% 使用单隐层前馈网络和误差逆传播算法寻训练网络
%网络初始化、
Data.numofinput = 2;
Data.numofhide = 3;
Data.numofoutput = 1;
Data.numofparameter = Data.numofhide*(1+Data.numofinput+Data.numofoutput)+Data.numofoutput;
disp("参数共有"+num2str(Data.numofparameter)+"个")
theta = randn(1,Data.numofoutput);
w = randn(Data.numofhide,Data.numofoutput);
gamma = randn(1,Data.numofhide);
v = randn(Data.numofinput,Data.numofhide);
%BP更新
x = Data.D(:,1:2);
y = Data.D(:,3:end);
eta = 0.1;
ITER = 100;
for j = 1:ITER
for i = 1:size(Data.D,1)
b = x(i,:)*v;
b = 1./(1+exp(-(b-gamma)));
% b(b>=0.5) = 1;
% b(b<0.5) = 0;
beta = b*w;
y_ba = 1./(1+exp(-(beta-theta)));
% y_ba(y_ba>=0.5) = 1;
% y_ba(y_ba<0.5) = 0;
g = y_ba.*(1-y_ba).*(y(i,:)-y_ba);
w = w+eta.*b'*g;
theta =theta -eta.*g;
e = b.*(1-b).*(w*g')';
v = v+x(i,:)'*e;
gamma = gamma-eta.*e;
end
end
%% 验证
Text = [-1,-1;1,2];
text_res = zeros(2,1);
for i = 1:2
text_b = Text(i,:)*v;
text_b = 1./(1+exp(-(text_b-gamma)));
% text_b(text_b<0.5) = 0;
% text_b(text_b>=0.5) = 1;
text_beta = text_b*w;
text_beta = 1./(1+exp(-(text_beta-theta)));
% text_beta(text_beta<0) = 0;
% text_beta(text_beta>=0) = 1;
text_res(i,:) = text_beta ;
end
text_res(text_res<0) = 0;
|