-
Notifications
You must be signed in to change notification settings - Fork 1
/
NewDann.Formula.pas
482 lines (422 loc) · 13.7 KB
/
NewDann.Formula.pas
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
// NewDann - Project
// Author : Jens Biermann, Linsburg
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
unit NewDann.Formula;
interface
uses
System.SysUtils, System.Math, System.Generics.Collections;
type
TThresholdType = (funcLINEAR, funcSIGMOID, funcGAUSSIAN, funcBinaryStep, functanH, funcArcTan, funcArcSinH, funcELLIOT, funcSIN,
funcCOS, funcRELU, funcLEAKY_RELU, funcSoftPlus);
TthresholdFunc = reference to function(const InputValue: Single): Single;
TthresholdDeriveFunc = reference to function(const OutputValue, InputValue: Single): Single;
ThresholdTypeHelper = record helper for TThresholdType
private
function NameToType(Name: string): TThresholdType;
public
constructor Create(Name: string);
function ToFunction: TthresholdFunc;
function ToDeriveFunction: TthresholdDeriveFunc;
function ToName: string;
class function ToArrayStr: TArray<string>; static;
end;
function SameThresholdType(ThresholdTypeArray: TArray<TThresholdType>; var ThresholdType: TThresholdType): Boolean;
// https://en.wikipedia.org/wiki/Activation_function#ref_heaviside
function threshold_Linear(const InputValue: Single): Single; inline;
function threshold_Sigmoid(const InputValue: Single): Single; inline;
function threshold_tanH(const InputValue: Single): Single; inline;
function threshold_ArcTan(const InputValue: Single): Single; inline;
function threshold_ArcSinH(const InputValue: Single): Single; inline;
function threshold_ElliotSig(const InputValue: Single): Single; inline;
function threshold_ReLU(const InputValue: Single): Single; inline;
function threshold_LeakyReLU(const InputValue: Single): Single; inline;
function threshold_SoftPlus(const InputValue: Single): Single; inline;
function threshold_Sin(const InputValue: Single): Single; inline;
function threshold_Cos(const InputValue: Single): Single; inline;
function threshold_Gaussian(const InputValue: Single): Single; inline;
function threshold_BinaryStep(const InputValue: Single): Single; inline;
function thresholdDerive_Linear(const OutputValue, InputValue: Single): Single; inline;
function thresholdDerive_Sigmoid(const OutputValue, InputValue: Single): Single; inline;
function thresholdDerive_tanH(const OutputValue, InputValue: Single): Single; inline;
function thresholdDerive_ArcTan(const OutputValue, InputValue: Single): Single; inline;
function thresholdDerive_ArcSinH(const OutputValue, InputValue: Single): Single; inline;
function thresholdDerive_ElliotSig(const OutputValue, InputValue: Single): Single; inline;
function thresholdDerive_ReLU(const OutputValue, InputValue: Single): Single; inline;
function thresholdDerive_LeakyReLU(const OutputValue, InputValue: Single): Single; inline;
function thresholdDerive_SoftPlus(const OutputValue, InputValue: Single): Single; inline;
function thresholdDerive_Sin(const OutputValue, InputValue: Single): Single; inline;
function thresholdDerive_Cos(const OutputValue, InputValue: Single): Single; inline;
function thresholdDerive_Gaussian(const OutputValue, InputValue: Single): Single; inline;
function thresholdDerive_BinaryStep(const OutputValue, InputValue: Single): Single; inline;
function _Sign(const AValue: Single; Epsilon: Single = 0): TValueSign; inline;
function SliceOf(Q: TArray<Single>; SetNum, Count: Integer): TArray<Single>; inline;
function RandomRangeF(const min, max: Single): Single; inline;
function RandomUniqueIntegerList(ToInt, Count: Integer): TArray<Integer>; overload; inline;
function RandomUniqueIntegerList(ToInt: Integer; Rate: Single): TArray<Integer>; overload; inline;
type
TErrorFunc = reference to function(const OutValues, ExpectedValues: TArray<Single>): Single;
TWeightErrorFunc = reference to function(const Weights: TArray<Single>): Single;
function loss_MSE(const OutValues, ExpectedValues: TArray<Single>): Single; inline;
function loss_MAE(const OutValues, ExpectedValues: TArray<Single>): Single; inline;
function loss_RMSE(const OutValues, ExpectedValues: TArray<Single>): Single; inline;
function Weight_Error1(const Weights: TArray<Single>): Single;
function Weight_Error2(const Weights: TArray<Single>): Single;
implementation
function threshold_Linear(const InputValue: Single): Single;
begin
Result := InputValue;
end;
function thresholdDerive_Linear(const OutputValue, InputValue: Single): Single;
begin
Result := 1;
end;
function threshold_Sigmoid(const InputValue: Single): Single;
begin
Result := 1 / (1 + Exp(-InputValue));
end;
function thresholdDerive_Sigmoid(const OutputValue, InputValue: Single): Single;
begin
Result := OutputValue * (1 - OutputValue);
end;
function threshold_tanH(const InputValue: Single): Single;
begin
Result := Tanh(InputValue)
end;
function thresholdDerive_tanH(const OutputValue, InputValue: Single): Single;
begin
Result := 1 - OutputValue * OutputValue;
end;
function threshold_ArcTan(const InputValue: Single): Single;
begin
Result := ArcTan(InputValue);
end;
function thresholdDerive_ArcTan(const OutputValue, InputValue: Single): Single;
begin
Result := 1 / (InputValue * InputValue + 1);
end;
function threshold_ArcSinH(const InputValue: Single): Single;
begin
Result := ArcSinh(InputValue);
end;
function thresholdDerive_ArcSinH(const OutputValue, InputValue: Single): Single;
begin
Result := 1 / Sqrt(InputValue * InputValue + 1);
end;
function threshold_ElliotSig(const InputValue: Single): Single;
begin
Result := InputValue / (1 + Abs(InputValue));
end;
function thresholdDerive_ElliotSig(const OutputValue, InputValue: Single): Single;
begin
Result := 1 / Sqr(1 + Abs(InputValue));
end;
function threshold_ReLU(const InputValue: Single): Single;
begin
Result := Max(InputValue, 0);
end;
function thresholdDerive_ReLU(const OutputValue, InputValue: Single): Single;
begin
Result := IfThen(InputValue > 0, 1, 0)
end;
function threshold_LeakyReLU(const InputValue: Single): Single;
begin
if InputValue < 0 then
Result := 0.01 * InputValue
else
Result := InputValue;
end;
function thresholdDerive_LeakyReLU(const OutputValue, InputValue: Single): Single;
begin
if InputValue < 0 then
Result := 0.01
else
Result := 1;
end;
function threshold_SoftPlus(const InputValue: Single): Single;
begin
Result := Ln(1 + Exp(InputValue));
end;
function thresholdDerive_SoftPlus(const OutputValue, InputValue: Single): Single;
begin
Result := 1 / (1 + Exp(-InputValue));
end;
function threshold_Sin(const InputValue: Single): Single;
begin
Result := Sin(InputValue);
end;
function thresholdDerive_Sin(const OutputValue, InputValue: Single): Single;
begin
Result := Cos(InputValue);
end;
function threshold_Cos(const InputValue: Single): Single;
begin
Result := Cos(InputValue);
end;
function thresholdDerive_Cos(const OutputValue, InputValue: Single): Single;
begin
Result := -Sin(InputValue);
end;
function threshold_Gaussian(const InputValue: Single): Single;
begin
Result := Exp(-InputValue * InputValue);
end;
function thresholdDerive_Gaussian(const OutputValue, InputValue: Single): Single;
begin
Result := -2 * InputValue * OutputValue
end;
function threshold_BinaryStep(const InputValue: Single): Single;
begin
Result := IfThen(InputValue >= 0, 1, 0);
end;
function thresholdDerive_BinaryStep(const OutputValue, InputValue: Single): Single;
begin
Result := 0;
end;
function _Sign(const AValue: Single; Epsilon: Single = 0): TValueSign;
begin
if IsZero(AValue, Epsilon) then
Result := ZeroValue
else if (PInteger(@AValue)^ and $80000000) = $80000000 then
Result := NegativeValue
else
Result := PositiveValue;
end;
function SliceOf(Q: TArray<Single>; SetNum, Count: Integer): TArray<Single>;
var
i: Integer;
begin
SetLength(Result, Count);
for i := 0 to Count - 1 do
Result[i] := Q[SetNum * Count + i];
end;
function RandomRangeF(const min, max: Single): Single;
begin
Result := min + Random * (max - min);
end;
function RandomUniqueIntegerList(ToInt, Count: Integer): TArray<Integer>;
var
A: TList<Integer>;
L: TList<Integer>;
i, x: Integer;
begin
Randomize;
L := TList<Integer>.Create;
A := TList<Integer>.Create;
try
for i := 0 to ToInt do
A.Add(i);
repeat
x := Random(A.Count);
if not L.Contains(A[x]) then
L.Add(A[x]);
until L.Count = Count;
L.Sort;
Result := L.ToArray;
finally
A.Free;
L.Free;
end;
end;
function RandomUniqueIntegerList(ToInt: Integer; Rate: Single): TArray<Integer>;
begin
Result := RandomUniqueIntegerList(ToInt, Integer(Round((ToInt + 1) * Rate)));
end;
function SameThresholdType(ThresholdTypeArray: TArray<TThresholdType>; var ThresholdType: TThresholdType): Boolean;
var
i: Integer;
begin
Result := Length(ThresholdTypeArray) > 0;
if Result then
begin
ThresholdType := ThresholdTypeArray[0];
for i := 1 to Length(ThresholdTypeArray) - 1 do
if ThresholdTypeArray[i] <> ThresholdType then
Exit(False);
end;
end;
function loss_MSE(const OutValues, ExpectedValues: TArray<Single>): Single;
// Mean Square Error
var
i: Integer;
d: Single;
MSE: Single;
begin
MSE := 0;
for i := 0 to Length(OutValues) - 1 do
begin
d := ExpectedValues[i] - OutValues[i];
MSE := MSE + d * d;
end;
Result := MSE / Length(OutValues);
end;
function loss_MAE(const OutValues, ExpectedValues: TArray<Single>): Single;
// Mean Absolute Error
var
i: Integer;
d: Single;
MSE: Single;
begin
MSE := 0;
for i := 0 to Length(OutValues) - 1 do
begin
d := ExpectedValues[i] - OutValues[i];
MSE := MSE + d;
end;
Result := MSE / Length(OutValues);
end;
function loss_RMSE(const OutValues, ExpectedValues: TArray<Single>): Single;
// Root Mean Square Error
begin
Result := Sqrt(loss_MSE(OutValues, ExpectedValues));
end;
function Weight_Error1(const Weights: TArray<Single>): Single;
var
W: Single;
begin
Result := 0;
for W in Weights do
Result := Result + W * W / (1 + W * W);
end;
function Weight_Error2(const Weights: TArray<Single>): Single;
var
W: Single;
begin
Result := 0;
for W in Weights do
Result := Result + W * W;
end;
{ ThresholdTypeHelper }
constructor ThresholdTypeHelper.Create(Name: string);
begin
Self := NameToType(Name);
end;
function ThresholdTypeHelper.NameToType(Name: string): TThresholdType;
var
Func: TThresholdType;
s: string;
begin
for Func := Low(TThresholdType) to High(TThresholdType) do
begin
s := Func.ToName;
if SameText(s, Name) then
Exit(Func);
end;
raise Exception.Create('Error: -> ThresholdTypeHelper.NameToType : ' + Name);
end;
class function ThresholdTypeHelper.ToArrayStr: TArray<string>;
var
L: TList<string>;
iTreshold: TThresholdType;
begin
L := TList<string>.Create;
try
for iTreshold := Low(TThresholdType) to High(TThresholdType) do
L.Add(iTreshold.ToName);
Result := L.ToArray;
finally
L.Free;
end;
end;
function ThresholdTypeHelper.ToDeriveFunction: TthresholdDeriveFunc;
begin
case Self of
funcLINEAR:
Result := thresholdDerive_Linear;
funcSIGMOID:
Result := thresholdDerive_Sigmoid;
functanH:
Result := thresholdDerive_TanH;
funcGAUSSIAN:
Result := thresholdDerive_GAUSSIAN;
funcBinaryStep:
Result := thresholdDerive_BinaryStep;
funcELLIOT:
Result := thresholdDerive_ElliotSig;
funcSIN:
Result := thresholdDerive_Sin;
funcCOS:
Result := thresholdDerive_Cos;
funcArcTan:
Result := thresholdDerive_ArcTan;
funcArcSinH:
Result := thresholdDerive_ArcSinH;
funcRELU:
Result := thresholdDerive_ReLU;
funcLEAKY_RELU:
Result := thresholdDerive_LeakyReLU;
funcSoftPlus:
Result := thresholdDerive_SoftPlus;
else
raise Exception.Create('Error: ThresholdTypeHelper.ToDeriveFunction');
end;
end;
function ThresholdTypeHelper.ToFunction: TthresholdFunc;
begin
case Self of
funcLINEAR:
Result := threshold_Linear;
funcSIGMOID:
Result := threshold_Sigmoid;
functanH:
Result := threshold_TanH;
funcGAUSSIAN:
Result := threshold_GAUSSIAN;
funcBinaryStep:
Result := threshold_BinaryStep;
funcELLIOT:
Result := threshold_ElliotSig;
funcSIN:
Result := threshold_Sin;
funcCOS:
Result := threshold_Cos;
funcArcTan:
Result := threshold_ArcTan;
funcArcSinH:
Result := threshold_ArcSinH;
funcRELU:
Result := threshold_ReLU;
funcLEAKY_RELU:
Result := threshold_LeakyReLU;
funcSoftPlus:
Result := threshold_SoftPlus;
else
raise Exception.Create('Error: ThresholdTypeHelper.ToFunction');
end;
end;
function ThresholdTypeHelper.ToName: string;
begin
case Self of
funcLINEAR:
Result := 'Linear';
funcSIGMOID:
Result := 'Sigmoid';
functanH:
Result := 'TanH';
funcGAUSSIAN:
Result := 'GAUSSIAN';
funcBinaryStep:
Result := 'BinaryStep';
funcELLIOT:
Result := 'ElliotSig';
funcSIN:
Result := 'Sin';
funcCOS:
Result := 'Cos';
funcArcTan:
Result := 'ArcTan';
funcArcSinH:
Result := 'ArcSinH';
funcRELU:
Result := 'ReLU';
funcLEAKY_RELU:
Result := 'LeakyReLU';
funcSoftPlus:
Result := 'SoftPlus';
else
raise Exception.Create('Error: ThresholdTypeHelper.ToName');
end;
end;
end.