tlearn 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/ext/tlearn/Exp/exp.c +13 -0
- data/ext/tlearn/activate.c +222 -0
- data/ext/tlearn/arrays.c +224 -0
- data/ext/tlearn/compute.c +404 -0
- data/ext/tlearn/extconf.rb +14 -0
- data/ext/tlearn/getopt.c +76 -0
- data/ext/tlearn/parse.c +594 -0
- data/ext/tlearn/subs.c +204 -0
- data/ext/tlearn/tlearn.c +525 -0
- data/ext/tlearn/tlearn_ext.c +587 -0
- data/ext/tlearn/update.c +577 -0
- data/ext/tlearn/weights.c +116 -0
- data/lib/tlearn.rb +17 -0
- data/lib/tlearn/config.rb +101 -0
- data/lib/tlearn/fitness_data.rb +24 -0
- data/lib/tlearn/run.rb +29 -0
- data/lib/tlearn/run_tlearn.rb +68 -0
- data/lib/tlearn/training_data.rb +41 -0
- metadata +64 -0
@@ -0,0 +1,222 @@
|
|
1
|
+
#include <math.h>
|
2
|
+
#include <stdio.h>
|
3
|
+
|
4
|
+
|
5
|
+
#ifdef EXP_TABLE
|
6
|
+
#define EXP(m) \
|
7
|
+
(exp_array[((int) ((m) * exp_mult)) + exp_add])
|
8
|
+
#else
|
9
|
+
#define EXP(m) exp(m)
|
10
|
+
#endif EXP_TABLE
|
11
|
+
|
12
|
+
|
13
|
+
extern int nn; /* number of nodes */
|
14
|
+
extern int ni; /* number of inputs */
|
15
|
+
extern int no; /* number of outputs */
|
16
|
+
extern int nt; /* nn + ni + 1 */
|
17
|
+
extern int np; /* ni + 1 */
|
18
|
+
|
19
|
+
extern struct cf {
|
20
|
+
int con; /* connection flag */
|
21
|
+
int fix; /* fixed-weight flag */
|
22
|
+
int num; /* group number */
|
23
|
+
int lim; /* weight limits */
|
24
|
+
float min; /* weight minimum */
|
25
|
+
float max; /* weight maximum */
|
26
|
+
};
|
27
|
+
|
28
|
+
extern struct nf {
|
29
|
+
int func; /* activation function type */
|
30
|
+
int dela; /* delay flag */
|
31
|
+
int targ; /* target flag */
|
32
|
+
};
|
33
|
+
|
34
|
+
extern struct cf **cinfo; /* (nn x nt) connection info */
|
35
|
+
|
36
|
+
extern struct nf *ninfo; /* (nn) node info */
|
37
|
+
|
38
|
+
extern int backprop; /* flag for back propagation */
|
39
|
+
extern int localist; /* flag for localist input */
|
40
|
+
extern int teacher; /* flag for feeding back target */
|
41
|
+
|
42
|
+
act_nds(aold,amem,anew,awt,local,atarget)
|
43
|
+
float *aold;
|
44
|
+
float *amem;
|
45
|
+
float *anew;
|
46
|
+
float **awt;
|
47
|
+
int *local;
|
48
|
+
float *atarget;
|
49
|
+
{
|
50
|
+
|
51
|
+
extern float *exp_array; /* table look-up for exp function */
|
52
|
+
extern float exp_mult;
|
53
|
+
extern long exp_add;
|
54
|
+
|
55
|
+
register int i;
|
56
|
+
register int j;
|
57
|
+
|
58
|
+
register struct cf *ci;
|
59
|
+
|
60
|
+
register float *w;
|
61
|
+
register float *zo;
|
62
|
+
register float *zn;
|
63
|
+
register float *zp;
|
64
|
+
|
65
|
+
register float **wp;
|
66
|
+
|
67
|
+
register struct cf **cp;
|
68
|
+
register struct nf *n;
|
69
|
+
register struct nf *on;
|
70
|
+
|
71
|
+
register int *l;
|
72
|
+
register float *t;
|
73
|
+
register int tcnt;
|
74
|
+
|
75
|
+
/* for each of nn nodes: update activations */
|
76
|
+
if (backprop == 0){
|
77
|
+
zo = aold + np;
|
78
|
+
zn = anew + np;
|
79
|
+
for (i = 0; i < nn; i++, zo++, zn++){
|
80
|
+
*zo = *zn;
|
81
|
+
}
|
82
|
+
}
|
83
|
+
/* remember current aold in amem */
|
84
|
+
zo = aold + np;
|
85
|
+
zn = amem + np;
|
86
|
+
for (i = 0; i < nn; i++, zo++, zn++){
|
87
|
+
*zn = *zo;
|
88
|
+
}
|
89
|
+
|
90
|
+
/* for each of nn nodes: update net inputs */
|
91
|
+
n = ninfo;
|
92
|
+
cp = cinfo;
|
93
|
+
wp = awt;
|
94
|
+
zn = anew + np;
|
95
|
+
zp = aold + np;
|
96
|
+
t = atarget;
|
97
|
+
tcnt = 0;
|
98
|
+
for (i = 0; i < nn; i++, zn++, n++, cp++, wp++, zp++){
|
99
|
+
if (localist){
|
100
|
+
ci = *cp + np;
|
101
|
+
w = *wp + np;
|
102
|
+
zo = aold + np;
|
103
|
+
*zn = **wp;
|
104
|
+
l = local;
|
105
|
+
while (*l != 0){
|
106
|
+
*zn += *(*wp + *l++);
|
107
|
+
}
|
108
|
+
if (teacher){
|
109
|
+
on = ninfo;
|
110
|
+
for (j = 0; j < nn; j++, w++, zo++, ci++, on++){
|
111
|
+
if (ci->con){
|
112
|
+
if (on->targ){
|
113
|
+
if (*t == -9999.) /* don't care */
|
114
|
+
*zn += *w * *zo;
|
115
|
+
else
|
116
|
+
*zn += *w * *t++;
|
117
|
+
if (++tcnt > no){
|
118
|
+
fprintf(stderr,"WHOA! -t flag requires each output feeding exactly one node\n");
|
119
|
+
exit(1);
|
120
|
+
}
|
121
|
+
}
|
122
|
+
else
|
123
|
+
*zn += *w * *zo;
|
124
|
+
}
|
125
|
+
}
|
126
|
+
}
|
127
|
+
else {
|
128
|
+
for (j = 0; j < nn; j++, w++, zo++, ci++){
|
129
|
+
if (ci->con)
|
130
|
+
*zn += *w * *zo;
|
131
|
+
}
|
132
|
+
}
|
133
|
+
/* apply activation function */
|
134
|
+
/* 0 = default */
|
135
|
+
/* 1 = bipolar */
|
136
|
+
/* 2 = linear */
|
137
|
+
/* 3 = binary */
|
138
|
+
if (n->func != 2){
|
139
|
+
if (*zn > 10.)
|
140
|
+
*zn = 10.;
|
141
|
+
else if (*zn < -10.)
|
142
|
+
*zn = -10.;
|
143
|
+
}
|
144
|
+
if (n->func < 2)
|
145
|
+
*zn = 1.0 / (1.0 + EXP(0.0 - *zn));
|
146
|
+
if (n->func == 1)
|
147
|
+
*zn = 2. * *zn - 1.;
|
148
|
+
else if (n->func == 3){
|
149
|
+
if (*zn > 0.)
|
150
|
+
*zn = 1.;
|
151
|
+
else
|
152
|
+
*zn = -1.;
|
153
|
+
}
|
154
|
+
/* if no-delay, then update immediately */
|
155
|
+
if (n->dela == 0)
|
156
|
+
*zp = *zn;
|
157
|
+
}
|
158
|
+
else {
|
159
|
+
ci = *cp;
|
160
|
+
w = *wp;
|
161
|
+
zo = aold;
|
162
|
+
*zn = 0.;
|
163
|
+
/* collect excitation */
|
164
|
+
if (teacher){
|
165
|
+
for (j = 0; j <= ni; j++, w++, zo++, ci++){
|
166
|
+
if (ci->con)
|
167
|
+
*zn += *w * *zo;
|
168
|
+
}
|
169
|
+
on = ninfo;
|
170
|
+
for (j = 0; j < nn; j++, w++, zo++, ci++, on++){
|
171
|
+
if (ci->con){
|
172
|
+
if (on->targ){
|
173
|
+
if (*t == -9999.)
|
174
|
+
*zn += *w * *zo;
|
175
|
+
else
|
176
|
+
*zn += *w * *t++;
|
177
|
+
if (++tcnt > no){
|
178
|
+
fprintf(stderr,"WHOA! -t flag requires each output feeding exactly one node\n");
|
179
|
+
exit(1);
|
180
|
+
}
|
181
|
+
}
|
182
|
+
else
|
183
|
+
*zn += *w * *zo;
|
184
|
+
}
|
185
|
+
}
|
186
|
+
}
|
187
|
+
else {
|
188
|
+
for (j = 0; j < nt; j++, w++, zo++, ci++){
|
189
|
+
if (ci->con)
|
190
|
+
*zn += *w * *zo;
|
191
|
+
}
|
192
|
+
}
|
193
|
+
/* apply activation function */
|
194
|
+
/* 0 = default */
|
195
|
+
/* 1 = bipolar */
|
196
|
+
/* 2 = linear */
|
197
|
+
/* 3 = binary */
|
198
|
+
if (n->func != 2){
|
199
|
+
if (*zn > 10.)
|
200
|
+
*zn = 10.;
|
201
|
+
else if (*zn < -10.)
|
202
|
+
*zn = -10.;
|
203
|
+
}
|
204
|
+
if (n->func < 2)
|
205
|
+
*zn = 1.0 / (1.0 + EXP(0.0 - *zn));
|
206
|
+
if (n->func == 1)
|
207
|
+
*zn = 2. * *zn - 1.;
|
208
|
+
else if (n->func == 3){
|
209
|
+
if (*zn > 0.)
|
210
|
+
*zn = 1.;
|
211
|
+
else
|
212
|
+
*zn = -1.;
|
213
|
+
}
|
214
|
+
/* if no-delay, then update immediately */
|
215
|
+
if (n->dela == 0)
|
216
|
+
*zp = *zn;
|
217
|
+
}
|
218
|
+
}
|
219
|
+
|
220
|
+
}
|
221
|
+
|
222
|
+
|
data/ext/tlearn/arrays.c
ADDED
@@ -0,0 +1,224 @@
|
|
1
|
+
|
2
|
+
/* make_arrays() - malloc space for arrays */
|
3
|
+
|
4
|
+
#include <stdio.h>
|
5
|
+
|
6
|
+
#ifdef ibmpc
|
7
|
+
extern char far *malloc();
|
8
|
+
#else
|
9
|
+
extern void *malloc();
|
10
|
+
#endif
|
11
|
+
extern int nn; /* number of nodes */
|
12
|
+
extern int ni; /* number of inputs */
|
13
|
+
extern int no; /* number of outputs */
|
14
|
+
extern int nt; /* nn + ni + 1 */
|
15
|
+
|
16
|
+
struct cf {
|
17
|
+
int con; /* connection flag */
|
18
|
+
int fix; /* fixed-weight flag */
|
19
|
+
int num; /* group number */
|
20
|
+
int lim; /* weight limits */
|
21
|
+
float min; /* weight minimum */
|
22
|
+
float max; /* weight maximum */
|
23
|
+
};
|
24
|
+
|
25
|
+
struct nf {
|
26
|
+
int func; /* activation function type */
|
27
|
+
int dela; /* delay flag */
|
28
|
+
int targ; /* target flag */
|
29
|
+
};
|
30
|
+
|
31
|
+
extern struct cf **cinfo; /* (nn x nt) connection info */
|
32
|
+
extern struct nf *ninfo; /* (nn) node activation function info */
|
33
|
+
|
34
|
+
extern int *outputs; /* (no) indices of output nodes */
|
35
|
+
extern int *selects; /* (nn+1) nodes selected for probe printout */
|
36
|
+
extern int *linput; /* (ni) localist input array */
|
37
|
+
|
38
|
+
extern float *znew; /* (nt) inputs and activations at time t+1 */
|
39
|
+
extern float *zold; /* (nt) inputs and activations at time t */
|
40
|
+
extern float *zmem; /* (nt) inputs and activations at time t */
|
41
|
+
extern float **wt; /* (nn x nt) weight TO node i FROM node j*/
|
42
|
+
extern float **dwt; /* (nn x nt) delta weight at time t */
|
43
|
+
extern float **winc; /* (nn x nt) accumulated weight increment*/
|
44
|
+
extern float *target; /* (no) output target values */
|
45
|
+
extern float *error; /* (nn) error = (output - target) values */
|
46
|
+
extern float ***pnew; /* (nn x nt x nn) p-variable at time t+1 */
|
47
|
+
extern float ***pold; /* (nn x nt x nn) p-variable at time t */
|
48
|
+
|
49
|
+
|
50
|
+
make_arrays()
|
51
|
+
{
|
52
|
+
|
53
|
+
int i;
|
54
|
+
int j;
|
55
|
+
|
56
|
+
struct cf *ci;
|
57
|
+
struct nf *n;
|
58
|
+
|
59
|
+
zold = (float *) malloc(nt * sizeof(float));
|
60
|
+
if (zold == NULL){
|
61
|
+
perror("zold malloc failed");
|
62
|
+
exit(1);
|
63
|
+
}
|
64
|
+
zmem = (float *) malloc(nt * sizeof(float));
|
65
|
+
if (zmem == NULL){
|
66
|
+
perror("zmem malloc failed");
|
67
|
+
exit(1);
|
68
|
+
}
|
69
|
+
znew = (float *) malloc(nt * sizeof(float));
|
70
|
+
if (znew == NULL){
|
71
|
+
perror("znew malloc failed");
|
72
|
+
exit(1);
|
73
|
+
}
|
74
|
+
target = (float *) malloc(no * sizeof(float));
|
75
|
+
if (target == NULL){
|
76
|
+
perror("target malloc failed");
|
77
|
+
exit(1);
|
78
|
+
}
|
79
|
+
error = (float *) malloc(nn * sizeof(float));
|
80
|
+
if (error == NULL){
|
81
|
+
perror("error malloc failed");
|
82
|
+
exit(1);
|
83
|
+
}
|
84
|
+
selects = (int *) malloc(nt * sizeof(int));
|
85
|
+
if (selects == NULL){
|
86
|
+
perror("selects malloc failed");
|
87
|
+
exit(1);
|
88
|
+
}
|
89
|
+
outputs = (int *) malloc(no * sizeof(int));
|
90
|
+
if (outputs == NULL){
|
91
|
+
perror("outputs malloc failed");
|
92
|
+
exit(1);
|
93
|
+
}
|
94
|
+
linput = (int *) malloc(ni * sizeof(int));
|
95
|
+
if (linput == NULL){
|
96
|
+
perror("linput malloc failed");
|
97
|
+
exit(1);
|
98
|
+
}
|
99
|
+
|
100
|
+
wt = (float **) malloc(nn * sizeof(float *));
|
101
|
+
if (wt == NULL){
|
102
|
+
printf("wt malloc failed--needed %d bytes for pointers", nn*sizeof(float *));
|
103
|
+
exit(1);
|
104
|
+
}
|
105
|
+
for (i = 0; i < nn; i++){
|
106
|
+
*(wt + i) = (float *) malloc(nt * sizeof(float));
|
107
|
+
if (*(wt + i) == NULL){
|
108
|
+
printf("wt malloc failed");
|
109
|
+
printf("--got %d nodes, %d remaining", i, nn);
|
110
|
+
exit(1);
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
dwt = (float **) malloc(nn * sizeof(float *));
|
115
|
+
if (dwt == NULL){
|
116
|
+
perror("dwt malloc failed");
|
117
|
+
exit(1);
|
118
|
+
}
|
119
|
+
for (i = 0; i < nn; i++){
|
120
|
+
*(dwt + i) = (float *) malloc(nt * sizeof(float));
|
121
|
+
if (*(dwt + i) == NULL){
|
122
|
+
perror("dwt malloc failed");
|
123
|
+
exit(1);
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
winc = (float **) malloc(nn * sizeof(float *));
|
128
|
+
if (winc == NULL){
|
129
|
+
perror("winc malloc failed");
|
130
|
+
exit(1);
|
131
|
+
}
|
132
|
+
for (i = 0; i < nn; i++){
|
133
|
+
*(winc + i) = (float *) malloc(nt * sizeof(float));
|
134
|
+
if (*(winc + i) == NULL){
|
135
|
+
perror("winc malloc failed");
|
136
|
+
exit(1);
|
137
|
+
}
|
138
|
+
}
|
139
|
+
|
140
|
+
cinfo = (struct cf **) malloc(nn * sizeof(struct cf *));
|
141
|
+
if (cinfo == NULL){
|
142
|
+
perror("cinfo malloc failed");
|
143
|
+
exit(1);
|
144
|
+
}
|
145
|
+
for (i = 0; i < nn; i++){
|
146
|
+
*(cinfo + i) = (struct cf *) malloc(nt * sizeof(struct cf));
|
147
|
+
if (*(cinfo + i) == NULL){
|
148
|
+
perror("cinfo malloc failed");
|
149
|
+
exit(1);
|
150
|
+
}
|
151
|
+
}
|
152
|
+
|
153
|
+
ninfo = (struct nf *) malloc(nn * sizeof(struct nf));
|
154
|
+
if (ninfo == NULL){
|
155
|
+
perror("ninfo malloc failed");
|
156
|
+
exit(1);
|
157
|
+
}
|
158
|
+
|
159
|
+
n = ninfo;
|
160
|
+
for (i = 0; i < nn; i++, n++){
|
161
|
+
n->func = 0;
|
162
|
+
n->dela = 0;
|
163
|
+
n->targ = 0;
|
164
|
+
ci = *(cinfo + i);
|
165
|
+
for (j = 0; j < nt; j++, ci++){
|
166
|
+
ci->con = 0;
|
167
|
+
ci->fix = 0;
|
168
|
+
ci->num = 0;
|
169
|
+
ci->lim = 0;
|
170
|
+
ci->min = 0.;
|
171
|
+
ci->max = 0.;
|
172
|
+
}
|
173
|
+
}
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
make_parrays()
|
178
|
+
{
|
179
|
+
|
180
|
+
int i;
|
181
|
+
int j;
|
182
|
+
|
183
|
+
pold = (float ***) malloc(nn * sizeof(float **));
|
184
|
+
if (pold == NULL){
|
185
|
+
perror("pold malloc failed");
|
186
|
+
exit(1);
|
187
|
+
}
|
188
|
+
for (i = 0; i < nn; i++){
|
189
|
+
*(pold + i) = (float **) malloc(nt * sizeof(float *));
|
190
|
+
if (*(pold + i) == NULL){
|
191
|
+
perror("pold malloc failed");
|
192
|
+
exit(1);
|
193
|
+
}
|
194
|
+
for (j = 0; j < nt; j++){
|
195
|
+
*(*(pold + i) + j) = (float *) malloc(nn * sizeof(float));
|
196
|
+
if (*(*(pold + i) + j) == NULL){
|
197
|
+
perror("pold malloc failed");
|
198
|
+
exit(1);
|
199
|
+
}
|
200
|
+
}
|
201
|
+
}
|
202
|
+
|
203
|
+
pnew = (float ***) malloc(nn * sizeof(float **));
|
204
|
+
if (pnew == NULL){
|
205
|
+
perror("pnew malloc failed");
|
206
|
+
exit(1);
|
207
|
+
}
|
208
|
+
for (i = 0; i < nn; i++){
|
209
|
+
*(pnew + i) = (float **) malloc(nt * sizeof(float *));
|
210
|
+
if (*(pnew + i) == NULL){
|
211
|
+
perror("pnew malloc failed");
|
212
|
+
exit(1);
|
213
|
+
}
|
214
|
+
for (j = 0; j < nt; j++){
|
215
|
+
*(*(pnew + i) + j) = (float *) malloc(nn * sizeof(float));
|
216
|
+
if (*(*(pnew + i) + j) == NULL){
|
217
|
+
perror("pnew malloc failed");
|
218
|
+
exit(1);
|
219
|
+
}
|
220
|
+
}
|
221
|
+
}
|
222
|
+
|
223
|
+
}
|
224
|
+
|