-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAM.java
63 lines (56 loc) · 2.23 KB
/
AM.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* Associative memory class
With locality sensitive hashing (LSH) only a few bits change with a small change in input.
Here in each dimension some output bits of a LSH (-1,1 bipolar) are weighted and summed.
For a particular input the weights are easily (and equally) adjusted to give a required output.
For a previously stored value retrived again the newly stored adjustment will tend to cancel
out to zero (with a Gaussian noise residue), assuming the LHS bit response to the two inputs
is quite different bit-wise.
*/
package s6regen;
import java.io.Serializable;
import java.util.Arrays;
public final class AM implements Serializable {
private final int vecLen;
private final int density;
private final long hash;
private final float[][] weights;
private final float[][] bipolar;
private final float[] workA;
private final float[] workB;
// vecLen must be 2,4,8,16,32.....
// density is the maximum number of vector pairs that can be associated with
// repeated training.
public AM(int vecLen, int density) {
this.vecLen = vecLen;
this.density = density;
hash = System.nanoTime();
weights = new float[density][vecLen];
bipolar = new float[density][vecLen];
workA = new float[vecLen];
workB = new float[vecLen];
}
public void recallVec(float[] resultVec, float[] inVec) {
System.arraycopy(inVec, 0, workA, 0, vecLen);
Arrays.fill(resultVec, 0f);
for (int i = 0; i < density; i++) {
WHT.fastRP(workA, hash + i);
WHT.signOf(bipolar[i], workA);
VecOps.multiplyAddTo(resultVec, weights[i], bipolar[i]);
}
}
public void trainVec(float[] targetVec, float[] inVec) {
float rate = 1f / density;
recallVec(workB, inVec);
for (int i = 0; i < vecLen; i++) {
workB[i] = (targetVec[i] - workB[i]) * rate; //get the error term in workB
}
for (int i = 0; i < density; i++) { // correct the weights
VecOps.multiplyAddTo(weights[i], workB, bipolar[i]); // to give the required output
}
}
public void reset() {
for (float[] x : weights) {
Arrays.fill(x, 0f);
}
}
}