I'm trying to write Hopfield neural network class in Java, but the network doesn't want to recognize patterns. And I can't understand where is the mistake. Network represents with the interconnection matrix w[n][n]. When the network is taught with some standard pattern I change the interconnection matrix with the following method:
private void teaching(int[] pattern)
{ //teaching for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
if(i==j) w[i][j]=0;
Else
w[i][j] += pattern[i]*pattern[j];
} }
Then I try to recognize the standard patterns in some similar patterns. The process should be stopped when the state of neurons stop changing or when threshold (65535 iterations) is overcome:
private int[] recognition(int[] pattern) { int net=0, s, j=0; int[] previousState = new int[n]; do{ System.arraycopy(pattern, 0, previousState, 0, n); int r = generateRandom(n); for(int i=0; i<n; i++) net+=pattern[i]*w[i][r]; s = signum(net); pattern[r] = s; j++; if(j>iterThreshold) { System.err.println("Threshold overcome."); return pattern; } }while(!Arrays.equals(pattern, previousState)); return pattern; } signum is an activation function: private static int signum(int x) { //activation function if(x>0) return 1; else return -1; }
The recognition process stops only when the threshold is passed. And out pattern doesn't look like a standard pattern. Please help to find the mistake. Thank you in advance.