Practical Manual ON Neural Networks: Dr. Dharm Singh Dr. Naveen Choudhary

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

PRACTICAL MANUAL

ON
NEURAL NETWORKS

Dr. Dharm Singh Dr. Naveen Choudhary

DEPARTMENT OF COMPUTER SCIENCE &


ENGINEERING
COLLEGE OF TECHNOLOGY & ENGINEERING
(Maharana Pratap University of Agriculture. and Technology, Udaipur)
PRACTICAL MANUAL
ON
DATA COMMUNICATION AND
NETWORKS

Dr. Dharm Singh Dr. Naveen Choudhary


Assistant Professor Associate Professor & Head

2008

DEPARTMENT OF COMPUTER SCIENCE &


ENGINEERING
COLLEGE OF TECHNOLOGY & ENGINEERING
(Maharana Pratap University of Agriculture. and Technology, Udaipur)
PREFACE

The practical manual on “Neural Networks” has been prepared for B.E. Computer Science &
Engineering Students. The “Neural Networks” is increasingly becoming the default choice of the
IT industry especially industries involving in Networking. Therefore, for proper development of
“Neural Networks” skills among the students this practical manual has been prepared.

The manual contains the exercise programs and their solution for easy & quick
understanding of the students. The author has gathered material from Books, Journals and Web
resources. We hope that this practical manual will be helpful for students of Computer Science &
Engineering for understanding the subject from the point of view of applied aspects.

There is always scope for improvement in the manual. We would appreciate to receive
valuable suggestions from readers and users for future use.

Udaipur Dr. Dharm Singh

Dr. Naveen Choudhary


FORWARD

Networks are nearly as prolific today as PCs were 10 to 15 years ago. Businesses of
every size, service, and marketplace have come to embrace networking as a means of sharing
resources, disseminating information, advertising, conducting commerce, and extending their
communities of interest through collaboration and joint venture. Elementary and secondary
education systems are as networked today as many colleges and universities were as recently as
five years ago.
Several factors contribute to the apparent ubiquity of networks. Personal computers are
now business appliances and are fast becoming home appliances. The hardware costs of local
area network interfaces and "hubbed" network connections is a small fraction of what it was only
five years ago. And the skill and software required to install and operate a small- to modest-size
PC LAN is readily acquired. The design of such networks is, in most cases, straightforward. The
computer industry is one of the fastest growing segments of our economy and that growth
promises to continue well into the next century. A great way to boost knowledge and
understanding of computing careers is by intensive practical training in various domains of
computing
I appreciate the effort made by Dr. Dharm Singh and Sh. Naveen Choudhary, Department
of Computer Science & Engineering of this college in bringing out this practical manual of “Data
Communication and Networks” for use of Computer Science & Engineering students.

I am sure that this manual will serve as a guide book to develop the practical aspects
more effectively among the Computer Science & Engineering students.

Dr. Virendra Kumar


Dean, CTAE
Udaipur
Dr. Dharm Singh, Dr. Naveen Choudhary

*/Exercise 1)*/
/*Write a Program for Matrix Addition*/

#include<stdio.h>
#include<conio.h>
/* Addition of two matrixes */
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,rowa,rowb,cola,colb;
clrscr();
printf("enter the number of rows for matrix a\n");
scanf("%d",&rowa);
printf("enter the number of columns for matrix a\n");
scanf("%d",&cola);
printf("enter the elements of matrix a\n");
for(i=0;i<rowa;i++)
{
for(j=0; j<cola;j++)
{
scanf("%d", &a[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("enter the number of rows for matrix b\n");
scanf("%d",&rowb);
printf("enter the number of columns for matrix b\n");
scanf("%d",&colb);
printf("enter the number of elements for matrix b\n");
for(i=0;i<rowb;i++)
{
for(j=0;j<colb;j++)
{
scanf("%d", &b[i][j]);
}

1
Manual for Neural Networks

printf("\n");
}
printf("\n");
printf(" Addition of two matrixes c\n");

for(i=0;i<rowa;i++)
{
for(j=0;j<cola;j++)
{
c[i][j]=a[i][j]+b[i][j]; printf("%3d",c[i][j]);
}
printf("\n");
}
getch();
}

2
Dr. Dharm Singh, Dr. Naveen Choudhary

*/Exercise 2)*/
/*Write a Program for Matrix Multiplication*/

#include<stdio.h>
#include<conio.h>
/* multiplication of two matrices*/
void main()
{
float x[10][10],y[10][10],z[10][10];
int i,j,p,q,k,rowx,rowy,colx,coly;
clrscr();
printf("enter the number of rows for matrix X\n");
scanf("%d",&rowx);
printf("enter the number of columns for matrix X\n");
scanf("%d",&colx);
printf("enter the elements of matrix X");
for(i=0;i<rowx;i++)
{
for(k=0;k<colx;k++)
{
scanf("%f",&x[i][k]);
printf("%7.2f",x[i][k]);
}
printf("\n");
}
printf("\n\n");
printf("enter the number rows for matrix Y\n");
scanf("%d",&rowy);
printf("enter the number of columns for matrix Y\n");
scanf("%d",&coly);
printf("enter numberof elements for matrix Y");
for(k=0;k<rowy;k++)
{
for(j=0;j<coly;j++)
scanf("%f",&y[k][j]);
printf("%7.2f",y[k][j]);
}

3
Manual for Neural Networks

printf("\n");
}
printf("\n\n");
if(colx==rowy)
{
for(i=0;i<rowx;i++)
{
j=0; do
{
q = 0; for(k=0;k<rowy;k++)

{
p=x[i][k];
q = q+ p*y[k][j];
}
z[i][j]=q; j++;
}
while(j<3);
}
printf("The elements of product matrix Z is \n");
for(i=0;i<rowx;i++)
{
for(j=0;j<coly;j++)
printf("%7.2f",z[i][j]);
printf("\n");
}
}
else
printf("multiplication is not possible");
getch();
}

4
Dr. Dharm Singh, Dr. Naveen Choudhary

*/Exercise 3)*/
/*Write a Program for Matrix Transpose*/
#include<stdio.h>
#include<conio.h>
/* Transpose Of matrixes */ void
main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("enter 9 numbers");
for(i=0;i<3;i++)
{
for(j=0; j<3;j++)
{
scanf("%d", &a[i][j]);
printf("%3d",a[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("Transpose of a matrix \n");

for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
{
c[i][j]=a[i][j];
printf("%3d",c[i][j]);
}
printf("\n");
}
getch();
}

5
Manual for Neural Networks

*/Exercise 4)*/
/* Write a Program using Perceptron Neural Network to recognise even and odd numbers. Given
numbers are in ASSCI form 0 to 9. */
#include<iostream.h>
#include<conio.h>

void main ()
{
int i,j;
float wt;
float w[7];
float inp[7];
float th,net=0;

clrscr();
cout<<" Decimal value ASSCII value\n\n";
cout<<" 0 0 1 1 0 0 0 0\n";
cout<<" 1 0 1 1 0 0 0 1\n";
cout<<" 2 0 1 1 0 0 1 0\n";
cout<<" 3 0 1 1 0 0 1 1\n";
cout<<" 4 0 1 1 0 1 0 0\n";
cout<<" 5 0 1 1 0 1 0 1\n";
cout<<" 6 0 1 1 0 1 1 0\n";
cout<<" 7 0 1 1 0 1 1 1\n";
cout<<" 8 0 1 1 1 0 0 0\n";
cout<<" 9 0 1 1 1 0 0 1\n\n";

cout<<"Enter the ASCII values of any decimal value to display\n\n";

for(i=0;i<7;i++)
cin>>inp[i];

cout<<"\n\n";

cout<<"Enter the threshold value:";


cin>>th; cout<<"\n\n";

6
Dr. Dharm Singh, Dr. Naveen Choudhary

cout<<"Enter the value of weights:"; cin>>wt;

for(i=0;i<7;i++) w[i]=wt;

for(i=0;i<7;i++)

net=net+inp[i]*w[i]; cout<<"\n\n";
cout<<"Net before adjustment="<<net<<"\n\n";

float x[5][7]= {
{ 0, 1, 1, 0, 0, 0, 0 },
{ 0, 1, 1, 0, 0, 1, 0 },
{ 0, 1, 1, 0, 1, 0, 0 },
{ 0, 1, 1, 0, 1, 1, 0 },
{ 0, 1, 1, 1, 0, 0, 0 }
};

for(i=0;i<5;i++)
{
for(j=0;j<7;j++)
{
if(x[i][j] != inp[j]) break;
}
if(j == 7) break;
}
if(net<th && j==7)
{
net=0; for(i=0;i<7;i++)
{
w[i]=w[i]+inp[i]; net=net+inp[i]*w[i]; if(net >= th);
break;

}
cout<<"\n\n";
cout<<"Net after adjustment="<<net<<"\n\n";
cout<<"THIS NUMBER IS EVEN\n";
}

7
Manual for Neural Networks

else if(net>th && j!=7)


{
net=0; for(i=0;i<7;i++)
{
w[i]=inp[i]-w[i]; net=net+inp[i]*w[i];
if(net<th)
break;

}
cout <<"Net after adjustment="<<net; cout<<"\n\n";
cout<<"THIS NUMBER IS ODD\n";
}
else if(net>th && j==7)
{
cout<<"\n\n";
cout<<"THIS NUMBER IS EVEN\n";
}
else
{
cout<<"\n\n";
cout<<"THIS NUMBER IS ODD\n";
}
getch();
}

8
Dr. Dharm Singh, Dr. Naveen Choudhary

*/Exercise5)*/
/* Write a Program for pattern reorganization. */ /*P_DIS3)*/
#include<stdio.h>
#include<conio.h>
void main()
{
int ip[9],y[9],x,i,th=1,count=0; float w[9],m=0.0,net=0.0;
clrscr();
printf("Enter the digit to display\n");
scanf("%d",&x);
//printf("enter the referance vector\n"); //
scanf("%d",&y[i]);
printf("Enter any input pattern\n");
for(i=0;i<9;i++)
scanf("%d",&ip[i]);
printf("Enter weight vector\n");
for(i=0;i<9;i++)
{
scanf("%f",&w[i]);
printf("%f ",w[i]);
}
for(i=0;i<9;i++)
{
m = ip[i] * w[i]; net = net + m;
}
printf("The value of net before adjustment= %f\n",net);
printf("Enter referance pattern\n");
for(i=0;i<9;i++)
scanf("%d",&y[i]);
for(i=0;i<9;i++)
{
if(ip[i]==y[i])
count++; else
break ;

9
Manual for Neural Networks

// printf("%d \n",count); if(count==9 && net<th)


{
do
{
m=0.0; net=0.0; for(i=0;i<9;i++)
{
w[i]=w[i]+ip[i]; m =ip[i] * w[i]; net= net + m;
}
}
while(net<th);
printf("The value of net after adj=%f\n",net);
printf("The number is =%d\n",x);
}
else if (count==9 && net>=th)
{
printf("The value of net =%f\n",net);
printf("The number is =%d\n",x);
}
// else
// printf ("the number is=%d",x);

else if(count<9 && net>=th)


{
do
{
m=0.0; net=0.0;
for(i=0;i<9;i++)
{
w[i]=w[i]-ip[i]; m
=ip[i] * w[i]; net=
net + m;
}
}
while(net>=th);
printf("The value of net after adj=%f\n",net);
printf("The number is not equal to %x",x);

10
Dr. Dharm Singh, Dr. Naveen Choudhary

}
else
printf("The number is not equal to %x",x);
// else if(count<9 && net<th)
// {
// printf("the value of net =%f",net);
// printf("the number is not =%d",x);
// }
getch();
}

11
Manual for Neural Networks

*/Exercise6)*/
/*Write a Program for Bidirectional Associative Memory with two pairs of vectors.*/
#include<iostream.h>
#include<conio.h>
int i,j,k;
void main()
{
int aw[4][3],bw[4][3],ab[4][3],abcd[3][4];
int X1[1][4],X2[1][4],Y1[1][3],Y2[1][3],X[1][4];
int AA1[1][4],AA2[1][4],BB1[1][3],BB2[1][3];
int A11[4][1],A22[4][1],th=0,m[1][3],n[1][4],Y[1][3];
void mat1(int c[1][4]);
void mat2(int c[1][3]);
void bipo1(int bb[1][4],int b[1][4]);
void bipo2(int bb[1][3],int b[1][3]);
void transpose(int att[4][1],int at[1][4]);
void disp1(int d[1][3]);
void disp2(int d[1][4]);
void disp3(int d[4][1]);
void mul(int xx[4][3],int xt[4][1],int x[1][3]);
void mult1(int e[1][3],int f[1][4],int g[4][3]);
void mult2(int e[1][4],int f[1][3],int g[3][4]);
void display1(int yy[4][3]);
void display2(int yy[3][4]);
void add(int pp[4][3],int qq[4][3],int rr[4][3]);
clrscr();
cout<<"Enter original vector X1:\n";
mat1(X1);
cout<<"Enter original vector X2:\n";
mat1(X2);
cout<<"Enter associated vector Y1:\n";

mat2(Y1);
cout<<"Enter associated vector Y2:\n";
mat2(Y2);
cout<<"Enter associated vector B2:\n";

12
Dr. Dharm Singh, Dr. Naveen Choudhary

// mat(B2);
// cout<<"Enter associated vector B3:\n";
// mat(B3); clrscr();

// cout<<"Input matrix a[1][3]\n\n";


// disp(a);

bipo1(AA1,X1);
bipo1(AA2,X2);
// bipo(AA3,A3);
// bipo(BB1,B1);
// bipo2(BB1,Y1);
// bipo2(BB2,Y2);

// cout<<"Input vector A[1][3] matrix after bipolarization\n\n";


// disp(AA1);

transpose(A11,AA1);
transpose(A22,AA2);
// transpose(A33,AA3);

// cout<<"Transpose of AA1\n";
// disp3(A11);

mul(aw,A11,BB1);
mul(bw,A22,BB2);
// mul(cw,A33,BB3);
// display1(aw);
// display1(bw);
add(ab,aw,bw);
// add(abc,ab,cw);

cout<<"The weight matrix is:\n";


display1(ab);

13
Manual for Neural Networks

cout<<"Enter any original vector\n";


mat1(X);
cout<<"Output NET vector for original vector\n";
mult1(m,X,ab);
disp1(m);
for(i=0;i<3;i++)
{
if(m[0][i]>=th)
m[0][i]=1;
else
m[0][i]=0;
}
cout<<"Output PATTRN of original vector X that is associated vector Y\n";
disp1(m);
for(i=0;i<3;i++) for(j=0;j<4;j++)
abcd[i][j]=ab[j][i];

cout<<"The transpose of weight matrix is:\n";


display2(abcd);
cout<<"Enter the corresponding associated vector\n"; mat2(Y);
cout<<"Output NET vector for associated vector B1\n";
mult2(n,Y,abcd);
disp2(n);

for(i=0;i<4;i++)
{
if(n[0][i]>=th) n[0][i]=1;
else
n[0][i]=0;
}
cout<<"Output PATTRN of associated vector B that is original vector A\n"; disp2(n);
cout<<"Hence this pair of vectors is heteroassociative";

getch();
}
void mat1(int c[1][4])
{
for(i=0;i<1;i++) for(j=0;j<4;j++)
{

14
Dr. Dharm Singh, Dr. Naveen Choudhary

cout<<"Enter c["<<i<<"]["<<j<<"]\n"; cin>>c[i][j];


}
}
void mat2(int c[1][3])
{
for(i=0;i<1;i++)
for(j=0;j<3;j++)
{
cout<<"Enter c["<<i<<"]["<<j<<"]\n"; cin>>c[i][j];
}
}

void disp1(int d[1][3])


{
for(i=0;i<1;i++) for(j=0;j<3;j++)
cout<<d[i][j]<<"\t";

cout<<"\n\n";
}
void disp2(int d[1][4])
{
for(i=0;i<1;i++) for(j=0;j<4;j++)
cout<<d[i][j]<<"\t";

cout<<"\n\n";
}
void disp3(int d[4][1])
{
for(i=0;i<4;i++) for(j=0;j<1;j++)
cout<<d[i][j]<<"\n";

cout<<"\n\n";
}
void mul(int xx[4][3],int xt[4][1],int x[1][3])
for(i=0;i<4;i++) for(j=0;j<3;j++)
{

15
Manual for Neural Networks

xx[i][j]=0; for(k=0;k<1;k++)
xx[i][j]=xx[i][j]+xt[i][k]*x[k][j];
}
}
void display1(int yy[4][3])
{
for(i=0;i<4;i++)
{
for(j=0;j<3;j++) cout<<yy[i][j]<<"\t";

cout<<"\n";
}
}
void display2(int yy[3][4])
{
for(i=0;i<3;i++)
{
for(j=0;j<4;j++) cout<<yy[i][j]<<"\t";

cout<<"\n";
}
}

void add(int pp[4][3],int qq[4][3],int rr[4][3])


{
for(i=0;i<4;i++) for(j=0;j<3;j++)
pp[i][j]=qq[i][j]+rr[i][j];
}

void mult1(int e[1][3],int f[1][4],int g[4][3])


{
for(i=0;i<1;i++)
for(j=0;j<3;j++)
{
e[i][j]=0;

16
Dr. Dharm Singh, Dr. Naveen Choudhary

for(k=0;k<4;k++) e[i][j]=e[i][j]+f[i][k]*g[k][j];

}
}

void mult2(int e[1][4],int f[1][3],int g[3][4])


{
for(i=0;i<1;i++)
for(j=0;j<4;j++)
{
e[i][j]=0;

for(k=0;k<3;k++)

e[i][j]=e[i][j]+f[i][k]*g[k][j];

}
}

void bipo1(int bb[1][4],int b[1][4])


{
for(j=0;j<4;j++)
{
if(b[0][j]==0) bb[0][j]=-1;
else
bb[0][j]=1;
}
}

void bipo2(int bb[1][3],int b[1][3])


{
for(j=0;j<3;j++)
{
if(b[0][j]==0) bb[0][j]=-1;
else
bb[0][j]=1;
}
}

17
Manual for Neural Networks

void transpose(int att[4][1],int at[1][4])


{
for(i=0;i<4;i++) att[i][0]=at[0][i];
}

18
Dr. Dharm Singh, Dr. Naveen Choudhary

*/Exercise7)*/
/*Write a Program for Implementation of Fuzzy Associative Memory.*/
#include <iostream.h>
#include<conio.h>
void main()
{
float in[10],b[10],c[10][10],w[10][10],b1[10]; float in1[10],wt[10][10],in2[10];
int i,j,k, row,colm,p,q;
clrscr();
cout<<"Enter the no of rows\n";
cin>>row;
cout<<"Enter the original fit vector\n";
for (i=0;i<row;i++)
cin >> in[i] ;
// for (i=0;i<row;i++)
// cout<< "\t"<< in[i]<<"\n";

// in[0] =0.3;
// in[1] =0.7;
// in[2] =0.4;
// in[3] =0.2;
cout<<"Enter the no of columns\n";
cin>>colm;
cout<<"Enter the associated fit vector\n";
for(i=0;i<colm;i++)
cin>>b[i];
// b[0] =0.4 ;
// b[1] =0.3;
// b[2] =0.9;

// cout<<in[0]<<"\t"<<in[1]<<"\t"<<in[2]<<"\t"<<in[3];
// cout<<"\n";
// cout<<b[0]<<"\t"<<b[1]<<"\t"<<b[2]<<"\n\n"; cout<<" The weight

matrix.\n"; for(i=0;i<row;i++)

19
Manual for Neural Networks

{
for(j=0;j<colm;j++)
{
if(in[i]<=b[j]) w[i][j]= in[i];
else
w[i][j]=b[j]; cout<<"\t"<<w[i][j];
}
cout<<"\n";
}

// cout<<"\n\n";

cout<<" Transpose of weight matrix.\n";

for(i=0;i<row;i++) for(j=0;j<row;j++)
wt[j][i]=w[i][j];

// cout<<"\n\n";
for(i=0;i<colm;i++)
{
for(j=0;j<row;j++)
cout<<"\t"<<wt[i][j];
cout<<"\n";
}
cout<<"\n";

for(i=0;i<colm;i++)
{
for(j=0;j<row;j++)
{
if(in[j] > w[j][i])
c[i][j] = w[j][i];
else
c[i][j]=in[j];
// cout<<"\t"<<c[i][j];
}

20
Dr. Dharm Singh, Dr. Naveen Choudhary

// cout<<"\n";
}

// cout<<"\n\n";
/* for (i=0;i<3;i++)
{
for (j=0;j<4;j++)
cout<<"\t"<<c[i][j];

cout<<"\n\n";
}*/

cout<<"The output vector\n";

for(j=0;j<colm;j++)
{
for(i=0;i<colm;i++)
{
if(c[j][i] > c[j][i+1]) c[j][i+1] = c[j][i];
}
in1[j]=c[j][i];

cout<<"\t"<<in1[j];
}

cout<<"\n";

for(i=0;i<row;i++)
{
for(j=0;j<colm;j++)
{
if(in1[j]<=wt[j][i])
c[i][j]= in1[j];
else
c[i][j] = wt[j][i];

21
Manual for Neural Networks

// cout<<"\t"<<c[i][j] ;
}
// cout<<"\n";
}

// cout<<"\n\n";
for(i=0;i<3;i++)
if(b[i] != in1[1])
break;

if(i<2)
cout<<" Output vector is a subset of input vector.\n" ;
else
cout<<" Output vector is same as associated vector.\n";
cout<<"\n";

cout<<" Output vector is applied through the transpose of


weight matrix.\n";
cout<<"\n";
cout<<" The corresponding output vector.\n";
for(j=0;j<row;j++)
{
for(i=0;i<colm-1;i++)
{
if(c[j][i] > c[j][i+1])
c[j][i+1] = c[j][i];
}
in2[j]=c[j][i];

cout<<"\t"<<in2[j];
}
cout<<"\n";

for(i=0;i<4;i++)
if(in[i] != in2[i])

22
Dr. Dharm Singh, Dr. Naveen Choudhary

break;
if(i<3)
cout<<"\n Hence the fit vectors are not heteroassociative.\n"; else

cout<< "\n Hence the fit vectors are heteroassociative.\n";


getch();

23
Manual for Neural Networks

*/Exercise8)*/
/*Write a Program for Hopfield Neural Network*/
/* HoP_Asy */

#include<iostream.h>
#include<conio.h>
int i,j,k;

void main()
{

inta[1][4],aa[1][4],am[1][4],b[1][4],bm[1][4],at[4][1],bt[4][1],c[1][4],aw[4][4],bw[4][4],ab[4][4],th=0
, m[1][4],n[1][4];
void mat(intc[1][4]);
void disp(intd[1][4]);
void mul(int xx[4][4],int xt[4][1],int
x[1][4]);
void mult(int e[1][4],int f[1][4],int
g[4][4]);
void display(int yy[4][4]);
void add(int pp[4][4],int qq[4][4],int
rr[4][4]);
clrscr();
mat(a);
mat(b);
// cout<<"Input matrix a[1][3]\n\n";
// disp(a);
for(j=0;j<4;j++)
{
if(a[0][j]==0)
am[0][j]==-1;
else
am[0][j]
=1;
}
cout<<"Input a[1][4] matrix after

24
Dr. Dharm Singh, Dr. Naveen Choudhary

bipolarization\n\n";
disp(am);
// cout<<"Input matrix b[1][3]\n\n";
disp(b);
for(j=0;j<4;j++)
{
if(b[0][j]==0)
bm[0][j]=-1;
else bm[0][j]=1;
}
cout<<"Input b[1][4] matrix after bipolarization\n\n";
disp(bm);
cout<<"Transpose at[4][1] matrix\n\n";
for(i=0;i<4;i++)
{
at[i][0]=am[0][i];
cout<<"\t"<<at[i][0]<<"\n";
}
cout<<"Transpose bt[4][1] matrix\n\n";
for(i=0;i<4;i++)
{
bt[i][0]=bm[0][i];
cout<<"\t"<<bt[i][0]<<"\n";
}
mul(aw,at,am);
// cout<<"Weight matrix of input A\n";
// display(aw); mul(bw,bt,bm);
// cout<<"Weight matrix of input B\n";
// display(bw); */ for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(i==j) aw[i][j]=bw[i][j]=0;

}
cout<<"Combination of wt. matrix A & B\n";
add(ab,aw,bw);
display(ab);

25
Manual for Neural Networks

cout<<"Output NET vector for A\n";


mult(m,a,ab);
/* for(i=0;i<1;i++)
for(j=0;j<4;j++)
{

m[i][j]=0;
for(k=0;k<4;k++)
m[i][j]=m[i][j]+a[i][k]*ab[k][j];
} */
disp(m);
for(i=0;i<4;i++)
{
if(m[0][i]>=th)
m[0][i]=1;
else
m[0][i]=0;
}
cout<<"Output PATTRN for A\n";
disp(m);
cout<<"Output NET vector for B\n";
mult(n,b,ab);
disp(n);
for(i=0;i<4;i++)
{
if(n[0][i]>=th)
n[0][i]=1;
else
n[0][i]=0;
}
cout<<"Output PATTRN for B\n";
disp(n);
cout<<"Enter new matrix\n";
mat(c);
for(i=0;i<1;i++)
for(j=0;j<4;j++)

26
Dr. Dharm Singh, Dr. Naveen Choudhary

{
aa[i][j]=0;
for(k=0;k<4;k++)
aa[i][j]=aa[i][j]+c[i][k]*ab[k][j];

if(aa[i][j]>=th)
c[i][j]=1;
else
c[i][j]=0;
}
cout<<"Asynchronous output vector C.\n";
disp(c);

// for(i=0;i<1;i++)
// for(j=0;j<4;j++)
if(a[0][0]==c[0][0])
{
cout<<"output is A matrix\n";
// break;
disp(c);
}
else
{
cout<<"Output is B matrix\n";
// break;
disp(c);
}
getch();
}

void mat(int c[1][4])


{
for(i=0;i<1;i++)
for(j=0;j<4;j++)
{
cout<<"Enter c["<<i<<"]["<<j<<"]\n";

27
Manual for Neural Networks

cin>>c[i][j];
}
}

void disp(int d[1][4])


{
for(i=0;i<1;i++)
for(j=0;j<4;j++)
{
// cout<<"Enter c["<<i<<"]["<<j<<"]";
// cout<<d[i][j]<<"\t";
}
cout<<"\n\n";
}

void mul(int xx[4][4],int xt[4][1],int x[1][4])


{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
xx[i][j]=0;
for(k=0;k<1;k++)
xx[i][j]=xx[i][j]+xt[i][k]*x[k][j];
}
}
void display(int yy[4][4])
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<yy[i][j]<<"\t";
}
cout<<"\n";
}
}

28
Dr. Dharm Singh, Dr. Naveen Choudhary

void add(int pp[4][4],int qq[4][4],int rr[4][4])


{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
// pp[i][j]=0; pp[i][j]=qq[i][j]+rr[i][j];
}
void mult(int e[1][4],int f[1][4],int g[4][4])
{
for(i=0;i<1;i++)
for(j=0;j<4;j++)
{
e[i][j]=0;

for(k=0;k<4;k++)

e[i][j]=e[i][j]+f[i][k]*g[k][j];

}
}

29
Manual for Neural Networks

*/Exercise9)*/
/*Write a Program for Hopfield Neural Network*/
/* HoP_Asy */
HoP_PAT

#include<iostream.h>
#include<conio.h>
int i,j,k;

void main()
{
Intaa[1][9],am[1][9],bm[1][9],at[9][1],bt[9][1],c[1][9],aw[9][9],bw[9][9],ab[9][9],th=0,m[1][9],n[1][9
]
;
void mat(int c[1][9]);
void disp(int d[1][9]);
void mul(int xx[9][9],int xt[9][1],int x[1][9]);
void mult(int e[1][9],int f[1][9],int g[9][9]);
void display(int yy[9][9]);
void add(int pp[9][9],int qq[9][9],int rr[9][9]);
void sign(int ss[9][9]);
clrscr();
int a[1][9]={0,1,0,1,1,1,0,1,0};

int b[1][9]={0,0,0,1,1,1,0,0,0};
// mat(a);
// mat(b);
// cout<<"Input matrix a[1][3]\n\n";
// disp(a);

for(j=0;j<9;j++)
{
if(a[0][j]==0)
am[0][j]=-1;
else
am[0][j]=1;

30
Dr. Dharm Singh, Dr. Naveen Choudhary

}
// cout<<"Input a[1][9] matrix after bipolarization\n\n";
// disp(am);
// cout<<"Input matrix b[1][3]\n\n";
// disp(b); for(j=0;j<9;j++)
{
if(b[0][j]==0) bm[0][j]=-1;
else bm[0][j]=1;
}
// cout<<"Input b[1][9] matrix after bipolarization\n\n";
// disp(bm);
// cout<<"Transpose at[4][1] matrix\n\n";
for(i=0;i<9;i++)
{
at[i][0]=am[0][i];
// cout<<"\t"<<at[i][0]<<"\n";
}
// cout<<"Transpose bt[9][1] matrix\n\n";
// for(i=0;i<9;i++)
{
bt[i][0]=bm[0][i];
// cout<<"\t"<<bt[i][0]<<"\n";
}
mul(aw,at,am);
// cout<<"Weight matrix of input A\n";
// display(aw); mul(bw,bt,bm);
// cout<<"Weight matrix of input B\n";
display(bw);
for(i=0;i<9;i++)
for(j=0;j<9;j++)
{
if(i==j) aw[i][j]=bw[i][j]=0;

// cout<<"Combination of wt. matrix A & B\n";

31
Manual for Neural Networks

// add(ab,aw,bw);
// display(ab);
// cout<<"Output NET vector for A\n";
mult(m,a,ab);
/* for(i=0;i<1;i++)
for(j=0;j<4;j++)
{

m[i][j]=0;
for(k=0;k<4;k++)
m[i][j]=m[i][j]+a[i][k]*ab[k][j];
} */
// disp(m); for(i=0;i<9;i++)
{
if(m[0][i]>th) m[0][i]=1;
else
m[0][i]=0;
}
// cout<<"Output PATTRN for A\n";
// disp(m);
// cout<<"Output NET vector for B\n"; mult(n,b,ab);
disp(n);
for(i=0;i<9;i++)
{
if(n[0][i]>th) n[0][i]=1;
else
n[0][i]=0;
}
// cout<<"Output PATTRN for B\n";
// disp(n);
cout<<"Enter new matrix\n"; mat(c);
clrscr();
cout<<"New matrix is:\n\n"; disp(c);
for(i=0;i<1;i++)
for(j=0;j<9;j++)
{

32
Dr. Dharm Singh, Dr. Naveen Choudhary

aa[i][j]=0;
for(k=0;k<9;k++)
aa[i][j]=aa[i][j]+c[i][k]*ab[k][j];

if(aa[i][j]>th)
c[i][j]=1;
else
c[i][j]=0;
}
cout<<"Asynchronous output vector C.\n\n";
disp(c);
cout<<"\n\n";
// for(i=0;i<1;i++)
// for(j=0;j<4;j++)
if(a[0][1]==c[0][1])
{
cout<<"Output matrix is positive(+) sign \n"; sign(c);
}
else
{
cout<<"Output matrix is negetive(-) sign\n"; sign(c);
}
getch();
}

void sign(int d[1][9])


{
// for(i=0;i<3;i++)
// {
for(j=0;j<3;j++)
// {
// cout<<"Enter c["<<i<<"]["<<j<<"]";
// if(d[0][j]==1)
cout<<"*"; else cout<<"
"; cout<<"\n";

33
Manual for Neural Networks

for(j=3;j<6;j++)
if(d[0][j]==1) cout<<"*";
else cout<<" ";
cout<<"\n";
for(j=6;j<9;j++) if(d[0][j]==1)
cout<<"*";
else cout<<" ";
cout<<"\n";
// }
cout<<"\n\n";
}

void mat(int c[1][9])


{
for(i=0;i<1;i++)
for(j=0;j<9;j++)
{
cout<<"Enter c["<<i<<"]["<<j<<"]\n";
cin>>c[i][j];
}
}

void disp(int d[1][9])


{
for(i=0;i<1;i++)
for(j=0;j<9;j++)
{
// cout<<"Enter c["<<i<<"]["<<j<<"]";
cout<<d[i][j]<<"\t";
}
cout<<"\n\n";
}

void mul(int xx[9][9],int xt[9][1],int x[1][9])


{
for(i=0;i<9;i++)

34
Dr. Dharm Singh, Dr. Naveen Choudhary

for(j=0;j<9;j++)
{
xx[i][j]=0;
for(k=0;k<1;k++)
xx[i][j]=xx[i][j]+xt[i][k]*x[k][j];
}
}

void display(int yy[9][9])


{
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
cout<<yy[i][j]<<"\t";
}
cout<<"\n";
}
}

void add(int pp[9][9],int qq[9][9],int rr[9][9])


{
for(i=0;i<9;i++)
for(j=0;j<9;j++)
// pp[i][j]=0; pp[i][j]=qq[i][j]+rr[i][j];
}

void mult(int e[1][9],int f[1][9],int g[9][9])


{
for(i=0;i<1;i++)
for(j=0;j<9;j++)
{
e[i][j]=0;

35
Manual for Neural Networks

for(k=0;k<9;k++)

e[i][j]=e[i][j]+f[i][k]*g[k][j];

}
}

36
Dr. Dharm Singh, Dr. Naveen Choudhary

/*Exercise 10*/
/*Write a Program to Match the Input Pattern*/
// m = ip[i] * w[i]
// net = net + m;

/*P_DISP3*/

#include<stdio.h>
#include<conio.h>
void main()
{
int ip[9],y[9],x,i,th=1,count=0;
float w[9],m=0.0,net=0.0; clrscr();
printf("Enter the digit to display\n");
scanf("%d",&x);
//printf("enter the reference vector\n"); //
scanf("%d",&y[i]);
printf("Enter any input pattern\n");
for(i=0;i<9;i++)
scanf("%d",&ip[i]);
printf("Enter weight vector\n");
for(i=0;i<9;i++)
{
scanf("%f",&w[i]);
printf("%f ",w[i]);
}
for(i=0;i<9;i++)
{
m = ip[i] * w[i];
net = net + m;
}
printf("The value of net before adjustment= %f\n",net);
printf("Enter referance pattern\n");
for(i=0;i<9;i++)
scanf("%d",&y[i]);
for(i=0;i<9;i++)

37
Manual for Neural Networks

{
if(ip[i]==y[i]) count++;
else break ;

}
// printf("%d \n",count);
if(count==9 && net<th)
{
do
{
m=0.0; net=0.0;
or(i=0;i<9;i++)
{
w[i]=w[i]+ip[i]; m =ip[i] * w[i];
net= net + m;
}
}
while(net<th);
printf("The value of net after adj=%f\n",net);
printf("The number is =%d\n",x);
}
else if (count==9 && net>=th)
{
printf("The value of net =%f\n",net);
printf("The number is =%d\n",x);
}
// else
printf ("the number is=%d",x);
else if(count<9 && net>=th)
{
do
{
m=0.0; net=0.0; for(i=0;i<9;i++)
{
w[i]=w[i]-ip[i]; m =ip[i] * w[i];
net= net + m;

38
Dr. Dharm Singh, Dr. Naveen Choudhary

}
}
while(net>=th);
printf("The value of net after adj=%f\n",net);
printf("The number is not equal to %x",x);
}
else
printf("The number is not equal to %x",x);
// else if(count<9 && net<th)
// {
// printf("the value of net =%f",net);
// printf("the number is not =%d",x);
} getch();
}

39

You might also like