/
fann-accs.cc
364 lines (300 loc) · 10.3 KB
/
fann-accs.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
/*
* All setters, getters and other information providers
*/
#include <string.h>
#include <stdio.h>
#include "node-fann.h"
Handle<Value> NNet::GetTrainingAlgorithm(Local<String> property, const AccessorInfo &info)
{
HandleScope scope;
Local<Object> self = info.Holder();
NNet *net = ObjectWrap::Unwrap<NNet>(self);
int size = sizeof(FANN_TRAIN_NAMES)/sizeof(char*);
enum fann_train_enum algo = fann_get_training_algorithm(net->FANN);
if (algo >= 0 && algo < size) {
return scope.Close(NormalizeName(FANN_TRAIN_NAMES[algo], TRAIN_PREFIX, sizeof(TRAIN_PREFIX)-1));
} else {
return Undefined();
}
}
Handle<Value> NNet::GetNetworkType(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
int size = sizeof(FANN_NETTYPE_NAMES)/sizeof(char*);
enum fann_nettype_enum ret = fann_get_network_type(net->FANN);
if (ret >= 0 && ret < size) {
return scope.Close(NormalizeName(FANN_NETTYPE_NAMES[ret], NETTYPE_PREFIX, sizeof(NETTYPE_PREFIX)-1));
} else {
return Undefined();
}
}
void NNet::SetTrainingAlgorithm(Local<String> property, Local<Value> value, const AccessorInfo& info)
{
HandleScope scope;
Local<Object> self = info.Holder();
NNet *net = ObjectWrap::Unwrap<NNet>(self);
int size = sizeof(FANN_TRAIN_NAMES)/sizeof(char*);
int num = -1;
if (value->IsString()) {
num = _SeekCharArray(String::Cast(*value), FANN_TRAIN_NAMES, size, TRAIN_PREFIX);
} else if (value->IsNumber()) {
num = value->NumberValue();
}
if (num >= 0 && num < size) {
fann_set_training_algorithm(net->FANN, fann_train_enum(num));
}
}
Handle<Value> NNet::GetLearningRate(Local<String> property, const AccessorInfo &info)
{
HandleScope scope;
Local<Object> self = info.Holder();
NNet *net = ObjectWrap::Unwrap<NNet>(self);
float rate = fann_get_learning_rate(net->FANN);
return scope.Close(Number::New(rate));
}
Handle<Value> NNet::GetLearningMomentum(Local<String> property, const AccessorInfo &info)
{
HandleScope scope;
Local<Object> self = info.Holder();
NNet *net = ObjectWrap::Unwrap<NNet>(self);
float rate = fann_get_learning_momentum(net->FANN);
return scope.Close(Number::New(rate));
}
void NNet::SetLearningRate(Local<String> property, Local<Value> value, const AccessorInfo& info)
{
HandleScope scope;
Local<Object> self = info.Holder();
NNet *net = ObjectWrap::Unwrap<NNet>(self);
fann_set_learning_rate(net->FANN, value->NumberValue());
}
void NNet::SetLearningMomentum(Local<String> property, Local<Value> value, const AccessorInfo& info)
{
HandleScope scope;
Local<Object> self = info.Holder();
NNet *net = ObjectWrap::Unwrap<NNet>(self);
fann_set_learning_momentum(net->FANN, value->NumberValue());
}
Handle<Value> NNet::ActivationFunction(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
if (args.Length() < 2)
return VException("Usage: func = activation_function(layer, neuron) or activation_function(layer, neuron, newfunc)");
int size = sizeof(FANN_ACTIVATIONFUNC_NAMES)/sizeof(char*);
int layer = args[0]->IntegerValue();
int neuron = args[1]->IntegerValue();
if (args.Length() >= 3) {
int num = -1;
if (args[2]->IsString()) {
num = _SeekCharArray(String::Cast(*args[2]), FANN_ACTIVATIONFUNC_NAMES, size, FANN_PREFIX);
} else if (args[2]->IsNumber()) {
num = args[2]->NumberValue();
}
if (num >= 0 && num < size) {
fann_set_activation_function(net->FANN, fann_activationfunc_enum(num), layer, neuron);
}
}
enum fann_activationfunc_enum func = fann_get_activation_function(net->FANN, layer, neuron);
if (func >= 0 && func < size) {
return scope.Close(NormalizeName(FANN_ACTIVATIONFUNC_NAMES[func], FANN_PREFIX, sizeof(FANN_PREFIX)-1));
} else {
return Undefined();
}
}
Handle<Value> NNet::ActivationFunctionHidden(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
int size = sizeof(FANN_ACTIVATIONFUNC_NAMES)/sizeof(char*);
if (args.Length() >= 1) {
int num = -1;
if (args[0]->IsString()) {
num = _SeekCharArray(String::Cast(*args[0]), FANN_ACTIVATIONFUNC_NAMES, size, FANN_PREFIX);
} else if (args[0]->IsNumber()) {
num = args[0]->NumberValue();
}
if (num >= 0 && num < size) {
fann_set_activation_function_hidden(net->FANN, fann_activationfunc_enum(num));
}
}
enum fann_activationfunc_enum func = fann_get_activation_function(net->FANN, 1, 0);
if (func >= 0 && func < size) {
return scope.Close(NormalizeName(FANN_ACTIVATIONFUNC_NAMES[func], FANN_PREFIX, sizeof(FANN_PREFIX)-1));
} else {
return Undefined();
}
}
Handle<Value> NNet::ActivationFunctionOutput(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
int size = sizeof(FANN_ACTIVATIONFUNC_NAMES)/sizeof(char*);
if (args.Length() >= 1) {
int num = -1;
if (args[0]->IsString()) {
num = _SeekCharArray(String::Cast(*args[0]), FANN_ACTIVATIONFUNC_NAMES, size, FANN_PREFIX);
} else if (args[0]->IsNumber()) {
num = args[0]->NumberValue();
}
if (num >= 0 && num < size) {
fann_set_activation_function_output(net->FANN, fann_activationfunc_enum(num));
}
}
enum fann_activationfunc_enum func = fann_get_activation_function(net->FANN, fann_get_num_layers(net->FANN)-1, 0);
if (func >= 0 && func < size) {
return scope.Close(NormalizeName(FANN_ACTIVATIONFUNC_NAMES[func], FANN_PREFIX, sizeof(FANN_PREFIX)-1));
} else {
return Undefined();
}
}
Handle<Value> NNet::GetMse(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
float ret = fann_get_MSE(net->FANN);
return scope.Close(Number::New(ret));
}
Handle<Value> NNet::GetNumInput(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
unsigned int ret = fann_get_num_input(net->FANN);
return scope.Close(Integer::New(ret));
}
Handle<Value> NNet::GetNumOutput(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
unsigned int ret = fann_get_num_output(net->FANN);
return scope.Close(Integer::New(ret));
}
Handle<Value> NNet::GetTotalNeurons(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
unsigned int ret = fann_get_total_neurons(net->FANN);
return scope.Close(Integer::New(ret));
}
Handle<Value> NNet::GetTotalConnections(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
unsigned int ret = fann_get_total_connections(net->FANN);
return scope.Close(Integer::New(ret));
}
Handle<Value> NNet::GetConnectionRate(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
float ret = fann_get_connection_rate(net->FANN);
return scope.Close(Number::New(ret));
}
Handle<Value> NNet::GetNumLayers(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
unsigned int ret = fann_get_num_layers(net->FANN);
return scope.Close(Integer::New(ret));
}
Handle<Value> NNet::GetLayerArray(Local<String> property, const AccessorInfo &info)
{
HandleScope scope;
Local<Object> self = info.Holder();
NNet *net = ObjectWrap::Unwrap<NNet>(self);
return scope.Close(net->GetLayers());
}
Handle<Value> NNet::GetLayerArray(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
return scope.Close(net->GetLayers());
}
Handle<Value> NNet::GetLayers()
{
HandleScope scope;
int size = fann_get_num_layers(FANN);
unsigned int* layers = new unsigned int[size];
fann_get_layer_array(FANN, layers);
Local<Array> result_arr = Array::New();
for (int i=0; i<size; i++) {
result_arr->Set(i, Number::New(layers[i]));
}
delete[] layers;
return scope.Close(result_arr);
}
Handle<Value> NNet::GetBiasArray(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
int size = fann_get_num_layers(net->FANN);
unsigned int* layers = new unsigned int[size];
fann_get_bias_array(net->FANN, layers);
Local<Array> result_arr = Array::New();
for (int i=0; i<size; i++) {
result_arr->Set(i, Number::New(layers[i]));
}
delete[] layers;
return scope.Close(result_arr);
}
Handle<Value> NNet::GetWeights(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
int size = fann_get_total_connections(net->FANN);
struct fann_connection *conns = new struct fann_connection[size];
fann_get_connection_array(net->FANN, conns);
Local<Object> result_object = Object::New();
for (int i=0; i<size; i++) {
Local<Object> obj;
if (!result_object->Has(conns[i].from_neuron)) {
obj = Object::New();
result_object->Set(conns[i].from_neuron, obj);
} else {
obj = Object::Cast(*result_object->Get(conns[i].from_neuron));
}
obj->Set(conns[i].to_neuron, Number::New(conns[i].weight));
}
delete[] conns;
return scope.Close(result_object);
}
Handle<Value> NNet::SetWeightsArr(const Arguments &args)
{
HandleScope scope;
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
if (!args[0]->IsObject())
return VException("First argument should be object");
Local<Array> arg = Array::Cast(*args[0]);
Local<Array> keys = arg->GetOwnPropertyNames();
struct fann_connection *conns = new struct fann_connection[fann_get_total_connections(net->FANN)];
int counter = 0;
for (unsigned i=0; i<keys->Length(); i++) {
Local<Value> idx = keys->Get(i);
if (!arg->Get(idx)->IsObject()) continue;
Local<Object> obj = Object::Cast(*arg->Get(idx));
Local<Array> keys2 = obj->GetOwnPropertyNames();
for (unsigned j=0; j<keys2->Length(); j++) {
conns[counter].from_neuron = idx->IntegerValue();
conns[counter].to_neuron = keys2->Get(j)->IntegerValue();
conns[counter].weight = obj->Get(keys2->Get(j))->NumberValue();
counter++;
}
}
fann_set_weight_array(net->FANN, conns, counter);
delete[] conns;
return Undefined();
}
Handle<Value> NNet::SetWeights(const Arguments &args)
{
HandleScope scope;
if (args[0]->IsObject())
return SetWeightsArr(args);
NNet *net = ObjectWrap::Unwrap<NNet>(args.This());
if (args.Length() < 3)
return VException("Usage: set_weights(new_object) or set_weight(from_neuron, to_neuron, weight)");
unsigned int from_neuron = args[0]->IntegerValue();
unsigned int to_neuron = args[1]->IntegerValue();
fann_type weight = args[2]->NumberValue();
fann_set_weight(net->FANN, from_neuron, to_neuron, weight);
return Undefined();
}