Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

GenerateDataSet

clf
% Line Plot
x1 = 0:0.1:5;
x2 = 2*x1 + 3;
plot(x1,x2)
grid on
hold on

liner = @(x1,x2) x2 - 2*x1 -3;

% Shorter Code
X = 5*rand(1,1000);
Y = max(x2)*rand(1,1000);
star = [];

% Loop check
for ii = 1:size(Y,2)
if liner(X(ii),Y(ii)) > liner(x1,x2)
scatter(X(ii),Y(ii),'ro')
star(ii) = 0;
%disp('*')
else
scatter(X(ii),Y(ii),'b*')
star(ii) = 1;
%disp('o')
end
end

dataset = [X' Y' star'];


save('dataset')

LabTask1
clf
% Line Plot
x1 = 0:0.1:5;
x2 = 2*x1 + 3;
plot(x1,x2)
grid on
hold on

liner = @(x1,x2) x2 - 2*x1 -3;

% Shorter Code
mydata = 5*rand(1,100);
b = max(x2)*rand(1,100);

% Loop check
for ii = 1:size(mydata,2)
if liner(mydata(ii),b(ii)) > liner(x1,x2)
scatter(mydata(ii),b(ii),'o')
%disp('*')
else
scatter(mydata(ii),b(ii),'*')
%disp('o')
end
end

% % Random Data points


% data1 = [];
% data2 = [];
% for ii = 1:200
% if rand > 0.5
% sign1 = -1;
% else
% sign1 = 1;
% end
% tempdata1 = sign1*5*rand;
%
% if rand < 0.5
% sign2 = -1;
% else
% sign2 = 1;
% end
% tempdata2 = sign2*5*rand;
%
% vert = [tempdata1; tempdata2];
% data1 = [data1 vert];
% end
% plot(x1,x2)
% grid on
% hold on
% scatter(data1(1,:),data1(2,:))
Load data set
load('dataset')
X = dataset(:,1);
Y = dataset(:,2);
star = dataset(:,3);

% Plot DataSet
figure(2)
hold on
for ii = 1:size(dataset,1)
if star(ii) == 0
scatter(X(ii),Y(ii),'o')
else
scatter(X(ii),Y(ii),'*')
end
end
PerceptronAlgorithmExample
clc,clear,close
mydata = rand(500,2);
% Separate the data into two classes
% acceptindex = abs(mydata(:,1)-mydata(:,2))>0.012;
acceptindex = abs(mydata(:,1)-mydata(:,2))>0.012;
mydata = mydata(acceptindex,:); % data
myclasses = mydata(:,1)>mydata(:,2); % labels
[m n]=size(mydata);
%training data
x=mydata(1:400,:); y=myclasses(1:400);
% test data
xt=mydata(401:m,:); yt=myclasses(401:m);
%=====================================
% Train the perceptron
%=====================================
[w,b,pass] = PerecptronTrn(x,y)
Iterations=pass
%=====================================
% Test
%=====================================
e=PerecptronTst(xt,yt,w,b)

disp(['Test_Errors=' num2str(e) ' Test Data Size= ' num2str(m-


400)])

%=====================================
% Draw the result (sparating hyperplane)
%=====================================
l=y==0;
hold on
plot(x(l,1),x(l,2),'ko' );
plot(x(~l,1),x(~l,2),'m*');
% [l,p]=size(x);
plot([0,1],[0,1],'r-')
axis([0 1 0 1]), axis square, grid on
drawnow

PerecptronTrn
function [w,b,pass]=PerecptronTrn(x,y);
% %Rosenblatt's Perecptron
tic
[l,p]=size(x);
w=zeros(p,1); % initialize weights
b=0; % initialize bias
ier=1; % initialize a misclassification indicator
pass=0; % number of iterations
n=0.5; % learning rate
r=max(sqrt(sum(x))); % max norm
while ier==1, %repeat until no error
ier=0;
e=0; % number of training errors
for i=1:l % a pass through x
w=w'+(n*y(i))*(x(i,:));
b=b+n*y(i)*(r^2);
e=e+1 ; % number of training errors
w=w';
end;
ee=e; % number of training errors
if ee>0 % cuntinue if there is still errors
ier=1;
end
pass=pass+1; % stop after 10000 iterations
if pass==10000
ier=0;
pass=0;
end;
end;
disp(['Training_Errors=' num2str(e) ' Training data Size='
num2str(l)])
toc

PerecptronTst
function e=PerecptronTst(x,y,w,b);
%==========================================
% Testing phase
%==========================================
tic
[l,p]=size(x);
e=0; % number of test errors
for i=1:l
xx=x(i,:); % take one row
ey=xx*w+b % apply the perceptron classification rule
if ey>=0.5
ey=1;
else
ey=0;
end
if y(i)~=ey
e=e+1
end;
end
toc
Training
load('dataset')
X = dataset(:,1);
Y = dataset(:,2);
star = dataset(:,3);
w1 = 0; w0 = 1; w2 = 1;
Model = @(x1,x2) w2*x2 - w1*x1 - w0;
% Plot DataSet
figure(2)
clf
hold on
for ii = 1:size(dataset,1)
if star(ii) == 0
scatter(X(ii),Y(ii),'ro')
else
scatter(X(ii),Y(ii),'b*')
end
end

for ii = 1:size(dataset,1)
alpha = 0.1;
predict = Model(X(ii),Y(ii));
if predict > 0
predict = 0;
else
predict = 1;
end

w0 = w0 + alpha*(star(ii)-predict);
w1 = w1 + alpha*(star(ii)-predict)*X(ii);
w2 = w2 + alpha*(star(ii)-predict)*Y(ii);
end

x = -1.5:1.5;
Line = @(x) (w1/w2)*x + w0/w2;
plot(Line(x))

% w0 = 4.3471
% w1 = 13.0375

Training_v2
load('dataset')
X = dataset(:,1);
Y = dataset(:,2);
star = dataset(:,3);
w1 = 0; w0 = 3;
Model = @(x1,x2) x2 - w1*x1 - w0;
% Plot DataSet
figure(2)
clf
hold on
for ii = 1:size(dataset,1)
if star(ii) == 0
scatter(X(ii),Y(ii),'o')
else
scatter(X(ii),Y(ii),'*')
end
end
title('Data Points')
xlabel('x1'), ylabel('x2')
axis([min(X) max(X) min(Y) max(Y)]);
iterT = 100;
for iter = 1:iterT
loss = []; count = 1;
alpha = 100/(100+i);
for ii = (count-1)*size(dataset,1)/iterT+1 :
count*size(dataset,1)/iterT+1
predict = Model(X(ii),Y(ii));
if predict > 0
predict = 0;
else
predict = 1;
end
loss = [loss star(ii)-predict];
end
avgLoss = sum(loss)/length(loss);
w0 = w0 + alpha*(avgLoss);
w1 = w1 + alpha*(avgLoss)*X(ii);
end
w0
w1
x = 0:5;
Line = @(x) w1*x + w0;
plot(x,Line(x))

% w0 = 4.3471
% w1 = 13.0375

Lab11 (neualnetwork)
GenerateDataSet
figure(1)
clf
% Equation for Comparison
liner = @(x1,x2) x2 - 2*x1 -3;
plotter = @(x) 2*x1 + 3;
% Line Plot
x1 = 0:0.1:5;
x2 = plotter(x1);
plot(x1,x2, 'LineWidth', 3)
grid on
hold on

% Shorter Code
X = 5*rand(1,500);
Y = 10*rand(1,500);
star = [];

% Loop check
for ii = 1:size(Y,2)
if liner(X(ii),Y(ii)) < 0
scatter(X(ii),Y(ii),'ro')
star(ii) = 0;
%disp('*')
else
scatter(X(ii),Y(ii),'b*')
star(ii) = 1;
%disp('o')
end
end
title('DataSet Generation')
xlabel('x1'), ylabel('x2')
axis([min(X) max(X) min(Y) max(Y)])

dataset = [X' Y' star'];


save('dataset')

NN1
% XOR Function
clear all
dataset = [0,0,0; 0,1,1; 1,0,1; 1,1,0];
sigmoid = @(y) 1/(1+exp(-y));

w13 = 0.5; w14 = 0.9; w23 = 0.4; w24 = 1;


w35 = -1.2; w45 = 1.1;
w03 = 0.8; w04 = -0.1; w05 = 0.3;

for i = 1:1000
%if i < 100
% alpha = 1000/(1000+i);
%else
alpha = 1000/(1000+i);
%end
%i = 4;
%alpha = 0.1;
rower = randi([1,4]);
%mod(i,3);
x1 = dataset(rower,1);
x2 = dataset(rower,2);
y = dataset(rower,3);

% Compute y3, y4
L3 = w23*x2 + w13*x1 - w03;
y3 = 1/(1+exp(-L3)); %sigmoid(L3);
L4 = w24*x2 + w14*x1 - w04;
y4 = 1/(1+exp(-L4)); %sigmoid(L4);

% Compute y5
L5 = w45*y4 + w35*y3 - w05;
y5 = 1/(1+exp(-L5)); %sigmoid(L5);

% Error in Last Layer


E = y - y5;
d5 = y5*(1-y5)*E;

% Desired Changes in weight of Last Layer


deltaw35 = d5*alpha*y3;
deltaw45 = d5*alpha*y4;
deltaw05 = d5*alpha*(-1);

% Error in Hidden Layer


d3 = y3*(1-y3)*d5*w35;
d4 = y4*(1-y4)*d5*w45;

% Desired Changes in weight of Hidden Layer


deltaw03 = d3*alpha*(-1);
deltaw13 = d3*alpha*x1;
deltaw23 = d3*alpha*x2;
deltaw04 = d4*alpha*(-1);
deltaw14 = d4*alpha*x1;
deltaw24 = d4*alpha*x2;

% Weight Updates
w03 = w03 + deltaw03;
w13 = w13 + deltaw13;
w23 = w23 + deltaw23;
w04 = w04 + deltaw04;
w14 = w14 + deltaw14;
w24 = w24 + deltaw24;
w05 = w05 + deltaw05;
w35 = w35 + deltaw35;
w45 = w45 + deltaw45;
end
w = [w03 w13 w23 w04 w14 w24 w05 w35 w45]'
liner = -2:2;
L3 = (-w13*liner + w03)/w23;
L4 = (-w14*liner + w04)/w24;
L5 = (-w35*liner + w05)/w45;
figure(12)
subplot(121), plot(liner, L3, liner, L4)
title('L3,L4'), xlabel('x1'), ylabel('x2')
legend('L3','L4'), grid on, hold on
for ii = 1:size(dataset,1)
if dataset(ii,3) == 0
scatter(dataset(ii,1),dataset(ii,2),'ro')
else
scatter(dataset(ii,1),dataset(ii,2),'b*')
end
end

subplot(122), plot(liner, L5)


title('L5'), xlabel('y3'), ylabel('y4')
legend('L5'), grid on

lab12
GenerateDataSet
figure(1)
clf
% Equation for Comparison
liner = @(x1,x2) x2 - 2*x1 -3;
plotter = @(x) 2*x1 + 3;

% Line Plot
x1 = 0:0.1:5;
x2 = plotter(x1);
plot(x1,x2, 'LineWidth', 3)
grid on
hold on

% Shorter Code
X = 5*rand(1,500);
Y = 10*rand(1,500);
star = [];

% Loop check
for ii = 1:size(Y,2)
if liner(X(ii),Y(ii)) < 0
scatter(X(ii),Y(ii),'ro')
star(ii) = 0;
%disp('*')
else
scatter(X(ii),Y(ii),'b*')
star(ii) = 1;
%disp('o')
end
end
title('DataSet Generation')
xlabel('x1'), ylabel('x2')
axis([min(X) max(X) min(Y) max(Y)])

dataset = [X' Y' star'];


save('dataset')

NN1
% XOR Function
%clear all
dataset = [0,0,0; 0,1,1; 1,0,1; 1,1,0];
sigmoid = @(y) 1/(1+exp(-y));

w13 = 0.5; w14 = 0.9; w23 = 0.4; w24 = 1;


w35 = -1.2; w45 = 1.1;
w03 = 0.8; w04 = -0.1; w05 = 0.3;
SE = 0; MSE4 = [];

for i = 1:1000
%if i < 100
% alpha = 1000/(1000+i);
%else
alpha = 10/(10+i);
%end
%i = 4;
%alpha = 0.1;
rower = randi([1,4]);
%mod(i,3);
x1 = dataset(rower,1);
x2 = dataset(rower,2);
y = dataset(rower,3);

% Compute y3, y4
L3 = w23*x2 + w13*x1 - w03;
y3 = 1/(1+exp(-L3)); %sigmoid(L3);
L4 = w24*x2 + w14*x1 - w04;
y4 = 1/(1+exp(-L4)); %sigmoid(L4);

% Compute y5
L5 = w45*y4 + w35*y3 - w05;
y5 = 1/(1+exp(-L5)); %sigmoid(L5);

% Error in Last Layer


E = y - y5;
d5 = y5*(1-y5)*E;

% Desired Changes in weight of Last Layer


deltaw35 = d5*alpha*y3;
deltaw45 = d5*alpha*y4;
deltaw05 = d5*alpha*(-1);

% Error in Hidden Layer


d3 = y3*(1-y3)*d5*w35;
d4 = y4*(1-y4)*d5*w45;

% Desired Changes in weight of Hidden Layer


deltaw03 = d3*alpha*(-1);
deltaw13 = d3*alpha*x1;
deltaw23 = d3*alpha*x2;
deltaw04 = d4*alpha*(-1);
deltaw14 = d4*alpha*x1;
deltaw24 = d4*alpha*x2;

% Weight Updates
w03 = w03 + deltaw03;
w13 = w13 + deltaw13;
w23 = w23 + deltaw23;
w04 = w04 + deltaw04;
w14 = w14 + deltaw14;
w24 = w24 + deltaw24;
w05 = w05 + deltaw05;
w35 = w35 + deltaw35;
w45 = w45 + deltaw45;

% Difference check
SE = SE + (y-y5)^2;
MSE4 = [MSE4 SE/i];
end
w = [w03 w13 w23 w04 w14 w24 w05 w35 w45]'
liner = -2:2;
L3 = (-w13*liner + w03)/w23;
L4 = (-w14*liner + w04)/w24;
L5 = (-w35*liner + w05)/w45;
figure(12)
subplot(121), plot(liner, L3, liner, L4)
title('L3,L4'), xlabel('x1'), ylabel('x2')
legend('L3','L4'), grid on, hold on
for ii = 1:size(dataset,1)
if dataset(ii,3) == 0
scatter(dataset(ii,1),dataset(ii,2),'ro')
else
scatter(dataset(ii,1),dataset(ii,2),'b*')
end
end

subplot(122), plot(liner, L5)


title('L5'), xlabel('y3'), ylabel('y4')
legend('L5'), grid on

% MSE Plot
%figure(1000), plot(MSE), grid on
%xlabel('Iterations', ylabel('MSE')
%plot(1:1000,MSE,'r',1:1000,MSE2,'b',1:1000,MSE3,'g',1:1000,MSE4,'y')
%legend('100/100+i','1000/1000+i','3000/3000+i','10/10+')
%legend('100/100+i','1000/1000+i','3000/3000+i','10/10+i')

NN1Test
load('w')
w = [w03 w13 w23 w04 w14 w24 w05 w35 w45]
w03 = w(1); w13 = w(2); w23 = w(3);
w04 = w(4); w14 = w(5); w24 = w(6);
w05 = w(7); w35 = w(8); w45 = w(9);

% xaxis = -2:2;
% myline5 = (-w35*xaxis + w05)/w45;
% figure(22)
% plot(xaxis, myline5)
% equator = 1*w35 + 1*w45
disp('----------------------')
x1 = 1, x2 = 1, y = 1

% Compute y3, y4
L3 = w23*x2 + w13*x1 - w03;
y3 = 1/(1+exp(-L3)); %sigmoid(L3);
L4 = w24*x2 + w14*x1 - w04;
y4 = 1/(1+exp(-L4)); %sigmoid(L4);

% Compute y5
L5 = w45*y4 + w35*y3 - w05;
y5 = 1/(1+exp(-L5)) %sigmoid(L5);
if y5 > 0.5
out = 1;
else
out = 0;
end

% Error in Last Layer


disp('Output')
disp(out)

You might also like