Activation
clc;
clear;
x=[-10:2:10];
e=exp(-x);
y1=x;
y2=1./(1+e);
y3=(1-e)./(1+e);
subplot(1,3,1);
plot(x,y1);
title('linear');
subplot(1,3,2);
plot(x,y2);
title('sigmoidal');
subplot(1,3,3);
plot(x,y3);
title('bipolar');
Hebb's
x=[1 1; 1 -1;-1 1;-1 -1];
y=[1 -1 -1 -1]
w1=0;
w2=0;
b=0;
for i =1:4
w1=w1+x(i)*y(i);
w2=w2+x(i)*y(i);
b=b+y(i);
disp(w1);
disp(w2);
disp(b);
end
disp('final weight w1');
disp(w1);
disp('final weight w2');
disp(w2);
disp('final bias');
disp(b);
Mcpitts
and:-
clear;
clc;
x1=[1 1 0 0];
x2=[1 0 1 0];
y=zeros(1,4);
disp('enter weights');
w1=input('weight w1');
w2=input('weight 2');
disp('enter threshold');
t=input('threshold');
yin=zeros(1,4);
for i=1:4
yin(i)=x1(i)*w1+x2(i)*w2;
if yin(i)>=t
y(i)=1;
else
y(i)=0;
end
end
disp('the outputs are');
disp(y);
andnot:-
clear;
clc;
%Getting weights and threshold value
disp('Enter weights');
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=[0 1 0 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('weight w1=');
w2=input('weight w2=');
theta=input('theta=');
end
end
disp('Mcculloch-Pitts Net for ANDNOT function');
disp('Weights of Neuron');
disp(w1);
disp(w2);
disp('Threshold value');
disp(theta);
xor:-
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=[1 1 0 0];
x2=[1 0 1 0];
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);
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 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);
Adeline_and
clc;
clear;
disp('adaline and');
x1=[1 1 -1 -1];
x2=[1 -1 1 -1];
t=[1 -1 -1 -1];
b=[0 0 0 0];
yin=[0 0 0 0];
er=[0 0 0 0];
a=0.2;
error=[0 0 0 0];
w1=0.2;
w2=0.2;
b=0.2;
epoch=[0 0 0 0 0 0 0 0];
for j=1:8
for i=1:4
yin(i)=b+(x1(i)*w1)+(x2(i)*w2);
er(i)=t(i)-yin(i);
w1=w1+((0.2)*er(i)*x1(i));
w2=w2+((0.2)*er(i)*x2(i));
b=b+((0.2)*er(i));
error(i)=er(i)*er(i);
disp(w1);
disp(w2);
disp(b);
disp(er(i));
epoch(j)=epoch(j)+error(i);
end
disp('epoch');
disp('final weights');
disp('wi');
disp(w1);
disp('w2');
disp(w2);
disp('bias');
disp(b);
end
for j=1:8
disp('epochs');
disp(epoch(j));
end
for j=1:8
if epoch(j+1)>epoch(j)
disp('consider the epoch');
disp(epoch(j));
break;
end
end
Adaline_OR
clc;
clear;
disp('adaline or');
x1=[1 1 -1 -1];
x2=[1 -1 1 -1];
t=[1 1 1 -1];
b=[0 0 0 0];
yin=[0 0 0 0];
er=[0 0 0 0];
a=0.2;
error=[0 0 0 0];
w1=0.2;
w2=0.2;
b=0.2;
epoch=[0 0 0 0 0 0 0 0];
for j=1:8
for i=1:4
yin(i)=b+(x1(i)*w1)+(x2(i)*w2);
er(i)=t(i)-yin(i);
w1=w1+((0.2)*er(i)*x1(i));
w2=w2+((0.2)*er(i)*x2(i));
b=b+((0.2)*er(i));
error(i)=er(i)*er(i);
disp(w1);
disp(w2);
disp(b);
disp(er(i));
epoch(j)=epoch(j)+error(i);
end
disp('epoch');
disp('final weights');
disp('wi');
disp(w1);
disp('w2');
disp(w2);
disp('bias');
disp(b);
end
for j=1:8
disp('epochs');
disp(epoch(j));
end
for j=1:8
if epoch(j+1)>epoch(j)
disp('consider the epoch');
disp(epoch(j));
break;
end
end
Adaline_XOR
clc;
clear;
disp('adaline xor');
x1=[1 1 -1 -1];
x2=[1 -1 1 -1];
t=[-1 1 1 -1];
b=[0 0 0 0];
yin=[0 0 0 0];
er=[0 0 0 0];
a=0.2;
error=[0 0 0 0];
w1=0.2;
w2=0.2;
b=0.2;
epoch=[0 0 0 0 0 0 0 0];
for j=1:8
for i=1:4
yin(i)=b+(x1(i)*w1)+(x2(i)*w2);
er(i)=t(i)-yin(i);
w1=w1+((0.2)*er(i)*x1(i));
w2=w2+((0.2)*er(i)*x2(i));
b=b+((0.2)*er(i));
error(i)=er(i)*er(i);
disp(w1);
disp(w2);
disp(b);
disp(er(i));
epoch(j)=epoch(j)+error(i);
end
disp('epoch');
disp('final weights');
disp('wi');
disp(w1);
disp('w2');
disp(w2);
disp('bias');
disp(b);
end
for j=1:8
disp('epochs');
disp(epoch(j));
end
for j=1:8
if epoch(j+1)>epoch(j)
disp('consider the epoch');
disp(epoch(j));
break;
end
end
Autoassociative _ Hebb
%To construct and test auto associative network for input vector using HEBB rule
%PROGRAM:
clear all;
clc;
disp(' AUTO ASSOCIATIVE NETWORK-----HEBB RULE');
w=[0 0 0 0 ;0 0 0 0 ;0 0 0 0 ;0 0 0 0 ];
s=[1 1 1 -1];
t=[1 1 1 -1];
ip=[1 -1 -1 -1];
disp('INPUT VECTOR');
s
for i=1:4
for j=1:4
w(i,j)=w(i,j)+(s(i)*t(j));
end
end
disp('WEIGHTS TO STORE THE GIVEN VECTOR IS');
w
disp('TESTING THE NET WITH VECTOR');
ip
yin=ip*w;
for i=1:4
if yin(i)>0
y(i)=1;
else
y(i)=-1;
end
end
if y==s
disp('PATTERN IS RECOGNIZED')
else
disp('PATTERN IS NOT RECOGNIZED')
end
Autoassociative _ Outer
clear all;
clc;
disp('To test Auto associatie network using outer product rule for following input vector');
x1=[1 -1 1 -1];
x2=[1 1 -1 -1];
n=0;
w1=x1'*x1;
w2=x2'*x2;
wm=w1+w2;
disp('input');
x1
x2
disp('Target');
x1
x2
disp('Weights');
w1
w2
disp('Weight matrix using Outer Products Rule');
wm
yin=x1*wm;
yin
for i=1:4
if(yin(i)>0)
y=1;
else
y=-1;
end
ny(i)=y;
if(y==x1(i))
n=n+1;
end
end
ny
if(n==4)
disp('This pattern is recognized');
else
disp('This pattern is not recognized');
end
n=0;
yin=x2*wm;
yin
for i=1:4
if(yin(i)>0)
y=1;
else
y=-1;
end
ny(i)=y;
if(y==x2(i))
n=n+1;
end
end
ny
if(n==4)
disp('This pattern is recognized');
else
disp('This pattern is not recognized');
end
Hetroasso
%To construct and test Hetero associative network for binary inputs and targets
%PROGRAM:
clear all;
clc;
disp('Heteroassociative Network');
x1=[1 0 0 0];
x2=[1 1 0 0];
x3=[0 0 0 1];
x4=[0 0 1 1];
t1=[1 0];
t2=[1 0];
t3=[0 1];
t4=[0 1];
n=0;
for i=1:4
for j=1:2
w(i,j)=((2*x1(i))-1)*((2*t1(j))-1)+((2*x2(i))-1)*((2*t2(j))-1)+((2*x3(i))-1)*((2*t3(j))-1)+((2*x4(i))-1)*((2*t4(j))-1);
end
end
w
yin1=x1*w
yin2=x2*w
yin3=x3*w
yin4=x4*w
t1=[1 -1];
t2=[1 -1];
t3=[-1 1];
t4=[-1 1];
for i=1:2
if(yin1(i)>0)
y1(i)=1;
elseif (yin1(i)==0)
y1(i)=0;
else
y1(i)=-1;
end
end
y1
for i=1:2
if(y1(i)==t1(i))
n=n+1;
end
end
if (n==2)
disp('The pattern is matched');
else
disp('The pattern is not matched');
end
n=0;
for i=1:2
if(yin2(i)>0)
y2(i)=1;
elseif (yin2(i)==0)
y2(i)=0;
else
y2(i)=-1;
end
end
y2
for i=1:2
if(y2(i)==t2(i))
n=n+1;
end
end
if (n==2)
disp('The pattern is matched');
else
disp('The pattern is not matched');
end
n=0;
for i=1:2
if(yin3(i)>0)
y3(i)=1;
elseif (yin3(i)==0)
y3(i)=0;
else
y3(i)=-1;
end
end
y3
for i=1:2
if(y3(i)==t3(i))
n=n+1;
end
end
if (n==2)
disp('The pattern is matched');
else
disp('The pattern is not matched');
end
n=0;
for i=1:2
if(yin4(i)>0)
y4(i)=1;
elseif (yin4(i)==0)
y4(i)=0;
else
y4(i)=-1;
end
end
y4
for i=1:2
if(y4(i)==t4(i))
n=n+1;
end
end
if (n==2)
disp('The pattern is matched');
else
disp('The pattern is not matched');
end
n-0;
hopfield :
clear all;
clc;
disp('Discrete Hopfield Network');
theta=0;
x=[1 -1 -1 -1;-1 1 1 -1;-1 -1 -1 1]
%Calculating Weight Matrix
w=x'*x
%calculating Energy
k=1;
while(k<=3)
temp=0;
for i=1:4
for j=1:4
temp=temp+(x(k,i)*w(i,j)*x(k,j));
end
end
E(k)=(-0.5)*temp;
k=k+1;
end
%Energy Function for 3 samples
E
%Test for given pattern s=[-1 1 -1 -1]
disp('Given input pattern for testing');
x1=[-1 1 -1 -1]
temp=0;
for i=1:4
for j=1:4
temp=temp+(x1(i)*w(i,j)*x1(j));
end
end
SE=(-0.5)*temp
disp('By synchronous updation method');
disp('The net input calculated is');
yin=x1*w
for i=1:4
if(yin(i)>theta)
y(i)=1;
elseif(yin(i)==theta)
y(i)=yin(i);
else
y(i)=-1;
end
end
disp('The output calculated from net input is');
y
temp=0;
for i=1:4
for j=1:4
temp=temp+(y(i)*w(i,j)*y(j));
end
end
SE=(-0.5)*temp
n=0;
for i=1:3
if (SE==E(i))
n=0;
k=i;
else
n=n+1;
end
end
if(n==3)
disp('Pattern is not associated with any input pattern');
else
disp('The test pattern');
x1
disp('is associated with');
x(k,:)
end
Madaline
%To implement AND function using MADALINE with bipolar inputs and outputs
%PROGRAM:
clear all;
clc;
disp('madaline network for and function bipolar inputs, bipolar targets');
x1=[1 1 -1 -1]; %input pattern
x2=[1 -1 1 -1]; %input pattern
x3=[1 1 1 1]; %x3 for bias
t=[1 -1 -1 -1]; %target
w11=0.1;
w12=0.1;
w21=0.1;
w22=0.1;
b1=0.1;
b2=0.1;
b3=0.5;
v1=0.5;
v2=0.5;
alpha=0.5;
e=2;
delw11=0;
delw12=0;
delw21=0;
delw22=0;
delb1=0;
delb2=0;
delb3=0;
delv1=0;
delv2=0;
epoch=0;
while (e>1.00)
epoch=epoch+1
e=0;
for i=1:4
zin1=x1(i)*w11+x2(i)*w21+b1;
zin2=x1(i)*w12+x2(i)*w22+b2;
z=[zin1 zin2];
if (zin1>=0)
z1=1;
else
z1=-1;
end
if (zin2>=0)
z2=1;
else
z2=-1;
end
hid=[z1 z2];
nety=b3+z1*v1+z2*v2;
if (nety>=0)
y=1;
else
y=-1;
end
nt=[t(i) nety y];
if (t(i)==1)
if (zin1<zin2)
delb1=alpha*(1-zin1);
b1=b1+delb1;
delw11=alpha*(1-zin1)*x1(i);
w11=w11+delw11;
delw21=alpha*(1-zin1)*x1(i);
w21=w21+delw21;
else
delb2=alpha*(1-zin2);
b2=b2+delb2;
delw12=alpha*(1-zin2)*x2(i);
w12=w12+delw12;
delw22=alpha*(1-zin2)*x2(i);
w22=w22+delw22;
end
elseif (t(i)==-1)
if (zin1>0)
delb1=alpha*(-1-zin1);
b1=b1+delb1;
delw11=alpha*(-1-zin1)*x1(i);
w11=w11+delw11;
delw21=alpha*(-1-zin1)*x1(i);
w21=w21+delw21;
else
delb2=alpha*(-1-zin2);
b2=b2+delb2;
delw12=alpha*(-1-zin2)*x2(i);
w12=w12+delw12;
delw22=alpha*(-1-zin2)*x2(i);
w22=w22+delw22;
end
end
del=[delw11 delw21 delb1 delw12 delw22 delb2];
in=[x1(i) x2(i) x3(i)];
bi=[v1 v2 b3];
pr=[in z hid nt del bi]
end
for i=1:4
zin1=b1+x1(i)*w11+x2(i)*w21;
zin2=b2+x1(i)*w12+x2(i)*w22;
z=[zin1 zin2];
if (zin1>=0)
z1=1;
else
z1=-1;
end
if (zin2>=0)
z2=1;
else
z2=-1;
end
nety=v1*z1+v2*z2+b3;
e=e+(t(i)-nety)^2;
end
end