我使用以下代码实现了单层感知器:
%%
clear all;close all;clc
%%
uni1 = 0.5 + rand(250,2);
uni2 = 1.5 + rand(250,2);
n = size(uni1,1);
m = size(uni2,2);
uni1 = [uni1 -1*(ones(n,1))];
uni2 = [uni2 ones(n,1)];
data = [uni1;uni2];
a=randsample(1:2*n,2*n);
data=data(a,:);
b = 1;
w = [0.5;0.5;0.5];
x = [ones(2*n,1) data(:,1:2)];
d = data(:,3);
y = zeros(2*n,1);
alpha = 0.01;
%%
idx1 = data(:,3)==-1;
data(idx1,3)=0;
idx = logical(data(:,3));
figure;
scatter(data(idx,1),data(idx,2),'bo')
hold on
scatter(data(~idx,1),data(~idx,2),'rx')
%%
itr = 300;
for j=1:itr
for i=1:2*n
aa = w'*(x(i,:))';
y(i) = sign(w'*(x(i,:))');
if y(i) ~= d(i)
w = w + alpha*(x(i,:)' * d(i));
end
Xax = 0.5:0.5:3; Yax=-(b*w(1)+Xax*w(2))/w(3);
plot(Xax,Yax,'k--')
end
end
我想在培训过程中画出一条线来看看它是如何调整的。我将绘图函数放在for循环中,但它只绘制了最后一行。我怎样才能实现目标?
谢谢