0

I am trying to implement a hopfield net to learn patterns.

This is what I current have....

I am current having it create a random number of patterns based on the number of neurons

Next I am trying to make a weight matrix

Should I have a seperate weight matrix for every or is one good enough to apply the hebbs learning rule to? One should be enough since I know based on nodes how many it can recognize based on following formula: N/4 * log N, where N is numofNodes. it is log base 2?

Thanks.

```
#include <iostream>
#include <time.h>
#include <math.h>
using namespace std;
void randomlyCreatePatterns(int numOfNeurons, int numOfPatterns, int* pattern);
void createRandomWeightMatrix(int numOfNeurons, int numOfPatterns, int* weightmatrix, int* pattern);
int main (int argc, char *argv[])
{
cout<<"Hopfield Artificial Neural Network (ANN):"<<endl<<endl;
srand(time(NULL)); // use current time to seed random number generator
int numOfNeurons = 5; // size of each pattern = number of neurons
int* pattern = NULL;
int numOfPatterns = 0;
int* weightmatrix = NULL;
//int columns,rows,k,sum;
// Create random pattern matrix to learn, Each row is a separate pattern to learn (n bits each).
cout<<"Training patterns:"<<endl<<endl;
// max capacity (number of patterns it can learn) of Hopfield network N/4 * log(N)
numOfPatterns = static_cast<int>((numOfNeurons/4)*(log10((double)numOfNeurons)/log10((double)2))); // number of patterns (rows)
cout<<"Number of Patterns being stored is "<<numOfPatterns<<endl;
pattern = new int[numOfPatterns * numOfNeurons];
randomlyCreatePatterns(numOfNeurons, numOfPatterns, pattern);
//Create and Print Out Weight Matrix
weightmatrix = new int[numOfNeurons * numOfNeurons];
createRandomWeightMatrix(numOfNeurons, numOfPatterns, weightmatrix, pattern);
}
void randomlyCreatePatterns(int numOfNeurons, int numOfPatterns, int* pattern){
int rows, columns;
for(rows = 0; rows < numOfPatterns; rows++){ // rows
for(columns = 0; columns < numOfNeurons; columns++){ // columns
pattern[rows * numOfNeurons + columns]=rand()%2;
cout<<pattern[rows * numOfNeurons + columns];
}
cout<<endl;
}
cout<<endl;
}
void createRandomWeightMatrix(int numOfNeurons, int numOfPatterns, int* weightmatrix, int* pattern){
// calculate the weight matrix (symmetric and square)
// w[i,j]=w[j,i] & i!=j (i==j => w=0)
int rows, columns, k;
for(rows = 0; rows <numOfNeurons ; rows++)
for(columns = rows; columns<numOfNeurons; columns++)
if(rows==columns)
weightmatrix[rows*numOfNeurons+columns] = 0;
else
{
//create a weight matrix for each pattern
int ran = rand()%3;
for(k=0;k<numOfPatterns;k++)
weightmatrix[rows*numOfNeurons+columns] = ran;
weightmatrix[columns*numOfNeurons+rows] = 0;
}
// print the weight matrix
cout<<"The weight matrix:"<<endl<<endl;
for(rows = 0; rows < numOfNeurons; rows++)
{
for(columns = 0; columns < numOfNeurons; columns++)
printf("%2d ",weightmatrix[rows*numOfNeurons+columns]);
cout<<endl;
}
cout<<endl;
}
```

*Edited 6 Years Ago by lotrsimp12345*: n/a