mamech
Full Member level 3
- Joined
- Nov 9, 2010
- Messages
- 176
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,296
- Activity points
- 3,135
hello everyone
I am newcomer to the world of AI, and I tried to make a matlab code where the user enter 4 points on the graph, and the program draw a line that separate the last point (which is supposed to be always class 1) from the other 3 points (supposed always to be class 0).
The main problem that I found bad performance from the program. In the beginning, I tested the program in learning AND gate, and it works very fine and it could get the right answer may be only in 10 or 100 iterations, but once I made it take input from the user, I found that the program sometimes classifies right and sometimes wrong even with 10000 iterations!!!
Note: I select the 4 points on the graph always in form of linearly separable formation, so we can exclude the misclassification because of this mistake
I am newcomer to the world of AI, and I tried to make a matlab code where the user enter 4 points on the graph, and the program draw a line that separate the last point (which is supposed to be always class 1) from the other 3 points (supposed always to be class 0).
The main problem that I found bad performance from the program. In the beginning, I tested the program in learning AND gate, and it works very fine and it could get the right answer may be only in 10 or 100 iterations, but once I made it take input from the user, I found that the program sometimes classifies right and sometimes wrong even with 10000 iterations!!!
Note: I select the 4 points on the graph always in form of linearly separable formation, so we can exclude the misclassification because of this mistake
Code:
close all
clear
clc
Xa=[];
Ya=[];
Xb=[];
Yb=[];
input=[];
num_ip = 4;
bias = -1;
learning_rate = 0.1;
weights = rand(3,1);
z=0:0.1:10
iterations = 1000
while(1)
if(length(Xa)==4)
break;
end
axis([0 10 0 10]); % open an axis from 0__>10 in each axis
grid on
[x,y]=ginput(1); % to get input from user .the programm Continue to get input from user
% until you press enter .
plot(x,y,'ro'); % plot the point
hold on
if(isempty(x)) % if you press enter the programm break
break;
end
Xa(end+1) = x; % to save the x data in a vector
Ya(end+1) = y; % to save the y data in a vector
end
tempx=[Xa ]'
tempy= [Ya]';
input=[tempx tempy];
desired_out = [0;0;0;1];
for i = 1:iterations
out = zeros(4,1);
for j = 1:num_ip
y = weights(1,1)*input(j,1)+ weights(2,1)*input(j,2)+weights(3,1)*bias;
if(sign(y)==0 || sign(y)==1 )
out(j) = 1 ;
end
if(sign(y)==-1)
out(j) = 0 ;
end
delta = desired_out(j)-out(j);
weights(1,1) = weights(1,1)+learning_rate*input(j,1)*delta;
weights(2,1) = weights(2,1)+learning_rate*input(j,2)*delta;
weights(3,1) = weights(3,1)+learning_rate*bias*delta;
hold on
end
end
classifier= -(weights(2,1)/weights(1,1))*z - bias*(weights(3,1)/weights(1,1));
plot(z,classifier)