How to get my 2D perceptron to converge évery time?

2 visualizaciones (últimos 30 días)
Lieke Ceton
Lieke Ceton el 18 de Oct. de 2019
Respondida: Sai Bhargav Avula el 22 de Oct. de 2019
I am trying to build a simple 2D perceptron in Matlab R2019a on a Windows 10 ASUS that splits a linearly separable set of 20 random data points. The code runs and outputs updates weights, however it only converges 1 in 5 times and I can not find out what is going wrong. The error is low and if it converges it stays at 0 but many times it simply does not. What am I doing wrong? I tried rebuilding the whole thing step-by-step to find the mistake but I have ended up in the exact same place. If I change threshold Z to 10 nothing happens, which feels incorrect. When looking at the weights I feel like the first weights is too small, causing an incorrect y-intercept.
%Initialise variables
datanr = 20; %numbers of points in data set
weights = zeros(3,1);
F = zeros(datanr,1);
Y = zeros(datanr,1); %Output vector (labels 0/1)
Yhat = zeros(datanr,1); %Predicted output vector
Z = 0; %Activation function/threshold
J = zeros(datanr,1); %Squared error/loss function
n = 30; %Amount of iterations
lala = 0;
%Create input X (x1 = xcoordinate, x2 = ycoordinate) and output Y
rng shuffle
X = round((rand(datanr,2))*100); %random integers between 0 and 100
X = [ones(datanr,1) X]; %add a first feature that is always 1
Y(X(:,3)>=0.5*X(:,2)+30) = 1; %Assign labels, F = 0.5x + 30
for b = 1:n
for a = 1:datanr
F(a) = dot(weights,X(a,:)); %Calculate the sum of the weights*features
if F(a) > Z %Compare to threshold
Yhat(a) = 1; %Assign boolean label
else
Yhat(a) = 0;
end
delta = Y(a) - Yhat(a);
weights(1) = weights(1)+0.1*delta*X(a,1);
weights(2) = weights(2)+0.1*delta*X(a,2);
weights(3) = weights(3)+0.1*delta*X(a,3);
end
J(b) = 0.5*sum((Y - Yhat).^2); %Error
if Y - Yhat == 0 & lala == 0
lala = b;
disp(lala);
end
end
x = [0,100];
y = (- weights(1) - weights(2)*x)/weights(3);
figure(1)
Xpos = X(:,2); Ypos = X(:,3);
scatter(Xpos(Y==1),Ypos(Y==1),'r')
hold on
scatter(Xpos(Y~=1),Ypos(Y~=1),'b')
plot(x,y);

Respuestas (1)

Sai Bhargav Avula
Sai Bhargav Avula el 22 de Oct. de 2019
Hi,
By looking at the code, I understand that you haven’t included the bias and it just consist of weights. Try adding the bias to your perceptron and update it according. It would solve the issue.
Hope this helps.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by