I'm trying to write Hopfield neural network class in Java, but network don't want to recognize patterns. And I can't understand where is the mistake. Network represents with the interconnection matrix w[n][n]. When network is taught with some standard pattern I change the interconnection matrix with 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 standard pattern in some similar pattern. The process should be stopped when 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;
}
Recognition process stops only when threshold is passed. And out pattern doesn't look like standard pattern. Please help to find the mistake. Thank you in advance.
P.S. The problem is solved. Main mistake was that I forgot to set to zero 'net' variable in the start of cycle:
private int[] recognition(int[] pattern){
int net=0, s, j=0;
...
do{
net=0;
for(int i=0; i<n; i++)
net+=pattern[i]*w[i][r];
...
}
}
Thanks for the attention.
Main mistake was that I forgot to set to zero 'net' variable in the start of cycle:
private int[] recognition(int[] pattern){
int net=0, s, j=0;
...
do{
net=0;
for(int i=0; i<n; i++)
net+=pattern[i]*w[i][r];
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With