tlearn 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ext/tlearn/tlearn_ext.c +44 -188
- metadata +2 -2
data/ext/tlearn/tlearn_ext.c
CHANGED
@@ -122,35 +122,39 @@ extern int load_wts();
|
|
122
122
|
extern int save_wts();
|
123
123
|
extern int act_nds();
|
124
124
|
|
125
|
-
extern int optind;
|
126
|
-
|
127
125
|
|
128
126
|
int run_training(nsweeps, file_path, current_weights_output)
|
129
127
|
long nsweeps;
|
130
128
|
char *file_path;
|
131
129
|
float *current_weights_output;
|
132
130
|
{
|
133
|
-
int argc = 1;
|
134
|
-
char *argv[argc];
|
135
|
-
argv[0] = "tlearn";
|
136
131
|
int status;
|
137
|
-
|
132
|
+
int learning = 1;
|
133
|
+
int loadflag = 0;
|
134
|
+
|
138
135
|
backprop = 0;
|
139
|
-
status = run(
|
136
|
+
status = run(learning, loadflag, nsweeps, file_path, backprop, current_weights_output);
|
140
137
|
|
141
138
|
return(status);
|
142
139
|
}
|
143
140
|
|
144
|
-
int run_fitness(
|
145
|
-
int argc;
|
146
|
-
char **argv;
|
141
|
+
int run_fitness(nsweeps,file_path, current_weights_output)
|
147
142
|
long nsweeps;
|
148
143
|
char *file_path;
|
149
144
|
float *current_weights_output;
|
150
145
|
{
|
151
146
|
int status;
|
152
147
|
backprop = 1;
|
153
|
-
|
148
|
+
char weights[255];
|
149
|
+
|
150
|
+
strcpy(weights, file_path);
|
151
|
+
strcat(weights, ".wts");
|
152
|
+
strcpy(loadfile,weights);
|
153
|
+
|
154
|
+
int learning = 0;
|
155
|
+
int loadflag = 1;
|
156
|
+
|
157
|
+
status = run(learning, loadflag, nsweeps, file_path, backprop, current_weights_output);
|
154
158
|
|
155
159
|
return(status);
|
156
160
|
}
|
@@ -179,7 +183,6 @@ void post_cleanup(){
|
|
179
183
|
}
|
180
184
|
|
181
185
|
void cleanup_horrid_globals(){
|
182
|
-
optind = 1;
|
183
186
|
sweep = 0;
|
184
187
|
tsweeps = 0;
|
185
188
|
rate = .1;
|
@@ -213,15 +216,13 @@ void cleanup_horrid_globals(){
|
|
213
216
|
data = 0;
|
214
217
|
ngroups = 0;
|
215
218
|
root[0] = 0;
|
216
|
-
loadfile[0] = 0;
|
217
|
-
|
218
219
|
otarget = 0;
|
219
220
|
start = 1;
|
220
221
|
}
|
221
222
|
|
222
|
-
int run(
|
223
|
-
int
|
224
|
-
|
223
|
+
int run(learning, loadflag, nsweeps, file_path, backprop, current_weights_output)
|
224
|
+
int learning;
|
225
|
+
int loadflag;
|
225
226
|
long nsweeps;
|
226
227
|
char *file_path;
|
227
228
|
int backprop;
|
@@ -238,8 +239,8 @@ int run(argc,argv, nsweeps, file_path, backprop, current_weights_output)
|
|
238
239
|
|
239
240
|
long ttime = 0; /* number of sweeps since time = 0 */
|
240
241
|
long utime = 0; /* number of sweeps since last update_weights */
|
241
|
-
long tmax = 0;
|
242
|
-
long umax = 0;
|
242
|
+
long tmax = 0; /* maximum number of sweeps (given in .data) */
|
243
|
+
long umax = 0; /* update weights every umax sweeps */
|
243
244
|
long rtime = 0; /* number of sweeps since last rms_report */
|
244
245
|
long check = 0; /* output weights every "check" sweeps */
|
245
246
|
long ctime = 0; /* number of sweeps since last check */
|
@@ -249,19 +250,16 @@ int run(argc,argv, nsweeps, file_path, backprop, current_weights_output)
|
|
249
250
|
int j;
|
250
251
|
int k;
|
251
252
|
int nticks = 1; /* number of internal clock ticks per input */
|
252
|
-
int ticks = 0;
|
253
|
-
int
|
254
|
-
|
255
|
-
int verify = 0; /* flag for printing output values */
|
256
|
-
int probe = 0; /* flag for printing selected node values */
|
253
|
+
int ticks = 0; /* counter for ticks */
|
254
|
+
int reset = 0; /* flag for resetting net */
|
255
|
+
|
257
256
|
int command = 1; /* flag for writing to .cmd file */
|
258
|
-
int
|
259
|
-
int
|
260
|
-
int
|
261
|
-
int
|
262
|
-
int seed = 0; /* seed for random() */
|
257
|
+
int iflag = 0; /* flag for -I */
|
258
|
+
int tflag = 0; /* flag for -T */
|
259
|
+
int rflag = 0; /* flag for -x */
|
260
|
+
int seed = 0; /* seed for random() */
|
263
261
|
|
264
|
-
float err = 0.;
|
262
|
+
float err = 0.; /* cumulative ss error */
|
265
263
|
float ce_err = 0.; /* cumulate cross_entropy error */
|
266
264
|
|
267
265
|
float *w;
|
@@ -272,110 +270,11 @@ int run(argc,argv, nsweeps, file_path, backprop, current_weights_output)
|
|
272
270
|
|
273
271
|
struct cf *ci;
|
274
272
|
|
275
|
-
char cmdfile[128]; /* filename for logging runs of program */
|
276
273
|
char cfile[128]; /* filename for .cf file */
|
277
274
|
|
278
|
-
FILE *cmdfp;
|
279
|
-
|
280
|
-
#ifdef THINK_C
|
281
|
-
argc = ccommand(&argv);
|
282
|
-
#endif /* THINK_C */
|
283
|
-
|
284
|
-
signal(SIGINT, intr);
|
285
|
-
#ifndef ibmpc
|
286
|
-
#ifndef THINK_C
|
287
|
-
signal(SIGHUP, intr);
|
288
|
-
signal(SIGQUIT, intr);
|
289
|
-
signal(SIGKILL, intr);
|
290
|
-
#endif /* THINK_C */
|
291
|
-
#endif
|
292
|
-
|
293
|
-
#ifndef ibmpc
|
294
|
-
exp_init();
|
295
|
-
#endif
|
296
|
-
|
297
275
|
root[0] = 0;
|
298
276
|
strcpy(root, file_path);
|
299
277
|
|
300
|
-
while ((c = getopt(argc, argv, "f:hil:m:n:r:s:tC:E:ILM:PpRS:TU:VvXB:H:D:")) != EOF) {
|
301
|
-
switch (c) {
|
302
|
-
case 'C':
|
303
|
-
check = (long) atol(optarg);
|
304
|
-
ctime = check;
|
305
|
-
break;
|
306
|
-
case 'i':
|
307
|
-
command = 0;
|
308
|
-
break;
|
309
|
-
case 'l':
|
310
|
-
loadflag = 1;
|
311
|
-
strcpy(loadfile,optarg);
|
312
|
-
break;
|
313
|
-
case 'm':
|
314
|
-
momentum = (float) atof(optarg);
|
315
|
-
break;
|
316
|
-
case 'n':
|
317
|
-
nticks = (int) atoi(optarg);
|
318
|
-
break;
|
319
|
-
case 'P':
|
320
|
-
learning = 0;
|
321
|
-
/* drop through deliberately */
|
322
|
-
case 'p':
|
323
|
-
probe = 1;
|
324
|
-
break;
|
325
|
-
case 'r':
|
326
|
-
rate = (double) atof(optarg);
|
327
|
-
break;
|
328
|
-
case 't':
|
329
|
-
teacher = 1;
|
330
|
-
break;
|
331
|
-
case 'V':
|
332
|
-
learning = 0;
|
333
|
-
/* drop through deliberately */
|
334
|
-
case 'v':
|
335
|
-
verify = 1;
|
336
|
-
break;
|
337
|
-
case 'X':
|
338
|
-
rflag = 1;
|
339
|
-
break;
|
340
|
-
case 'E':
|
341
|
-
rms_report = (long) atol(optarg);
|
342
|
-
break;
|
343
|
-
case 'I':
|
344
|
-
iflag = 1;
|
345
|
-
break;
|
346
|
-
case 'M':
|
347
|
-
criterion = (float) atof(optarg);
|
348
|
-
break;
|
349
|
-
case 'R':
|
350
|
-
randomly = 1;
|
351
|
-
break;
|
352
|
-
case 'S':
|
353
|
-
seed = atoi(optarg);
|
354
|
-
break;
|
355
|
-
case 'T':
|
356
|
-
tflag = 1;
|
357
|
-
break;
|
358
|
-
case 'U':
|
359
|
-
umax = atol(optarg);
|
360
|
-
break;
|
361
|
-
case 'B':
|
362
|
-
init_bias = atof(optarg);
|
363
|
-
break;
|
364
|
-
/*
|
365
|
-
* if == 1, use cross-entropy as error;
|
366
|
-
* if == 2, also collect cross-entropy stats.
|
367
|
-
*/
|
368
|
-
case 'H':
|
369
|
-
ce = atoi(optarg);
|
370
|
-
break;
|
371
|
-
case '?':
|
372
|
-
case 'h':
|
373
|
-
default:
|
374
|
-
usage();
|
375
|
-
return(2);
|
376
|
-
break;
|
377
|
-
}
|
378
|
-
}
|
379
278
|
if (nsweeps == 0){
|
380
279
|
perror("ERROR: No -s specified");
|
381
280
|
return(1);
|
@@ -388,26 +287,6 @@ int run(argc,argv, nsweeps, file_path, backprop, current_weights_output)
|
|
388
287
|
return(1);
|
389
288
|
}
|
390
289
|
|
391
|
-
if (command){
|
392
|
-
sprintf(cmdfile, "%s.cmd", root);
|
393
|
-
cmdfp = fopen(cmdfile, "a");
|
394
|
-
if (cmdfp == NULL) {
|
395
|
-
perror("ERROR: Can't open .cmd file");
|
396
|
-
return(1);
|
397
|
-
}
|
398
|
-
for (i = 1; i < argc; i++)
|
399
|
-
fprintf(cmdfp,"%s ",argv[i]);
|
400
|
-
fprintf(cmdfp,"\n");
|
401
|
-
fflush(cmdfp);
|
402
|
-
}
|
403
|
-
|
404
|
-
#ifndef THINK_C
|
405
|
-
sprintf(cmdfile, "%s.pid", root);
|
406
|
-
fpid = fopen(cmdfile, "w");
|
407
|
-
fprintf(fpid, "%d\n", getpid());
|
408
|
-
fclose(fpid);
|
409
|
-
#endif /* THINK_C */
|
410
|
-
|
411
290
|
sprintf(cfile, "%s.cf", root);
|
412
291
|
cfp = fopen(cfile, "r");
|
413
292
|
if (cfp == NULL) {
|
@@ -421,12 +300,14 @@ int run(argc,argv, nsweeps, file_path, backprop, current_weights_output)
|
|
421
300
|
get_connections();
|
422
301
|
get_special();
|
423
302
|
|
424
|
-
if (!seed)
|
303
|
+
if (!seed){
|
425
304
|
seed = time((time_t *) NULL);
|
305
|
+
}
|
426
306
|
srandom(seed);
|
427
307
|
|
428
|
-
if (loadflag)
|
308
|
+
if (loadflag){
|
429
309
|
load_wts();
|
310
|
+
}
|
430
311
|
else {
|
431
312
|
for (i = 0; i < nn; i++){
|
432
313
|
w = *(wt + i);
|
@@ -479,22 +360,24 @@ int run(argc,argv, nsweeps, file_path, backprop, current_weights_output)
|
|
479
360
|
|
480
361
|
nsweeps += tsweeps;
|
481
362
|
for (sweep = tsweeps; sweep < nsweeps; sweep++){
|
482
|
-
|
483
363
|
for (ticks = 0; ticks < nticks; ticks++){
|
484
364
|
|
485
365
|
update_reset(ttime,ticks,rflag,&tmax,&reset);
|
486
366
|
|
487
367
|
if (reset){
|
488
|
-
if (backprop == 0)
|
368
|
+
if (backprop == 0){
|
489
369
|
reset_network(zold,znew,pold,pnew);
|
490
|
-
|
370
|
+
}
|
371
|
+
else{
|
491
372
|
reset_bp_net(zold,znew);
|
373
|
+
}
|
492
374
|
}
|
493
375
|
|
494
376
|
update_inputs(zold,ticks,iflag,&tmax,&linput);
|
495
377
|
|
496
|
-
if (learning || teacher || (rms_report != 0))
|
378
|
+
if (learning || teacher || (rms_report != 0)){
|
497
379
|
update_targets(target,ttime,ticks,tflag,&tmax);
|
380
|
+
}
|
498
381
|
|
499
382
|
act_nds(zold,zmem,znew,wt,linput,target);
|
500
383
|
|
@@ -504,42 +387,27 @@ int run(argc,argv, nsweeps, file_path, backprop, current_weights_output)
|
|
504
387
|
comp_deltas(pold,pnew,wt,dwt,zold,znew,error);
|
505
388
|
if (learning && (backprop == 1))
|
506
389
|
comp_backprop(wt,dwt,zold,zmem,target,error,linput);
|
507
|
-
|
508
|
-
if (probe)
|
509
|
-
print_nodes(zold);
|
510
390
|
}
|
511
|
-
if (
|
391
|
+
if (learning == 0){
|
512
392
|
for (i = 0; i < no; i++){
|
513
393
|
current_weights_output[i] = zold[ni+outputs[i]];
|
514
394
|
}
|
515
395
|
}
|
516
|
-
if (rms_report && (++rtime >= rms_report)){
|
517
|
-
rtime = 0;
|
518
|
-
if (ce == 2)
|
519
|
-
print_error(&ce_err);
|
520
|
-
else
|
521
|
-
print_error(&err);
|
522
|
-
}
|
523
396
|
|
524
|
-
if (
|
525
|
-
ctime = 0;
|
526
|
-
save_wts();
|
527
|
-
}
|
528
|
-
|
529
|
-
if (++ttime >= tmax)
|
397
|
+
if (++ttime >= tmax){
|
530
398
|
ttime = 0;
|
531
|
-
|
399
|
+
}
|
532
400
|
if (learning && (++utime >= umax)){
|
533
401
|
utime = 0;
|
534
402
|
update_weights(wt,dwt,winc);
|
535
403
|
}
|
536
404
|
|
537
405
|
}
|
538
|
-
if (learning)
|
406
|
+
if (learning){
|
539
407
|
save_wts();
|
408
|
+
}
|
540
409
|
|
541
410
|
return(0);
|
542
|
-
|
543
411
|
}
|
544
412
|
|
545
413
|
/* -- Ruby interface -- */
|
@@ -561,9 +429,6 @@ static VALUE tlearn_train(VALUE self, VALUE config) {
|
|
561
429
|
}
|
562
430
|
|
563
431
|
static VALUE tlearn_fitness(VALUE self, VALUE config) {
|
564
|
-
int tlearn_args_count = 4;
|
565
|
-
char *tlearn_args[tlearn_args_count];
|
566
|
-
|
567
432
|
VALUE ruby_array = rb_ary_new();
|
568
433
|
VALUE file_root_value = rb_hash_aref(config, ID2SYM(rb_intern("file_root")));
|
569
434
|
|
@@ -571,20 +436,11 @@ static VALUE tlearn_fitness(VALUE self, VALUE config) {
|
|
571
436
|
long nsweeps = NUM2DBL(sweeps_value);
|
572
437
|
|
573
438
|
char *file_root = StringValueCStr(file_root_value);
|
574
|
-
char weights[strlen(file_root) + strlen(".wts")];
|
575
|
-
|
576
439
|
float *result_weights;
|
577
440
|
|
578
|
-
strcpy(weights, file_root);
|
579
|
-
|
580
|
-
tlearn_args[0] = "tlearn_fitness";
|
581
|
-
tlearn_args[1] = "-l";
|
582
|
-
tlearn_args[2] = strcat(weights, ".wts");
|
583
|
-
tlearn_args[3] = "-V";
|
584
|
-
|
585
441
|
float current_weights_output[6];
|
586
442
|
|
587
|
-
int failure = run_fitness(
|
443
|
+
int failure = run_fitness(nsweeps, file_root, current_weights_output);
|
588
444
|
|
589
445
|
post_cleanup();
|
590
446
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tlearn
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-03 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|