Uploaded by Adnan Shiekh

Assignment 2

advertisement
ASSIGNMENT # 2
Write a code for training two inputs AND gate on the principle of error
correction learning:
clear;
close all;
clc;
%p = input fetures
%t = tragets
%w = weights
%b = bias
%% Define the training data:
display ('Training');
% input data;
P = [0 0;
0 1;
1 0;
1 1];
%
T
%
N
Targets;
= [0;0;0;1];
Total examples in training set:
= size (P,1);
%%Plot the training data;
figure,
plotpv(P',T');
grid off;
%% Train perception network
%initialize weights and bias
R=2;
S=1;
%inputs=col-wise
%neurons=row.wise
%initialize weights and biases for training
w = 0.37*ones(S,R); %weights closer to zero
% w=0.7*ones(S,R); %weights closer to 1
b = 0.2; %one bias for each neuron
%epoch = 1; for weight closer to 1
epoch = 5 % for closer to 0
tic
for i = 1:epoch
for j = 1:N
p = P(j,:);
t = T(j);
% calculate output by implementing the hard limit
x = w*p'+b;
Fuzzy Control Systems
ASSIGNMENT # 2
if x<0
a = 0;
else
a = 1;
end
e = t-a;
w= w+e*p;
b = b+e;
end
plotpv(P',T');grid on,
plotpc(w,b);
pause(2);
end
toc
% store the adjusted values:
record_w= w
record_b= b
%%Testing the perception netwrok:
output =zeros(4,1);
for j=1:N
p_test = P(j,:);
y = w*p_test'+b;
if y<=0
a = 0;
else
a = 1;
end
output(j) = a;
end
Fuzzy Control Systems
Download