📄 lms2dim2u.m
字号:
%%%% 2 classes non-linear-classfing
%% Produce sample set and original classfying line
% Produce samples: R0,R1
close all
clear all
M=500; % Maxmum allowably ierative times
N=500; % Nomber of total samples
alf=0.01; % Step length
rpr=0.0001; % Precision
figure(1)
hold on
whitebg(1,'k')
figure(2)
whitebg(2,'k')
% Produce and plot sample set R0 and R1, learning samiple set R=R0+R1
R0=randn(N,2)+ones(N,2);
R1=randn(N,2)-ones(N,2);
R=[R0;R1];
figure(1) %在图1画点
plot(R0(:,1),R0(:,2),'m*')
plot(R1(:,1),R1(:,2),'gx')
% Plot axies
Xmax=max([R0(:,1);R1(:,1)]);
Xmin=min([R0(:,1);R1(:,1)]);
Ymax=max([R0(:,2);R1(:,2)]);
Ymin=min([R0(:,2);R1(:,2)]);
Xmax=ceil(Xmax);
Xmin=floor(Xmin);
Ymax=ceil(Ymax);
Ymin=floor(Ymin);
plot([Xmin,Xmax],[0,0],'b-.') %画坐标系
plot([0,0],[Ymin,Ymax],'b-.')
text(Xmin+0.3,Ymax-0.5,'R0: *'); %标注R
text(Xmin+0.3,Ymax-0.9,'R1: x');
% Produce original W and plot classfying line
W(1)=-rand(1,1);
W(2)=rand(1,1);
W=W/sqrt(W(1)^2+W(2)^2); %化为单位向量
y0=-W(1)*Xmin/W(2);
y1=-W(1)*Xmax/W(2);
figure(1)
plot([Xmin,Xmax],[y0,y1],'y-.','linewidth',3) % Starting
axis([Xmin,Xmax,Ymin,Ymax])
input('Press enter key to continue--');
disp(['Generation',' ','Error',' ','delta-Error',' W= ','W(1)',' ','W(2)',' '])
%% Supervised learning
k=1;
dalt=1;
while (dalt>rpr)&(k<M) % rpr:Relative error , M: allowable loop number
% Learning with R
err=0; % Estimation of square errors in current step
grad=[0,0]; % Estimation of the grad in the step
for n=1:2*N % N: Learning set size
X=[R(n,1),R(n,2)]; % R: N by 2 learning set
y=W*X'; % y: output of current input X
if n<=N % Get designated class value of current X
d=1;
else
d=-1;
end
err=err+(d-y)^2; % Get and cumulating errors by each sample for judgement the error
grad=grad+(y-d)*X; % Get and cumulating grads
end
e(k)=err/(2*N); %
grad=grad/(2*N); % Averging the summation of grads
W=W-alf*grad; % to modify threshold matrix
if k>1 % Display algorithm track
dalt=abs(e(k)-e(k-1))/e(k);
disp([num2str(k),' ',num2str(e(k-1)),' ',num2str(dalt),' ',num2str(W(1)),' ',num2str(W(2)),' '])
end
k=k+1;
end
% Plot end line
y0=-W(1)*Xmin/W(2);
y1=-W(1)*Xmax/W(2);
figure(1)
plot([Xmin,Xmax],[y0,y1],'y','linewidth',3) % Starting
axis([Xmin,Xmax,Ymin,Ymax])
% Learning loop end
figure(2) % Plot error line
whitebg
plot(e)
% Make signs in the wrong classified samples
figure(1)
e1=0;
for n=1:N
X=[R(n,1),R(n,2)];
y=W*X';
if y<0
plot(R(n,1),R(n,2),'rs')
e1=e1+1;
end
end
e2=0;
for n=N+1:2*N
X=[R(n,1),R(n,2)];
y=W*X';
if y>0
plot(R(n,1),R(n,2),'bo')
e2=e2+1;
end
end
disp(['Weight vector W=[',' ',num2str(W),']'])
disp(['Wrong classfied samples with R0 and R1 are ',num2str(e1),' and ',num2str(e2)])
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -