Professional Documents
Culture Documents
and Division
8
Experiment-1
import numpy as np
class Perceptron:
def __init__(self, num_inputs, learning_rate=0.01, epochs=100):
self.learning_rate = learning_rate
self.epochs = epochs
self.weights = np.zeros(num_inputs + 1) # +1 for the bias
self.errors = []
if __name__ == "__main__":
# Example usage:
training_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
labels = np.array([0, 0, 0, 1])
class Perceptron:
def __init__(self, num_inputs, learning_rate=0.01, epochs=100):
self.learning_rate = learning_rate
self.epochs = epochs
self.weights = np.zeros(num_inputs + 1) # +1 for the bias
self.errors = []
if __name__ == "__main__":
# Example usage:
training_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
labels = np.array([0, 1, 1, 1])
perceptron = Perceptron(num_inputs=2, learning_rate=0.1, epochs=100)
perceptron.train(training_data, labels)
function add=ADD(a, b)
add=a+b;
end
sub=a-b;
end
multiply=a*b;
end
divide=a/b;
end
OUTPUT:
Experiment-5(a)
Write a M-file to calculate the weights for the following patterns using
hetero associative neural net for mapping four input vectors to two output
vectors.
S1 S2 S3 S4 T1 T2
1 1 0 0 1 0
1 0 1 0 1 0
1 1 1 0 0 1
0 1 1 0 0 1
%Hetero 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:
Experiment-5(b)
net to store the vector(1 1 -1 -1). Test the response of the network by
unknown vector.
clc;
clear;
x=[1 1 -1 -1];
w=zeros(4,4);
w=x'*x;
yin=x*w;
for i=1:4
if yin(i)>0
y(i)=1;
else
y(i)=-1;
end
end
disp('Weight matrix');
disp(w);
if x==y
else
end
OUTPUT:
Experiment-5(c)
Write an M-file to store the vector (-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.
clc;
clear;
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)
else
end
OUTPUT:
Experiment-5(d)
Write a MATLAB program to find the weight matrix in bipolar form for
S(1) = (1 1 0) t1(1) = (1 0)
S(2) = (1 0 1) t(2) = (0 1)
clc;
clear;
x=2*s-1
y=2*t-1
w=zeros(3,2);
for i=1:2
w=w+x(i,:)'*y(i,:);
end
disp('The calculated weight matrix');
disp(w);
OUTPUT:
Experiment-6
Write a MATLAB program to store the vector (1 1 1 -1). Find the weight
matrix with no self-connection. Test this using discrete hopfield net with
discrete Hopfield net uses binary vectors]. Also the given pattern in binary
form is [1 1 1 0].
clc;
clear;
x=[1 1 1 0];
tx=[0 0 1 0];
w=(2*x'-1)*(2*x-1);
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('converged output');
disp(y);
con=0;
end
end
OUTPUT: