Implementing perceptron using while and for

조회 수: 3 (최근 30일)
Nick Vasilakis
Nick Vasilakis 2022년 4월 14일
댓글: Nick Vasilakis 2022년 4월 14일
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

답변 (1개)

Geoff Hayes
Geoff Hayes 2022년 4월 14일
@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
Nick Vasilakis
Nick Vasilakis 2022년 4월 14일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Language Support에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by