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 Center および File ExchangeLanguage Support についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by