0% found this document useful (0 votes)
34 views8 pages

Code

The document describes several perceptron models: 1) An OR perceptron that converges in 2 epochs with weights of 2, 2 and bias of 2. 2) An AND perceptron that converges in 2 epochs with weights of 0.2, 0.2 and bias of 0.2. 3) A McCulloch AND model with weights of 1, 1 and threshold of 2 that correctly classifies the inputs. 4) McCulloch OR and XOR models that also correctly classify the inputs with different weights and thresholds.

Uploaded by

paritosh bobade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views8 pages

Code

The document describes several perceptron models: 1) An OR perceptron that converges in 2 epochs with weights of 2, 2 and bias of 2. 2) An AND perceptron that converges in 2 epochs with weights of 0.2, 0.2 and bias of 0.2. 3) A McCulloch AND model with weights of 1, 1 and threshold of 2 that correctly classifies the inputs. 4) McCulloch OR and XOR models that also correctly classify the inputs with different weights and thresholds.

Uploaded by

paritosh bobade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

OR Perceptron

clear;
clc;

% Input data (4 samples with 2 features each)


x = [1 1 -1 -1; 1 -1 1 -1];
% Desired output (OR function)
t = [1 1 1 -1];

% Initialize weights and bias


w = [0 0];
b = 0;

alpha = input('Enter learning rate: ');


theta = input('Enter the threshold: ');

converged = false;
epoch = 0;

while ~converged
converged = true; % Assume convergence until proven otherwise
for i = 1:4
yin = b + w * x(:, i);

% Activation function (step function)


if yin <= theta
y = -1;
else
y = 1;
end

if y ~= t(i)
converged = false; % We need to keep training
w = w + alpha * (t(i) - y) * x(:, i)';
b = b + alpha * (t(i) - y);
end
end

epoch = epoch + 1;
end

disp('Perceptron OR function');
disp(['Epochs required for convergence: ' num2str(epoch)]);
disp('Final Weight Matrix:');
disp(w);
disp('Final Bias:');
disp(b);

Enter learning rate:


1
Enter the threshold:
1
Perceptron OR function
Epochs required for convergence: 2
Final Weight Matrix:
2 2

Final Bias:
2

AND Perceptron
clear;
clc;
x = [1 1 -1 -1; 1 -1 1 -1];
t = [1 -1 -1 -1];
w = [0 0];
b = 0;
alpha = input('Enter learning rate: ');
theta = input('Enter the threshold: ');
converged = false;
epoch = 0;

while ~converged
converged = true; % Assume convergence until proven otherwise
for i = 1:4
yin = b + x(1, i) * w(1) + x(2, i) * w(2);

% Activation function (step function)


if yin <= theta
y = -1;
else
y = 1;
end

if y ~= t(i)
converged = false; % We need to keep training
for j = 1:2
w(j) = w(j) + alpha * (t(i) - y) * x(j, i);
end
b = b + alpha * (t(i) - y);
end
end

epoch = epoch + 1;
end

disp('Perceptron for AND function');


disp(['Epochs required for convergence: ' num2str(epoch)]);
disp('Final Weight Matrix:');
disp(w);
disp('Final Bias:');
disp(b);
Enter learning rate:
0.1
Enter the threshold:
0.5
Perceptron for AND function
Epochs required for convergence: 2
Final Weight Matrix:
0.2000 0.2000

Final Bias:
0.2000

AND Function using MC Culloch


clear;
clc;
disp('Enter the Weights');
w1 = input('Weight w1=');
w2 = input('Weight w2=');
disp('Enter the theta');
theta = input('theta=');

y = [0 0 0 0];
x1 = [1 1 0 0];
x2 = [1 0 1 0];
z = [1 0 0 0];
con = 1;

while con
zin = x1 * w1 + x2 * w2; % Fixed the calculation of zin
for i = 1:4
if zin(i) >= theta
y(i) = 1;
else
y(i) = 0;
end
end
disp('Output of net');
disp(y);
if isequal(y, z) % Changed '==' to 'isequal' for array comparison
con = 0;
else
disp('Net is not learning');
w1 = input('weight w1=');
w2 = input('weight w2=');
theta = input('theta=');
end
end

disp('McCulloch AND function'); % Corrected spelling


disp('Weights of neuron');
disp(w1);
disp(w2);
disp('Threshold value');
disp(theta);

Enter the Weights


Weight w1=
1
Weight w2=
1
Enter the theta
theta=
2
Output of net
1 0 0 0

McCulloch AND function


Weights of neuron
1

Threshold value
2

OR Function using MC Culloch


clear;
clc;
disp('Enter the weight');
w1=input('Weight w1=');
w2=input('Weight w2=');
disp('Enter threshold value');
theta=input('theta=');
y=[0 0 0 0];
x1=[1 1 0 0];
x2=[1 0 1 0];
z=[1 1 1 0];
con=1;
while con
zin=x1*w1+x2*w2;
for i=1:4
if zin(i)>=theta
y(i)=1;
else
y(i)=0;
end
end
disp('Output of net=');
disp(y);
if y==z
con=0;
else disp('Net is not learning Enter another set of weights and threshold
value');
w1=input('Weigth w1=');
w2=input('Weigth w2=');
theta=input('theta=');

end
end
disp('Mc Culluch pitts net for OR function');
disp('Weight of neuron');
disp(w1);
disp(w2);
disp('threshold value=');
disp(theta);

Weigth w1=
1
Weigth w2=
1
theta=
1
Output of net=
1 1 1 0

Mc Culluch pitts net for OR function


Weight of neuron
1

threshold value=
1
XOR function using mc Culloch
clear;
clc;

disp('Enter the weights');


w11 = input('Weight w11=');
w12 = input('Weight w12=');
w21 = input('Weight w21=');
w22 = input('Weight w22=');
v1 = input('Weight v1=');
v2 = input('Weight v2=');

disp('Enter threshold value');


theta = input('Theta=');

x1 = [0 0 1 1];
x2 = [0 1 0 1];
z = [0 1 1 0];
converged = false;

while ~converged
zin1 = x1 * w11 + x2 * w12;
zin2 = x1 * w21 + x2 * w22;
y1 = zeros(1, 4);
y2 = zeros(1, 4);

for i = 1:4
if zin1(i) >= theta
y1(i) = 1;
end

if zin2(i) >= theta


y2(i) = 1;
end
end

yin = y1 * v1 + y2 * v2;
y = zeros(1, 4);

for i = 1:4
if yin(i) >= theta
y(i) = 1;
end
end

if isequal(y, z)
converged = true;
else
disp('Net is learning');
w11 = input('Weight w11=');
w12 = input('Weight w12=');
w21 = input('Weight w21=');
w22 = input('Weight w22=');
v1 = input('Weight v1=');
v2 = input('Weight v2=');
theta = input('Theta=');
end
end

disp('McCulloch XOR function');


disp('Weights of the neuron:');
disp(['w11 = ' num2str(w11)]);
disp(['w12 = ' num2str(w12)]);
disp(['w21 = ' num2str(w21)]);
disp(['w22 = ' num2str(w22)]);
disp(['v1 = ' num2str(v1)]);
disp(['v2 = ' num2str(v2)]);
disp(['Theta = ' num2str(theta)]);

Enter the weights


Weight w11=
1
Weight w12=
-1
Weight w21=
-1
Weight w22=
1
Weight v1=
1
Weight v2=
1
Enter threshold value
Theta=
1
McCulloch XOR function
Weights of the neuron:
w11 = 1
w12 = -1
w21 = -1
w22 = 1
v1 = 1
v2 = 1
Theta = 1

You might also like