Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hopfield Neural Network doesn't recognize

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.

like image 926
user1020946 Avatar asked Feb 13 '12 00:02

user1020946


1 Answers

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];
        ...
    }
}
like image 186
kristianp Avatar answered Nov 02 '22 12:11

kristianp