Salome HOME
'/usr/bin/env python' -> '/usr/bin/env python3'
[modules/yacs.git] / src / pmml / Test / samples / unittest_ref_ann_model.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 from math import tanh, exp
5
6 def ActivationFunction(sum): 
7     return ( 1.0 / ( 1.0 + exp( -1.0 * sum ) ) ); 
8
9 def myTestFunc(param):
10
11     ############################## 
12     #
13     # File used by unit test
14     # PMMLBasicsTest1::testExportNeuralNetworkPython
15     #
16     ############################## 
17
18     nInput = 8;
19     nOutput = 1;
20     nHidden = 1;
21     nNeurones = 10;
22     myTestFunc_act = [];
23     res = [];
24
25     # --- Preprocessing of the inputs and outputs
26     myTestFunc_minInput = [
27       0.099999, 25048.9, 89334.9, 89.5523, 1050, 
28     760.001, 1400.02, 10950, 
29     ];
30     myTestFunc_minOutput = [
31         77.8117
32     ];
33     myTestFunc_maxInput = [
34     0.028899, 14419.8, 15180.8, 15.2866, 34.6793, 
35     34.6718, 161.826, 632.913, 
36     ];
37     myTestFunc_maxOutput = [
38         45.7061
39     ];
40     # --- Values of the weights
41     myTestFunc_valW = [
42     -1.74548, 6.96551, -1.26357, 0.753663, 0.00165366, 
43     0.004725, 0.00996979, 0.178798, -0.180981, -0.173569, 
44     0.0855967, 
45     ];
46     # --- Constants
47     indNeurone = 0;
48
49     # --- Input Layers
50     for i in range(nInput) :
51         myTestFunc_act.append( ( param[i] - myTestFunc_minInput[i] ) / myTestFunc_maxInput[i] ) ;
52         indNeurone += 1 ;
53         pass
54
55     # --- Hidden Layers
56     for member in range(nHidden):
57         CrtW = member * ( nInput + 2) + 2;
58         sum = myTestFunc_valW[CrtW];
59         CrtW += 1 ;
60         for source in range(nInput) :
61             sum += myTestFunc_act[source] * myTestFunc_valW[CrtW];
62             CrtW += 1 ;
63             pass
64         myTestFunc_act.append( ActivationFunction(sum) ) ;
65         indNeurone += 1 ;
66         pass
67
68     # --- Output
69     for member in range(nOutput):
70         sum = myTestFunc_valW[0];
71         for source in range(nHidden):
72             CrtW = source * ( nInput + 2) + 1;
73             sum += myTestFunc_act[nInput+source] * myTestFunc_valW[CrtW];
74             pass
75         myTestFunc_act.append( sum );
76         indNeurone += 1 ;
77         res.append( myTestFunc_minOutput[member] + myTestFunc_maxOutput[member] * sum );
78         pass
79
80     return res;
81
82