forked from FrevoProject/FREVO
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HebbNet.template
89 lines (81 loc) · 2.22 KB
/
HebbNet.template
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
group HebbNet;
getOutputDeclaration(learningrate,stepnumber,inputnodes,outputsize,outputnodes,nodes,biases,weights) ::= <<
float bias[<nodes>]={<biases:{bia|<bia>f}; separator=", ">};
float currentweight[<nodes>][<nodes>]={<weights:{wei|{<wei:{we|<we>f}; separator=", ">}}; separator=", ">};
float netOutput[<outputsize>];
float linearActivate(float x){
if (x \>= 1)
{return 1;}
else {
if (x \<= 0)
{return 0;}
else
{return x;}
}
}
Result getStep(float netInput[], boolean learn, long inputsize){
long i;
long j;
float activation [<nodes>];
for (i=0L; i \< <nodes>L; i=i+1){
activation[i]=0.0f;
}
for (i=0L; i \< inputsize; i=i+1) {
netOutput[i]=netInput[i];
}
for (i=<inputnodes>L; i \< <nodes>L; i=i+1) {
float sumValue=0.0f;
for (j=0L; j \< <nodes>L; j=j+1) {
sumValue=sumValue+currentweight[j][i]*netOutput[j];
}
activation[i]=bias[i]+sumValue;
}
float outputVector [<outputnodes>];
for (i = <inputnodes>L; i \< <nodes>L; i=i+1) {
netOutput[i]=linearActivate(activation[i]);
}
if (learn==true){
float delta[<nodes>][<nodes>];
for (i=<inputnodes>L; i \< <nodes>L; i=i+1) {
for (j=0L; j \< <nodes>L; j=j+1){
delta[j][i]=(float)(<learningrate>*(netOutput[i]-0.5f)*(netOutput[j]-0.5f));
}
}
float sumValue=0.0f;
long n=0L;
for (i=<inputnodes>L; i \< <nodes>L; i=i+1) {
for (j=0L; j \< <nodes>L; j=j+1){
if (i!=j){
sumValue=sumValue+delta[j][i];
n=n+1;
}
}
}
float corr;
corr=sumValue/((float)n);
for (i=<inputnodes>L; i \< <nodes>L; i=i+1) {
for (j=0L; j \< <nodes>L; j=j+1){
if (i!=j){
currentweight[j][i]=currentweight[j][i]+delta[j][i]-corr;
}
}
}
}
for (i = (<nodes>L - <outputnodes>L); i \< <nodes>L; i=i+1) {
j = i - (<nodes>L - <outputnodes>L);
outputVector[j]=netOutput[i];
}
Result r(outputVector,<outputnodes>L);
return r;
}
Result getOutput(float netInput[], long inputsize){
long i;
for (i=0L; i \< <outputsize>L; i=i+1) {
netOutput[i]=0;
}
for (i=0L; i \< <stepnumber>L - 1; i=i+1) {
getStep(netInput,false,inputsize);
}
return getStep(netInput,true,inputsize);
}
>>