Implementing perceptron using while and for

1 Ansicht (letzte 30 Tage)
Nick Vasilakis
Nick Vasilakis am 14 Apr. 2022
Kommentiert: Nick Vasilakis am 14 Apr. 2022
Hello, so i'm trying to implement a perceptron in Matlab in order to seperate the letters I and O by using a while and a for loop. I run the code but it wont come to an end. Any ideas?
I've attached the Excel file from where I import my data. In the first column is the number of data, in the second the number of pixels, the third is the variance on the horizontal axis and the final column is the classes, where with 1 is the letter I and with -1 the letter O.
Here's the script i use to call the function and initialize my data:
clear
clc
data=xlsread("O_I_classification data.xlsx");
x1=data(:,2); %Ορισμός της μεταβλητής X1
x2=data(:,3); %Ορισμός της μεταβλητής Χ2
arithmos_dedomenwn=data(:,1); %Ως arithmos_dedomenwn ορίζεται η πρώτη στήλη των δεδομένων που περιέχει τον αριθμό των δεδομένων
y=data(:,4); %Ως y ορίζεται η τελευταία στήλη που περιλαμβάνει τις κλάσεις (δεδομένα εξόδου).
[w1,w2,b,counter]= Ask31(x1,x2,y);
And here's the function I've managed to write:
function [w1,w2,b,counter]= Ask31(x1,x2,y)
x_k=[x1,x2];
x_k_ones=[x_k ones(59,1)];
row=size(x_k_ones);
W=rand(3,1)';
counter=0;
for i=1:row
counter=counter+1;
z=sum(x_k_ones(i,:).*W);
y_problepsi=sign(z);
W(i,1)=W(i,1)+0.7*(y(i,1)-y_problepsi(i,1)).*x_k_ones(i,1);
while sum(abs(y-y_problepsi))~=0
delta_w=0.7*(y-y_problepsi).*x_k_ones;
W=W+delta_w;
if isequal(y,y_problepsi)
break
end
end
end
w1=W(1,1);
w2=W(1,2);
b=W(1,3);
end

Antworten (1)

Geoff Hayes
Geoff Hayes am 14 Apr. 2022
@Nick Vasilakis - your while loop looks like
while sum(abs(y-y_problepsi))~=0
delta_w=0.7*(y-y_problepsi).*x_k_ones;
W=W+delta_w;
if isequal(y,y_problepsi)
break
end
end
yet no where in the code do the values for the condition, y and y_problepsi change. This could be the reason why you are getting stuck in this loop. I recommend reviewing how the W is defined and whether it should be used to update y or y_problepsi.
  1 Kommentar
Nick Vasilakis
Nick Vasilakis am 14 Apr. 2022
I've changed the code, yet the problem remains.Now I've written:
function [w1,w2,b,counter]= Ask31(x1,x2,y)
x_k=[x1,x2];
x_k_ones=[x_k ones(59,1)];
W=rand(3,1)';
y_problepsi=sign(x_k_ones(:,1).*W(1,1));
counter=0;
while sum(abs(y_problepsi-y))~=0
for i=1:59
counter=counter+1;
y_problepsi(i)=sign(x_k_ones(i,:)*W');
W=W+0.01*(y(i)-y_problepsi(i))*x_k_ones(i);
end
if isequal(y,y_problepsi)
break
end
end
w1=W(1,1);w2=W(1,2);b=W(1,3);
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Sequence and Numeric Feature Data Workflows finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by