-
Notifications
You must be signed in to change notification settings - Fork 12
/
bind_ann_base.lua.cc
549 lines (450 loc) · 14.6 KB
/
bind_ann_base.lua.cc
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
* This file is part of the Neural Network modules of the APRIL toolkit (A
* Pattern Recognizer In Lua).
*
* Copyright 2012, Salvador España-Boquera, Adrian Palacios Corella, Francisco
* Zamora-Martinez
*
* The APRIL-ANN toolkit is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
//BIND_HEADER_C
#include "bind_activation_function.h"
#include "bind_matrix.h"
//BIND_END
//BIND_HEADER_H
#include "ann.h"
#include "connection.h"
#include "all_all_connection.h"
#include "bias_connection.h"
#include "action.h"
#include "dot_product_action.h"
#include "forward_bias_action.h"
#include "activations_action.h"
#include "actunit.h"
#include "errorfunc.h"
#include "bind_trainablesuper.h"
#include "bind_function_interface.h"
#include "trainsuper.h"
using namespace Trainable;
using namespace ANN;
using namespace Functions;
//BIND_END
//BIND_LUACLASSNAME TrainableSupervised __trainable__
//BIND_LUACLASSNAME ANNBase ann.__base__
//BIND_CPP_CLASS ANNBase
//BIND_SUBCLASS_OF ANNBase TrainableSupervised
//////////////////////////////////////////////////////////////
//BIND_LUACLASSNAME ActivationUnits ann.units.__base__
//BIND_LUACLASSNAME RealActivationUnits ann.units.real_cod
//BIND_LUACLASSNAME LocalActivationUnits ann.units.local_cod
//BIND_CPP_CLASS ActivationUnits
//BIND_CPP_CLASS RealActivationUnits
//BIND_CPP_CLASS LocalActivationUnits
//BIND_SUBCLASS_OF RealActivationUnits ActivationUnits
//BIND_SUBCLASS_OF LocalActivationUnits ActivationUnits
//BIND_CONSTRUCTOR ActivationUnits
{
LUABIND_ERROR("Abstract class!!!\n");
}
//BIND_END
//BIND_METHOD ActivationUnits num_neurons
{
LUABIND_RETURN(uint, obj->numNeurons());
}
//BIND_END
//BIND_CONSTRUCTOR RealActivationUnits
//DOC_BEGIN
// ann.units.real{ size = ..., ann = ..., type = (inputs, hidden, outputs) }
/// Create a real activation units layer
/// @param size The number of neurons
/// @param ann The ANN which belongs
/// @param type The type, could be inputs, hidden, outputs
//DOC_END
{
LUABIND_CHECK_ARGN(==,1);
check_table_fields(L, 1, "size", "ann",
"type", 0);
unsigned int size;
ANNBase *ann;
const char *type;
LUABIND_GET_TABLE_PARAMETER(1, size, uint, size);
LUABIND_GET_TABLE_PARAMETER(1, ann, ANNBase, ann);
LUABIND_GET_TABLE_PARAMETER(1, type, string, type);
if (strcmp(type, "inputs")!=0 &&
strcmp(type, "hidden")!=0 &&
strcmp(type, "outputs")!=0) {
LUABIND_FERROR1("Incorrect type '%s'!!\n", type);
}
obj = new RealActivationUnits(size, ann->getConfReference(),
strcmp(type, "inputs") != 0);
ann->registerActivationUnits(obj);
if (strcmp(type, "inputs") == 0) ann->registerInput(obj);
else if (strcmp(type, "outputs") == 0) ann->registerOutput(obj);
LUABIND_RETURN(RealActivationUnits, obj);
}
//BIND_END
//BIND_CONSTRUCTOR LocalActivationUnits
//DOC_BEGIN
// ann.units.real{ size = ..., ann = ... }
/// Create a local activation input units layer
/// @param size The number of neurons
/// @param ann The ANN which belongs
//DOC_END
{
LUABIND_CHECK_ARGN(==,1);
check_table_fields(L, 1, "size", "num_groups", "ann", 0);
unsigned int size, num_groups;
ANNBase *ann;
LUABIND_GET_TABLE_PARAMETER(1, size, uint, size);
LUABIND_GET_TABLE_OPTIONAL_PARAMETER(1, num_groups, uint, num_groups, 1);
LUABIND_GET_TABLE_PARAMETER(1, ann, ANNBase, ann);
obj = new LocalActivationUnits(num_groups, size, ann->getConfReference());
ann->registerActivationUnits(obj);
LUABIND_RETURN(LocalActivationUnits, obj);
}
//BIND_END
////////////////////////////////////////////////
//BIND_CONSTRUCTOR ANNBase
{
LUABIND_ERROR("Abstract class!!!");
}
//BIND_END
//BIND_METHOD ANNBase get_num_weights
{
LUABIND_RETURN(uint, obj->getNumWeights());
}
//BIND_END
//BIND_METHOD ANNBase register_action
{
Action *action;
LUABIND_GET_PARAMETER(1, Action, action);
obj->registerAction(action);
LUABIND_RETURN(Action, action);
}
//BIND_END
//BIND_METHOD ANNBase register_input
{
ActivationUnits *actu;
LUABIND_GET_PARAMETER(1, ActivationUnits, actu);
obj->registerInput(actu);
}
//BIND_END
//BIND_METHOD ANNBase register_output
{
ActivationUnits *actu;
LUABIND_GET_PARAMETER(1, ActivationUnits, actu);
obj->registerOutput(actu);
}
//BIND_END
//BIND_METHOD ANNBase set_use_cuda
{
bool use_cuda, pinned;
LUABIND_GET_PARAMETER(1, bool, use_cuda);
LUABIND_GET_OPTIONAL_PARAMETER(2, bool, pinned, true);
obj->setUseCuda(use_cuda, pinned);
}
//BIND_END
/////////////////////////////////////////////////////
//BIND_LUACLASSNAME Action ann.actions.__base__
//BIND_CPP_CLASS Action
//BIND_CONSTRUCTOR Action
{
LUABIND_ERROR("Abstract class!!!");
}
//BIND_END
//BIND_METHOD Action set_option
{
const char *name;
double value;
LUABIND_GET_PARAMETER(1, string, name);
LUABIND_GET_PARAMETER(2, double, value);
obj->setOption(name, value);
}
//BIND_END
//BIND_METHOD Action get_option
{
const char *name;
LUABIND_GET_PARAMETER(1, string, name);
LUABIND_RETURN(double, obj->getOption(name));
}
//BIND_END
//BIND_METHOD Action has_option
{
const char *name;
LUABIND_GET_PARAMETER(1, string, name);
LUABIND_RETURN(bool, obj->hasOption(name));
}
//BIND_END
//////////////////////////////////////////////////////
//BIND_LUACLASSNAME ForwardBiasAction ann.actions.forward_bias
//BIND_CPP_CLASS ForwardBiasAction
//BIND_SUBCLASS_OF ForwardBiasAction Action
//BIND_CONSTRUCTOR ForwardBiasAction
{
LUABIND_CHECK_ARGN(==,1);
check_table_fields(L, 1, "ann", "output", "connections", 0);
ActivationUnits *output;
Connections *conn;
ANNBase *ann;
LUABIND_GET_TABLE_PARAMETER(1, output, ActivationUnits, output);
LUABIND_GET_TABLE_PARAMETER(1, connections, Connections, conn);
LUABIND_GET_TABLE_PARAMETER(1, ann, ANNBase, ann);
obj = new ForwardBiasAction(ann->getConfReference(),
output, conn);
ann->registerAction(obj);
LUABIND_RETURN(ForwardBiasAction, obj);
}
//BIND_END
//////////////////////////////////////////////////////
//BIND_LUACLASSNAME DotProductAction ann.actions.dot_product
//BIND_CPP_CLASS DotProductAction
//BIND_SUBCLASS_OF DotProductAction Action
//BIND_CONSTRUCTOR DotProductAction
{
LUABIND_CHECK_ARGN(==,1);
check_table_fields(L, 1, "ann", "input", "output", "connections",
"transpose", 0);
ActivationUnits *input;
ActivationUnits *output;
Connections *conn;
ANNBase *ann;
bool transpose;
LUABIND_GET_TABLE_PARAMETER(1, input, ActivationUnits, input);
LUABIND_GET_TABLE_PARAMETER(1, output, ActivationUnits, output);
LUABIND_GET_TABLE_PARAMETER(1, connections, Connections, conn);
LUABIND_GET_TABLE_PARAMETER(1, ann, ANNBase, ann);
LUABIND_GET_TABLE_OPTIONAL_PARAMETER(1, transpose, bool, transpose, false);
obj = new DotProductAction(ann->getConfReference(),
input, output, conn,
transpose);
ann->registerAction(obj);
LUABIND_RETURN(DotProductAction, obj);
}
//BIND_END
//////////////////////////////////////////////////////
//BIND_LUACLASSNAME ActivationsAction ann.actions.activations
//BIND_CPP_CLASS ActivationsAction
//BIND_SUBCLASS_OF ActivationsAction Action
//BIND_CONSTRUCTOR ActivationsAction
{
LUABIND_CHECK_ARGN(==,1);
check_table_fields(L, 1, "ann", "actfunc", "output", 0);
ActivationUnits *output;
ActivationFunction *actfunc;
ANNBase *ann;
LUABIND_GET_TABLE_PARAMETER(1, output, ActivationUnits, output);
LUABIND_GET_TABLE_PARAMETER(1, actfunc, ActivationFunction, actfunc);
LUABIND_GET_TABLE_PARAMETER(1, ann, ANNBase, ann);
obj = new ActivationsAction(ann->getConfReference(),
output, actfunc);
ann->registerAction(obj);
LUABIND_RETURN(ActivationsAction, obj);
}
//BIND_END
//////////////////////////////////////////////////////
//BIND_LUACLASSNAME ErrorFunction ann.error_functions.__base__
//BIND_CPP_CLASS ErrorFunction
//BIND_CONSTRUCTOR ErrorFunction
{
LUABIND_ERROR("Abstract class!!!");
}
//BIND_END
//BIND_LUACLASSNAME MSE ann.error_functions.mse
//BIND_CPP_CLASS MSE
//BIND_SUBCLASS_OF MSE ErrorFunction
//BIND_CONSTRUCTOR MSE
{
obj = new MSE();
LUABIND_RETURN(MSE, obj);
}
//BIND_END
//BIND_LUACLASSNAME Tanh ann.error_functions.tanh
//BIND_CPP_CLASS Tanh
//BIND_SUBCLASS_OF Tanh ErrorFunction
//BIND_CONSTRUCTOR Tanh
{
obj = new Tanh();
LUABIND_RETURN(Tanh, obj);
}
//BIND_END
//BIND_LUACLASSNAME CrossEntropy ann.error_functions.cross_entropy
//BIND_CPP_CLASS CrossEntropy
//BIND_SUBCLASS_OF CrossEntropy ErrorFunction
//BIND_CONSTRUCTOR CrossEntropy
{
obj = new CrossEntropy();
LUABIND_RETURN(CrossEntropy, obj);
}
//BIND_END
//BIND_LUACLASSNAME FullCrossEntropy ann.error_functions.full_cross_entropy
//BIND_CPP_CLASS FullCrossEntropy
//BIND_SUBCLASS_OF FullCrossEntropy ErrorFunction
//BIND_CONSTRUCTOR FullCrossEntropy
{
obj = new FullCrossEntropy();
LUABIND_RETURN(FullCrossEntropy, obj);
}
//BIND_END
//BIND_LUACLASSNAME LocalFMeasure ann.error_functions.local_fmeasure
//BIND_CPP_CLASS LocalFMeasure
//BIND_SUBCLASS_OF LocalFMeasure ErrorFunction
//BIND_CONSTRUCTOR LocalFMeasure
{
obj = new LocalFMeasure();
LUABIND_RETURN(LocalFMeasure, obj);
}
//BIND_END
///////////////////////////////////////////////////
//BIND_LUACLASSNAME Connections ann.connections.__base__
//BIND_CPP_CLASS Connections
//BIND_CONSTRUCTOR Connections
{
LUABIND_ERROR("Abstract class!!!");
}
//BIND_END
//BIND_METHOD Connections load
{
LUABIND_CHECK_ARGN(==,1);
check_table_fields(L, 1, "w", "oldw", "first_pos", "column_size", 0);
unsigned int first_pos, column_size;
MatrixFloat *w, *oldw;
LUABIND_GET_TABLE_PARAMETER(1, w, MatrixFloat, w);
LUABIND_GET_TABLE_OPTIONAL_PARAMETER(1, oldw, MatrixFloat, oldw, w);
LUABIND_GET_TABLE_OPTIONAL_PARAMETER(1, first_pos, uint, first_pos, 0);
LUABIND_GET_TABLE_OPTIONAL_PARAMETER(1, column_size, uint, column_size,
obj->getNumInputs());
LUABIND_RETURN(uint, obj->loadWeights(w, oldw, first_pos, column_size));
}
//BIND_END
//BIND_METHOD Connections weights
{
LUABIND_CHECK_ARGN(==,1);
check_table_fields(L, 1, "w", "oldw", "first_pos", "column_size", 0);
unsigned int first_pos, column_size;
MatrixFloat *w, *oldw;
LUABIND_GET_TABLE_OPTIONAL_PARAMETER(1, w, MatrixFloat, w, 0);
LUABIND_GET_TABLE_OPTIONAL_PARAMETER(1, oldw, MatrixFloat, oldw, 0);
LUABIND_GET_TABLE_OPTIONAL_PARAMETER(1, first_pos, uint, first_pos, 0);
LUABIND_GET_TABLE_OPTIONAL_PARAMETER(1, column_size, uint, column_size, 1);
int size = static_cast<int>(obj->size());
if (!w) w = new MatrixFloat(1, first_pos + size);
if (!oldw) oldw = new MatrixFloat(1, w->size);
if (first_pos + obj->size() > static_cast<unsigned int>(w->size) ||
first_pos + obj->size() > static_cast<unsigned int>(oldw->size) )
LUABIND_ERROR("Incorrect matrix size!!\n");
LUABIND_RETURN(uint, obj->copyWeightsTo(w, oldw, first_pos, column_size));
LUABIND_RETURN(MatrixFloat, w);
LUABIND_RETURN(MatrixFloat, oldw);
}
//BIND_END
//BIND_METHOD Connections size
{
LUABIND_RETURN(uint, obj->size());
}
//BIND_END
//BIND_LUACLASSNAME AllAllConnections ann.connections.all_all
//BIND_CPP_CLASS AllAllConnections
//BIND_SUBCLASS_OF AllAllConnections Connections
//BIND_CONSTRUCTOR AllAllConnections
{
check_table_fields(L, 1, "input_size", "output_size", "ann", 0);
unsigned int input_size, output_size;
ANNBase *ann;
LUABIND_GET_TABLE_PARAMETER(1, input_size, uint, input_size);
LUABIND_GET_TABLE_PARAMETER(1, output_size, uint, output_size);
LUABIND_GET_TABLE_PARAMETER(1, ann, ANNBase, ann);
obj=new AllAllConnections(input_size, output_size);
ann->registerConnections(obj);
LUABIND_RETURN(AllAllConnections, obj);
}
//BIND_END
//BIND_LUACLASSNAME BiasConnections ann.connections.bias
//BIND_CPP_CLASS BiasConnections
//BIND_SUBCLASS_OF BiasConnections Connections
//BIND_CONSTRUCTOR BiasConnections
{
check_table_fields(L, 1, "size", "ann", 0);
unsigned int size;
ANNBase *ann;
LUABIND_GET_TABLE_PARAMETER(1, size, uint, size);
LUABIND_GET_TABLE_PARAMETER(1, ann, ANNBase, ann);
obj=new BiasConnections(size);
ann->registerConnections(obj);
LUABIND_RETURN(BiasConnections, obj);
}
//BIND_END
/////////////////////////////////////////////////////
//BIND_LUACLASSNAME TimeSeriesErrorFunction ann.time_series_error_functions.__base__
//BIND_CPP_CLASS TimeSeriesErrorFunction
//BIND_CONSTRUCTOR TimeSeriesErrorFunction
{
LUABIND_ERROR("Abstract class!!!");
}
//BIND_END
//BIND_METHOD TimeSeriesErrorFunction computeErrorFromTimeSerie
{
float *outputs;
float *target_outputs;
unsigned int output_size;
check_table_fields(L, 1, "outputs", "target_outputs", 0);
lua_getfield(L, 1, "outputs");
LUABIND_TABLE_GETN(-1, output_size);
outputs = new float[output_size];
LUABIND_TABLE_TO_VECTOR(-1, float, outputs, output_size);
lua_pop(L, 1);
lua_getfield(L, 1, "target_outputs");
unsigned int aux;
LUABIND_TABLE_GETN(-1, aux);
if (aux != output_size)
LUABIND_ERROR ("outputs and target_outputs sizes are diferent!!!\n");
target_outputs = new float[output_size];
LUABIND_TABLE_TO_VECTOR(-1, float, target_outputs, output_size);
lua_pop(L, 1);
LUABIND_RETURN(float, obj->computeErrorFromTimeSerie(outputs, target_outputs,
output_size));
delete[] outputs;
delete[] target_outputs;
}
//BIND_END
////////////////////////////////////////////////////
//BIND_LUACLASSNAME NormalizedRootMSE ann.time_series_error_functions.nrmse
//BIND_CPP_CLASS NormalizedRootMSE
//BIND_SUBCLASS_OF NormalizedRootMSE TimeSeriesErrorFunction
//BIND_CONSTRUCTOR NormalizedRootMSE
{
obj = new NormalizedRootMSE();
LUABIND_RETURN(NormalizedRootMSE, obj);
}
//BIND_END
////////////////////////////////////////////////////
//BIND_LUACLASSNAME RootMSE ann.time_series_error_functions.rmse
//BIND_CPP_CLASS RootMSE
//BIND_SUBCLASS_OF RootMSE TimeSeriesErrorFunction
//BIND_CONSTRUCTOR RootMSE
{
obj = new RootMSE();
LUABIND_RETURN(RootMSE, obj);
}
//BIND_END
////////////////////////////////////////////////////
//BIND_LUACLASSNAME AbsoluteError ann.time_series_error_functions.absolute_error
//BIND_CPP_CLASS AbsoluteError
//BIND_SUBCLASS_OF AbsoluteError TimeSeriesErrorFunction
//BIND_CONSTRUCTOR AbsoluteError
{
obj = new AbsoluteError();
LUABIND_RETURN(AbsoluteError, obj);
}
//BIND_END