-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencog.js
2855 lines (2424 loc) · 77.4 KB
/
encog.js
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Encog(tm) Core v0.1 - Javascript Version
* http://www.heatonresearch.com/encog/
* http://code.google.com/p/encog-java/
* Copyright 2008-2012 Heaton Research, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information on Heaton Research copyrights, licenses
* and trademarks visit:
* http://www.heatonresearch.com/copyright
*/
/**
* The main Encog namespace. This is the only global property created by Encog.
* @type {*}
*/
var ENCOG = ENCOG || {
/**
* The version of Encog that this is.
* @property property
* @type String
* @final
*/
VERSION : '1.0',
/**
* The Encog platform being used.
* @property property
* @type String
* @final
*/
PLATFORM : 'javascript',
/**
* The precision that Encog uses.
* @property precision
* @type String
* @final
*/
precision : 1e-10,
/**
* A newline character.
* @property property
* @type String
* @final
*/
NEWLINE : '\n',
/**
* The Encog type for activation functions.
* @property ENCOG_TYPE_ACTIVATION
* @type String
* @final
*/
ENCOG_TYPE_ACTIVATION : 'ActivationFunction',
/**
* The Encog type for RBF functions.
* @property ENCOG_TYPE_ACTIVATION
* @type String
* @final
*/
ENCOG_TYPE_RBF : 'RBF'
};
/**
* The namespace function, used to define new namespaces.
* @param namespaceString The namespace that is to be defined.
* @method namespace
* @return {Object} The newly created namespace, or existing one.
*/
ENCOG.namespace = function (namespaceString) {
'use strict';
var parts = namespaceString.split('.'),
parent = window,
currentPart = '',
i,
length;
for (i = 0, length = parts.length; i < length; i += 1) {
currentPart = parts[i];
parent[currentPart] = parent[currentPart] || {};
parent = parent[currentPart];
}
return parent;
};
ENCOG.namespace('ENCOG.ActivationSigmoid');
ENCOG.namespace('ENCOG.ActivationTANH');
ENCOG.namespace('ENCOG.ActivationLinear');
ENCOG.namespace('ENCOG.ActivationElliott');
ENCOG.namespace('ENCOG.ActivationElliottSymmetric');
ENCOG.namespace('ENCOG.RadialGaussian');
ENCOG.namespace('ENCOG.RadialMexicanHat');
ENCOG.namespace('ENCOG.Util');
ENCOG.namespace('ENCOG.MathUtil');
ENCOG.namespace('ENCOG.ArrayUtil');
ENCOG.namespace('ENCOG.BasicLayer');
ENCOG.namespace('ENCOG.BasicNetwork');
ENCOG.namespace('ENCOG.PropagationTrainer');
ENCOG.namespace('ENCOG.LinearErrorFunction');
ENCOG.namespace('ENCOG.LinearErrorFunction');
ENCOG.namespace('ENCOG.Swarm');
ENCOG.namespace('ENCOG.Anneal');
ENCOG.namespace('ENCOG.Genetic');
ENCOG.namespace('ENCOG.SOM');
ENCOG.namespace('ENCOG.TrainSOM');
ENCOG.namespace('ENCOG.ReadCSV');
ENCOG.namespace('ENCOG.EGFILE');
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// MathUtil: The following code provides math utilities for Encog //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* The math utilities for Encog.
* @class MathUtil
* @constructor
*/
ENCOG.MathUtil = function () {
'use strict';
};
/**
* Calculate the hyperbolic tangent.
* Unfortunately, Javascript does not have this built in.
* @method tanh
* @param x The value to calculate for.
* @return {Number} The result from the calculation.
*/
ENCOG.MathUtil.tanh = function (x) {
'use strict';
var pos, neg;
pos = Math.exp(x);
neg = Math.exp(-x);
return (pos - neg) / (pos + neg);
};
/**
* Calculate the sign of a number, return 0 for zero,
* 1 for positive, -1 for negative.
* @method sign
* @param x The value to calculate for.
* @return {Number} The result.
*/
ENCOG.MathUtil.sign = function (x) {
'use strict';
if (Math.abs(x) < ENCOG.precision) {
return 0;
} else if (x > 0) {
return 1;
} else {
return -1;
}
};
/**
* Calculate the euclidean distance between a1 and a2. Use the specified starting index and length.
* @param a1 The first array to consider.
* @param a2 The second array to consider.
* @param startIndex The starting index.
* @param len The length.
* @method euclideanDistance
* @return {Number}
*/
ENCOG.MathUtil.euclideanDistance = function (a1, a2, startIndex, len) {
'use strict';
var result = 0, i, diff;
for (i = startIndex; i < (startIndex + len); i += 1) {
diff = a1[i] - a2[i];
result += diff * diff;
}
return Math.sqrt(result);
};
/**
* Determine which multi-dimensional array element, from lst, is the nearest to a1.
* @param a1 A single-dimension array that is searched for in lst.
* @param lst A 2d array that contains arrays with the same length of a1.
* @param k The number of neighbors to find.
* @param maxDist The maximum distance to consider.
* @param startIndex The starting index.
* @param len The length.
* @return {Array} The k elements from lst that were the closest to a1.
*/
ENCOG.MathUtil.kNearest = function (a1, lst, k, maxDist, startIndex, len) {
'use strict';
var result = [], tempDist = [], idx = 0, worstIdx = -1, dist, agent;
while (idx < lst.length) {
agent = lst[idx];
if (a1 !== agent) {
dist = ENCOG.MathUtil.euclideanDistance(a1, agent, startIndex, len);
if (dist < maxDist) {
if (result.length < k) {
result.push(agent);
tempDist.push(dist);
worstIdx = ENCOG.ArrayUtil.arrayMaxIndex(tempDist);
} else {
if (dist < tempDist[worstIdx]) {
tempDist[worstIdx] = dist;
result[worstIdx] = agent;
worstIdx = ENCOG.ArrayUtil.arrayMaxIndex(tempDist);
}
}
}
}
idx += 1;
}
return result;
};
/**
* Generate a random floating point number.
* @param low The first array to consider.
* @param high The second array to consider.
* @method randomFloat
* @return {Number}
*/
ENCOG.MathUtil.randomFloat = function (low, high) {
'use strict';
return (Math.random * (high - low)) + low;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ArrayUtil: The following code provides array utilities for Encog //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* The Encog array utilities.
* @class ArrayUtil
* @constructor
*/
ENCOG.ArrayUtil = function () {
'use strict';
};
/**
* Fill an array with a specific value.
* @method fillArray
* @param arr The array to fill.
* @param start The starting index.
* @param stop The stopping index.
* @param v The value to fill.
*/
ENCOG.ArrayUtil.fillArray = function (arr, start, stop, v) {
'use strict';
var i;
for (i = start; i < stop; i += 1) {
arr[i] = v;
}
};
/**
* Create a new floating point array.
* @param sz The size of the array to create.
* @method newFloatArray
* @return {Array}
*/
ENCOG.ArrayUtil.newFloatArray = function (sz) {
'use strict';
var result;
result = [];
while (sz > 0) {
result.push(0.0);
sz-=1;
}
return result;
};
/**
* Create a new int array.
* @param sz The size of the array to create.
* @method newIntArray
* @return {Array}
*/
ENCOG.ArrayUtil.newIntArray = function (sz) {
'use strict';
var result;
result = [];
while ((sz -= 1) > 0) {
result.push(0);
}
return result;
};
/**
* Fill a 2D array.
* @param arr The size of the array to create.
* @param v The value to fill the array with.
* @method fillArray2D
*/
ENCOG.ArrayUtil.fillArray2D = function (arr, v) {
'use strict';
var i, j, row;
for (i = 0; i < arr.length; i += 1) {
row = arr[i];
for (j = 0; j < row.length; j += 1) {
row[j] = v;
}
}
};
/**
* Randomize an array.
* @param arr The array to randomize.
* @param start The starting index in the array.
* @param stop The stopping index in the array.
* @param low The low-end of the random range.
* @param high The high-end of the random range.
* @method randomizeArray
*/
ENCOG.ArrayUtil.randomizeArray = function (arr, start, stop, low, high) {
'use strict';
var i;
for (i = start; i < stop; i += 1) {
arr[i] = ENCOG.MathUtil.randomFloat(low, high);
}
};
/**
* Randomize a 2D array.
* @param arr The array to randomize.
* @param low The low-end of the random range.
* @param high The high-end of the random range.
* @method randomizeArray2D
*/
ENCOG.ArrayUtil.randomizeArray2D = function (arr, low, high) {
'use strict';
var i, j, row;
for (i = 0; i < arr.length; i += 1) {
row = arr[i];
for (j = 0; j < row.length; j += 1) {
row[j] = ENCOG.MathUtil.randomFloat(low, high);
}
}
};
/**
* Allocate an array of zeros of the specified size.
* @method allocate1D
* @param x The size of the array.
*/
ENCOG.ArrayUtil.allocate1D = function (x) {
'use strict';
var i, result;
result = [];
for (i = 0; i < x; i += 1) {
result[i] = 0;
}
return result;
};
/**
* Allocate a 2D array of booleans.
* @param rows The number of rows.
* @param cols The number of columns.
* @return {Array} The allocated array.
*/
ENCOG.ArrayUtil.allocateBoolean2D = function (rows, cols) {
'use strict';
var result, row, col, temp;
result = [
[]
];
for (row = 0; row < rows; row += 1) {
temp = [];
for (col = 0; col < cols; col += 1) {
temp[col] = false;
}
result[row] = temp;
}
return result;
};
/**
* Copy an array.
* @method arrayCopy
* @param source The source array.
* @param sourceStart The index to start at in the source.
* @param target The target array.
* @param targetStart The target starting index.
* @param count The count of values to copy.
*/
ENCOG.ArrayUtil.arrayCopy = function (source, sourceStart, target, targetStart, count) {
'use strict';
var i;
for (i = 0; i < count; i += 1) {
target[i + targetStart] = source[i + sourceStart];
}
};
/**
* Generate benchmark data. This is a random training set.
* @method generateBenchmarkData
* @param rowCount The number of rows to generate.
* @param colCount The number of columns to generate.
* @return {Array} The resulting array.
*/
ENCOG.ArrayUtil.generateBenchmarkData = function (rowCount, colCount) {
'use strict';
var result, item, row, col;
result = [
[]
];
for (row = 0; row < rowCount; row += 1) {
item = [];
for (col = 0; col < colCount; col += 1) {
item[col] = (Math.random() * 2) - 1;
}
result[row] = item;
}
return result;
};
/**
* Calculate the mean of one dimension in the 2D array a1.
* @method arrayMean
* @param a1 A 2D array.
* @param idx The second dimension in a1 to calculate the mean of.
* @return {Number} The mean of each idx element of a1.
*/
ENCOG.ArrayUtil.arrayMean = function (a1, idx) {
'use strict';
var result, i;
result = 0;
for (i = 0; i < a1.length; i += 1) {
result += a1[i][idx];
}
result /= a1.length;
return result;
};
/**
* Determine the index of the minimum value in an array.
* @method arrayMinIndex
* @param a1 A 1D array.
* @return {Number} Index of the minimum value in the array.
*/
ENCOG.ArrayUtil.arrayMinIndex = function (a1) {
'use strict';
var result, resultIndex, i;
result = Number.MAX_VALUE;
resultIndex = -1;
for (i = 0; i < a1.length; i += 1) {
if (a1[i] < result) {
result = a1[i];
resultIndex = i;
}
}
return resultIndex;
};
/**
* Determine the index of the maximum value in an array.
* @method arrayMinIndex
* @param a1 A 1D array.
* @return {Number} Index of the maximum value in the array.
*/
ENCOG.ArrayUtil.arrayMaxIndex = function (a1) {
'use strict';
var result, resultIndex, i;
result = Number.MIN_VALUE;
resultIndex = -1;
for (i = 0; i < a1.length; i += 1) {
if (a1[i] > result) {
result = a1[i];
resultIndex = i;
}
}
return resultIndex;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Util: The following code provides general utilities for Encog //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Strip leading, and ending, quotes from a string. If the quotes are there.
* @method stripQuotes
* @param s The string to process.
* @return {String} The string, with stripped quotes.
*/
ENCOG.Util.stripQuotes = function (s) {
'use strict';
var l = s.length;
if (s[0] === '\"' || s[0] === '\'') {
s = s.substr(1);
l -= 1;
}
if (s[l - 1] === '\"' || s[l - 1] === '\'') {
s = s.substr(0, l - 1);
}
return s;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Activation Functions: The following code implements activation functions used by Encog. //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* The sigmoid activation function takes on a sigmoidal shape. Only positive
* numbers are generated. Do not use this activation function if negative number
* output is desired.
* @constructor
* @class ActivationSigmoid
*/
ENCOG.ActivationSigmoid = function () {
'use strict';
};
ENCOG.ActivationSigmoid.prototype = {
/**
* The name of this object.
* @property NAME
* @type String
* @final
*/
NAME : "ActivationSigmoid",
/**
* The Encog Type of this object.
* @property encogType
* @type String
* @final
*/
encogType : ENCOG.ENCOG_TYPE_ACTIVATION,
/**
* Calculate the activation function for the specified value.
* @method activationFunction
* @param x An array to calculate the values for.
* @param start The starting point in the array to calculate.
* @param size The size to calculate.
*/
activationFunction : function (x, start, size) {
'use strict';
var i;
for (i = start; i < start + size; i += 1) {
x[i] = 1.0 / (1.0 + Math.exp(-1 * x[i]));
}
},
/**
* Calculate the derivative. For efficiency both the before and after
* activation values are passed in. Many activation derivatives can
* be more efficiently calculated using the value after the regular
* activation is calculated.
* @param b The value before the regular activation was calculated.
* @param a The value after the regular activation was calculated.
* @return {Number} The result.
*/
derivativeFunction : function (b, a) {
'use strict';
return a * (1.0 - a);
}
};
/**
* Create a Sigmoid activation function.
* @method create
* @return {ENCOG.ActivationSigmoid} The newly created activation function.
*/
ENCOG.ActivationSigmoid.create = function () {
'use strict';
return new ENCOG.ActivationSigmoid();
};
/**
* The hyperbolic tangent activation function takes the curved shape of the
* hyperbolic tangent. This activation function produces both positive and
* negative output. Use this activation function if both negative and positive
* output is desired.
* @constructor
* @class ActivationTANH
*/
ENCOG.ActivationTANH = function () {
'use strict';
};
ENCOG.ActivationTANH.prototype = {
/**
* The name of this object.
* @property NAME
* @type String
* @final
*/
NAME : "ActivationTANH",
encogType : ENCOG.ENCOG_TYPE_ACTIVATION,
/**
* Calculate the activation function for the specified value.
* @method activationFunction
* @param x An array to calculate the values for.
* @param start The starting point in the array to calculate.
* @param size The size to calculate.
*/
activationFunction : function (x, start, size) {
'use strict';
var i;
for (i = start; i < start + size; i += 1) {
x[i] = ENCOG.MathUtil.tanh(x[i]);
}
},
/**
* Calculate the derivative. For efficiency both the before and after
* activation values are passed in. Many activation derivatives can
* be more efficiently calculated using the value after the regular
* activation is calculated.
* @param b The value before the regular activation was calculated.
* @param a The value after the regular activation was calculated.
* @return {Number} The result.
*/
derivativeFunction : function (b, a) {
'use strict';
return (1.0 - a * a);
}
};
/**
* Create a TANH activation function.
* @method create
* @return {ENCOG.ActivationTANH} The newly created activation function.
*/
ENCOG.ActivationTANH.create = function () {
'use strict';
return new ENCOG.ActivationTANH();
};
/**
* The Linear layer is really not an activation function at all. The input is
* simply passed on, unmodified, to the output. This activation function is
* primarily theoretical and of little actual use. Usually an activation
* function that scales between 0 and 1 or -1 and 1 should be used.
* @constructor
* @class ActivationLinear
*/
ENCOG.ActivationLinear = function () {
'use strict';
};
ENCOG.ActivationLinear.prototype = {
/**
* The name of this object.
* @property NAME
* @type String
* @final
*/
NAME : "ActivationLinear",
/**
* The encog type of this object.
* @property encogType
* @type String
* @final
*/
encogType : ENCOG.ENCOG_TYPE_ACTIVATION,
/**
* Calculate the activation function for the specified value.
* @method activationFunction
*/
activationFunction : function () {
'use strict';
},
/**
* Calculate the derivative. For efficiency both the before and after
* activation values are passed in. Many activation derivatives can
* be more efficiently calculated using the value after the regular
* activation is calculated.
* @return {Number} The result.
*/
derivativeFunction : function () {
'use strict';
return 1.0;
}
};
/**
* Create a Linear activation function.
* @method create
* @return {ENCOG.ActivationTANH} The newly created activation function.
*/
ENCOG.ActivationLinear.create = function () {
'use strict';
return new ENCOG.ActivationLinear();
};
/**
* Computationally efficient alternative to ActivationSigmoid.
* Its output is in the range [0, 1], and it is derivable.
*
* It will approach the 0 and 1 more slowly than Sigmoid so it
* might be more suitable to classification tasks than predictions tasks.
*
* Elliott, D.L. "A better activation function for artificial neural networks", 1993
* http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.46.7204&rep=rep1&type=pdf
* @constructor
* @class ActivationElliott
*/
ENCOG.ActivationElliott = function () {
'use strict';
};
ENCOG.ActivationElliott.prototype = {
/**
* The name of this object.
* @property NAME
* @type String
* @final
*/
NAME : "ActivationElliott",
/**
* The encog type of this object.
* @property encogType
* @type String
* @final
*/
encogType : ENCOG.ENCOG_TYPE_ACTIVATION,
slope : 1,
/**
* Calculate the activation function for the specified value.
* @method activationFunction
* @param x An array to calculate the values for.
* @param start The starting point in the array to calculate.
* @param size The size to calculate.
*/
activationFunction : function (x, start, size) {
'use strict';
var i;
for (i = start; i < start + size; i += 1) {
x[i] = ((x[i] * this.slope) / 2) / (1 + Math.abs(x[i] * this.slope)) + 0.5;
}
},
/**
* Calculate the derivative. For efficiency both the before and after
* activation values are passed in. Many activation derivatives can
* be more efficiently calculated using the value after the regular
* activation is calculated.
* @param b The value before the regular activation was calculated.
* @param a The value after the regular activation was calculated.
* @return {Number} The result.
*/
derivativeFunction : function (b, a) {
'use strict';
return this.slope / (2.0 * (1.0 + Math.abs(b * this.slope)) * (1 + Math.abs(b * this.slope)));
}
};
/**
* Create a Elliott activation function.
* @method create
* @return {ENCOG.ActivationElliott} The newly created activation function.
*/
ENCOG.ActivationElliott.create = function (s) {
'use strict';
var result = new ENCOG.ActivationElliott();
result.slope = s || 1;
return result;
};
//
/**
* Computationally efficient alternative to ActivationTANH.
* Its output is in the range [-1, 1], and it is derivable.
*
* It will approach the -1 and 1 more slowly than Tanh so it
* might be more suitable to classification tasks than predictions tasks.
*
* Elliott, D.L. "A better activation function for artificial neural networks", 1993
* http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.46.7204&rep=rep1&type=pdf
* @constructor
* @class ActivationElliottSymmetric
*/
ENCOG.ActivationElliottSymmetric = function () {
'use strict';
};
ENCOG.ActivationElliottSymmetric.prototype = {
/**
* The name of this object.
* @property NAME
* @type String
* @final
*/
NAME : "ActivationElliottSymmetric",
/**
* The encog type of this object.
* @property encogType
* @type String
* @final
*/
encogType : ENCOG.ENCOG_TYPE_ACTIVATION,
slope : 1,
/**
* Calculate the activation function for the specified value.
* @method activationFunction
* @param x An array to calculate the values for.
* @param start The starting point in the array to calculate.
* @param size The size to calculate.
*/
activationFunction : function (x, start, size) {
'use strict';
var i;
for (i = start; i < start + size; i += 1) {
x[i] = (x[i] * this.slope) / (1 + Math.abs(x[i] * this.slope));
}
},
/**
* Calculate the derivative. For efficiency both the before and after
* activation values are passed in. Many activation derivatives can
* be more efficiently calculated using the value after the regular
* activation is calculated.
* @param b The value before the regular activation was calculated.
* @param a The value after the regular activation was calculated.
* @return {Number} The result.
*/
derivativeFunction : function (b, a) {
'use strict';
var d = (1.0 + Math.abs(b * this.slope));
return this.slope / (d * d);
}
};
/**
* Create Elliott Symmetric activation function.
* @method create
* @return {ENCOG.ActivationElliottSymmetric} The newly created activation function.
*/
ENCOG.ActivationElliottSymmetric.create = function (s) {
'use strict';
var result = new ENCOG.ActivationElliottSymmetric();
result.slope = s || 1;
return result;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Radial Basis Functions: The following code implements Radial Basis Functions used by Encog //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ENCOG.RadialGaussian = function () {
'use strict';
};
ENCOG.RadialGaussian.prototype = {
/**
* The name of this object.
* @property NAME
* @type String
* @final
*/
NAME : "RadialGaussian",
/**
* The encog type of this object.
* @property encogType
* @type String
* @final
*/
encogType : ENCOG.ENCOG_TYPE_RBF,
center : [],
width : 1,
peak : 1,
/**
* Calculate the activation function for the specified value.
* @method calculate
* @param x An array to calculate the values for.
*/
calculate : function (x) {
'use strict';
var value = 0, i;
for (i = 0; i < this.center.length; i += 1) {
value += Math.pow(x[i] - this.center[i], 2) / (2.0 * this.width * this.width);
}
return this.peak * Math.exp(-value);
}
};
/**
* Create a gaussian RBF.
* @method create
* @return {ENCOG.ActivationElliottSymmetric} The newly created activation function.
*/
ENCOG.RadialGaussian.create = function (thePeak, theCenters, theWidth) {
'use strict';
var result = new ENCOG.RadialGaussian();
result.peak = thePeak || 1;
result.centers = theCenters;
result.width = theWidth || 1;
return result;
};
ENCOG.RadialMexicanHat = function () {
'use strict';
};
ENCOG.RadialMexicanHat.prototype = {
/**
* The name of this object.
* @property NAME
* @type String
* @final
*/
NAME : "RadialMexicanHat",
/**
* The encog type of this object.
* @property encogType
* @type String
* @final
*/
encogType : ENCOG.ENCOG_TYPE_RBF,
center : [],
width : [],
peak : 1,
/**
* Calculate the activation function for the specified value.
* @method calculate
* @param x An array to calculate the values for.
*/
calculate : function (x) {
'use strict';
// calculate the "norm", but don't take square root
// don't square because we are just going to square it
var norm = 0, i;
for (i = 0; i < this.center.length; i += 1) {
norm += Math.pow(x[i] - this.center[i], 2);
}
// calculate the value
return this.peak * (1 - norm) * Math.exp(-norm / 2);
}
};
/**