0% found this document useful (0 votes)
76 views9 pages

Neural Practicals

The document provides MATLAB code for implementing various neural network models, including: 1. An XOR function using a McCulloch-Pitts neuron with weights and thresholds updated iteratively until the correct output is produced. 2. A Hebbian network classifying 2D input patterns of letters F and E. 3. A perceptron network for an AND function with bipolar inputs and targets using weight and bias updating.

Uploaded by

mansi
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)
76 views9 pages

Neural Practicals

The document provides MATLAB code for implementing various neural network models, including: 1. An XOR function using a McCulloch-Pitts neuron with weights and thresholds updated iteratively until the correct output is produced. 2. A Hebbian network classifying 2D input patterns of letters F and E. 3. A perceptron network for an AND function with bipolar inputs and targets using weight and bias updating.

Uploaded by

mansi
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/ 9

Q1. Generate XOR function using McCulloch-Pitts neuron by writing an M-file.

Ans 1. The truth table for the XOR function is,


X1

X2

0
0
1
1

0
1
0
1

0
1
1
0

The MATLAB program is given by,


Program
%XOR function using McCulloch-Pitts neuron
clear;
clc;
%Getting weights and threshold value
disp('Enter 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];
con=1;
while con
zin1=x1*w11+x2*w21;
zin2=x1*w21+x2*w22;
for i=1:4
if zin1(i)>=theta
y1(i)=1;
else
y1(i)=0;
end
if zin2(i)>=theta
y2(i)=1;
else
y2(i)=0;
end
end
yin=y1*v1+y2*v2;
for i=1:4
if yin(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');

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-Pitts Net for XOR function');
disp('Weights of Neuron Z1');
disp(w11);
disp(w21);
disp('weights of Neuron Z2');
disp(w12);
disp(w22);
disp('weights of Neuron Y');
disp(v1);
disp(v2);
disp('Threshold value');
disp(theta);
Output
Enter weights
Weight w11=1
weight w12=-1
Weight w21=-1
weight w22=1
weight v1=1
weight v2=1
Enter Threshold Value
theta=1
Output of Net
0
1
1
0
McCulloch-Pitts Net for XOR function
Weights of Neuron Z1
1
-1
weights of Neuron Z2
-1
1
weights of Neuron Y
1
1
Threshold value

Q2. Write a program for Hebb Net to classify 2D input patterns of F and E with *
as +1 and . as -1.
Ans 2. The MATLAB program is given as follows
Program
%Hebb Net to classify two dimensional input patterns
clear;
clc;
%Input Patterns
E=[1 1 1 1 1 -1 -1 -1 1 1 1 1 1 -1 -1 -1 1 1 1 1];
F=[1 1 1 1 1 -1 -1 -1 1 1 1 1 1 -1 -1 -1 1 -1 -1 -1];
x(1,1:20)=E;
x(2,1:20)=F;
w(1:20)=0;
t=[1 -1];
b=0;
for i=1:2
w=w+x(i,1:20)*t(i);
b=b+t(i);
end
disp('Weight matrix');
disp(w);
disp('Bias');
disp(b);
Output
Weight matrix
Columns 1 through 18
0 0 0 0 0 0 0 0 0
Columns 19 through 20
2
2
Bias

0 0 0 0 2

Q3. Write a MATLAB program for perceptron net for an AND function with bipolar inputs and targets.
Ans 3. The truth table for the AND function is given as
X1

X2

The MATLAB program for the above table is given as follows.


%Perceptron for AND funtion
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 Threshold value=');
con=1;
epoch=0;
while con
con=0;
for i=1:4
yin=b+x(1,i)*w(1)+x(2,i)*w(2);
if yin>theta
y=1;
end
if yin <=theta & yin>=-theta
y=0;
end
if yin<-theta
y=-1;
end
if y-t(i)
con=1;
for j=1:2
w(j)=w(j)+alpha*t(i)*x(j,i);
end
b=b+alpha*t(i);
end
end
epoch=epoch+1;
end
disp('Perceptron for AND funtion');
disp(' Final Weight matrix');
disp(w);
disp('Final Bias');
disp(b);
Output
Enter Learning rate=1
Enter Threshold value=0.5
Perceptron for AND funtion
Final Weight matrix
1
1
Final Bias
-1

Q4. WAP for hetero-associative neural network for mapping input and output as given.
Ans 4. The MATLAB program for calculating the weight matrix is as follows
Program
%Hetro associative neural net for mapping input vectors to output vectors
clc;
clear;
x=[1 1 0 0;1 0 1 0;1 1 1 0;0 1 1 0];
t=[1 0;1 0;0 1;0 1];
w=zeros(4,2);
for i=1:4
w=w+x(i,1:4)'*t(i,1:2);
end
disp('weight matrix');
disp(w);
Output
weight matrix
2 1
1 2
1 2
0 0

Q5. Write an Mfile to store the vectors (1 1 1 1 ) and ( 1 1 1 1 ) in an auto


associative net. Find the weight matrix. Test the net with (1 1 1 1) as input.

Ans 5. The MATLAB program for the auto association problem is as follows:
Program
clc;
clear;
x=[1 1 1 1;1 1 1 1];
t=[1 1 1 1];
w=zeros(4,4);
for i=1:2
w=w+x(i,1:4)'*x(i,1:4);
end
yin=t*w;
for i=1:4
if yin(i)>0
y(i)=1;
else
y(i)=1;
end
end
disp('The calculated weight matrix');
disp(w);
if x(1,1:4)==y(1:4) | x(2,1:4)==y(1:4)
disp('The vector is a Known Vector');
else
disp('The vector is a unknown vector');
end
Output
The calculated weight matrix
2
2
2
2
0
0
0
0
The vector is

0
0
0
0
2
2
2
2
an unknown vector.

Q6. Write M-File for Hopfield net for storing vector [1 1 1 0] and test with [0 0 1 0]

Ans 6. The MATLAB program is as follows


Program
%Discrete Hopfield net
clc;
clear;
x=[1 1 1 0];
tx=[0 0 1 0];
w=(2*x'1)*(2*x1);
for i=1:4
w(i,i)=0;
end
con=1;
y=[0 0 1 0];
while con
up=[4 2 1 3];
for i=1:4
yin(up(i))=tx(up(i))+y*w(1:4,up(i));
if yin(up(i))>0
y(up(i))=1;
end
end
if y==x
disp('Convergence has been obtained');
disp('The Converged Ouput');
disp(y);
con=0;
end
end
Output

Convergence has been obtained


The Converged Ouput
1

Q7. Write an M-File for XOR function using Back Propagation Network with binary input,
output.
Ans 7. The MATLAB program is given as follows.
Program
function y=binsig(x)
y=1/(1+exp(-x));
function y=binsig1(x)
y=binsig(x)*(1-binsig(x));
%Back Propagation Network for XOR function with Binary Input and Output
clc;
clear;
%Initialize weights and bias
v=[0.197 0.3191 -0.1448 0.3394;0.3099 0.1904 -0.0347 -0.4861];
v1=zeros(2,4);
b1=[-0.3378 0.2771 0.2859 -0.3329];
b2=-0.1401;
w=[0.4919;-0.2913;-0.3979;0.3581];
w1=zeros(4,1);
x=[1 1 0 0;1 0 1 0];
t=[0 1 1 0];
alpha=0.02;
mf=0.9;
con=1;
epoch=0;
while con
e=0;
for I=1:4
%Feed forward
for j=1:4
zin(j)=b1(j);
for i=1:2
zin(j)=zin(j)+x(i,I)*v(i,j);
end
z(j)=binsig(zin(j));
end
yin=b2+z*w;
y(I)=binsig(yin);
%Backpropagation of Error
delk=(t(I)-y(I))*binsig1(yin);
delw=alpha*delk*z'+mf*(w-w1);
delb2=alpha*delk;
delinj=delk*w;
for j=1:4
delj(j,1)=delinj(j,1)*binsig1(zin(j));
end
for j=1:4
for i=1:2
delv(i,j)=alpha*delj(j,1)*x(i,I)+mf*(v(i,j)-v1(i,j));
end
end
delb1=alpha*delj;
w1=w;
v1=v;
%Weight updation

w=w+delw;
b2=b2+delb2;
v=v+delv;
b1=b1+delb1';
e=e+(t(I)-y(I))^2;
end
if e<0.005
con=0;
end
epoch=epoch+1;
end
disp('BPN for XOR funtion with Binary input and Output');
disp('Total Epoch Performed');
disp(epoch);
disp('Error');
disp(e);
disp('Final Weight matrix and bias');
v
b1
w
b2
Output
BPN for XOR funtion with Binary Input and Output
Total Epoch Performed
5385
Error
0.0050
Final Weight matrix and bias
v=
4.4164 4.4836 2.6086 4.0386
4.5230 2.1693 1.1147 6.6716
b1 =
0.9262 0.5910 0.6254 -1.0927
w=
6.9573
5.5892
5.2180
7.7782
b2 =

0.3536

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy