C program of Hopfield Networks : Hopfield network is a special kind of recurrent neural networks that can be used as associative memory. Associative memory is a memory that is addressed through its contents. For this reason it is also known as content addressable memory. That is, if a pattern is presented to an associative memory, then it checks whether the given pattern coincides with a stored pattern and gives the result. The coincidence need not be perfect, though. An associative memory may also return a stored pattern that is similar to the presented one, so that noisy input can also be recognized.
The Hopfield network model consists of a set of neurons and a corresponding set of unit delays, forming a multiple-loop feedback system.
- They consist of single layer of fully connected neurons.
- This is a recurrent neural network i.e. the output of a neuron is feedback to the input of other neurons but not to itself.
- It serves as a content addressable memory system. A content addressable memory finds its application in very high speed searching applications.
Let us see how to write a C program of Hopfield Networks with a single layer of fully interconnected neurons and the network should recall the 1010 and 0101 patterns correctly.
C program of Hopfield Networks :
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
class neuron
{
protected:
int activation;
friend class network;
public:
int weightv[4];
neuron() {};
neuron(int *j) ;
int act(int, int*);
};
class network
{
public:
neuron nrn[4];
int output[4];
int threshld(int) ;
void activation(int j[4]);
network(int*,int*,int*,int*);
};
neuron::neuron(int *j)
{
int i;
for(i=0;i<4;i++)
{
weightv[i]= *(j+i);
}
}
int neuron::act(int m, int *x)
{
int i;
int a=0;
for(i=0;i<m;i++)
{
a += x[i]*weightv[i];
}
return a;
}
int network::threshld(int k)
{
if(k>=0)
return (1);
else
return (0);
}
network::network(int a[4],int b[4],int c[4],int d[4])
{
nrn[0] = neuron(a) ;
nrn[1] = neuron(b) ;
nrn[2] = neuron(c) ;
nrn[3] = neuron(d) ;
}
void network::activation(int *patrn)
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("\nNeuron[%d].Weightv[%d] is: %d ",i,j,nrn[i].weightv[j]);
}
nrn[i].activation = nrn[i].act(4,patrn);
printf("\nActivation is : %d",nrn[i].activation);
output[i]=threshld(nrn[i].activation);
printf("\nOutput value is : %d \n ",output[i]);
getch();
}
}
int main ()
{
int patrn1[]= {1,0,1,0},i;
int wt1[]= {0,-2,2,-2};
int wt2[]= {-2,0,-2,2};
int wt3[]= {2,-2,0,-2};
int wt4[]= {-2,2,-2,0};
printf("-----------------------------------------------------------\n");
printf("----------------------Made by C code champ-----------------\n");
printf("-----------------------------------------------------------\n\n");
printf("\t\tHOPFILED NETWORK ALGORITHM\n\n\n\n");
/*create the network by calling its constructor.
the constructor calls neuron constructor as many times as the number of
neurons in the network.*/
network h1(wt1,wt2,wt3,wt4);
printf("\n\n\tPattern for 1010 :\n\n");
/*Present a pattern to the network and get the activations of the neurons*/
h1.activation(patrn1);
/*check if the pattern given is correctly recalled and give message*/
for(i=0;i<4;i++)
{
if (h1.output[i] == patrn1[i])
printf("\nPattern = %d output = %d component matches",patrn1[i],h1.output[i]);
else
printf("\nPattern= %d output = %d discrepancy occurred",patrn1[i],h1.output[i]);
}
getch();
printf("\n\n\n");
int patrn2[]= {0,1,0,1};
printf("\n\n\tPattern for 0101 :\n\n");
h1.activation(patrn2);
for(i=0;i<4;i++)
{
if (h1.output[i] == patrn2[i])
printf("\nPattern = %d output = %d component matches",patrn2[i],h1.output[i]);
else
printf("\nPattern= %d output = %d discrepancy occurred",patrn2[i],h1.output[i]);
}
getch();
}
Output of Program : Note : This Program is just a sample for recalling 1010 and 0101 patterns correctly of fully interconnected neurons.
———————————————————–
———————-Made by C code champ—————–
———————————————————–
HOPFILED NETWORK ALGORITHM
Pattern for 1010 :
Neuron[0].Weightv[0] is: 0
Neuron[0].Weightv[1] is: -2
Neuron[0].Weightv[2] is: 2
Neuron[0].Weightv[3] is: -2
Activation is : 2
Output value is : 1
Neuron[1].Weightv[0] is: -2
Neuron[1].Weightv[1] is: 0
Neuron[1].Weightv[2] is: -2
Neuron[1].Weightv[3] is: 2
Activation is : -4
Output value is : 0
Neuron[2].Weightv[0] is: 2
Neuron[2].Weightv[1] is: -2
Neuron[2].Weightv[2] is: 0
Neuron[2].Weightv[3] is: -2
Activation is : 2
Output value is : 1
Neuron[3].Weightv[0] is: -2
Neuron[3].Weightv[1] is: 2
Neuron[3].Weightv[2] is: -2
Neuron[3].Weightv[3] is: 0
Activation is : -4
Output value is : 0
Pattern = 1 output = 1 component matches
Pattern = 0 output = 0 component matches
Pattern = 1 output = 1 component matches
Pattern = 0 output = 0 component matches
Pattern for 0101 :
Neuron[0].Weightv[0] is: 0
Neuron[0].Weightv[1] is: -2
Neuron[0].Weightv[2] is: 2
Neuron[0].Weightv[3] is: -2
Activation is : -4
Output value is : 0
Neuron[1].Weightv[0] is: -2
Neuron[1].Weightv[1] is: 0
Neuron[1].Weightv[2] is: -2
Neuron[1].Weightv[3] is: 2
Activation is : 2
Output value is : 1
Neuron[2].Weightv[0] is: 2
Neuron[2].Weightv[1] is: -2
Neuron[2].Weightv[2] is: 0
Neuron[2].Weightv[3] is: -2
Activation is : -4
Output value is : 0
Neuron[3].Weightv[0] is: -2
Neuron[3].Weightv[1] is: 2
Neuron[3].Weightv[2] is: -2
Neuron[3].Weightv[3] is: 0
Activation is : 2
Output value is : 1
Pattern = 0 output = 0 component matches
Pattern = 1 output = 1 component matches
Pattern = 0 output = 0 component matches
Pattern = 1 output = 1 component matches
We hope you all have enjoyed the C program of Hopfield Networks. If you have any queries ask us in form of comments.


I am continuously invstigating online for articles that can facilitate me. Thx!
I was unable to write a program for a neural network model. can any body write this in a C code. please its urgent
I’m sick to death of reading Blogs with reduced quality content material and I’m so glad that I found your write-up these days. It’s undoubtedly cleared lots of things up for me
please send me the c code for multilayer perceptron.
what will be the output of the hopfield network.? please show it.
Output of program is now included into the post itself. This program is for patten recognition using Hopfield Network. This program recalls 1010 and 0101 patterns by using Hopfield Network of fully interconnected neurons.
This is the exact C program of Hopfield Networks journal for anyone who wants to move out out around this substance. You observe so some its almost wearying to contend with you (not that I real would want…HaHa). You definitely put a new twirl on a message thats been handwritten virtually for period. City squeeze, but high!