blob: 33fd58ba20dcffb1004da3f1f550d6c8152124c0 (
plain)
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
|
// Sigmoid
//
// sigmoid - sig
//
// f(x) = ( L / ( 1 + e^( -( 4m * ( x - x_0 ) / L ) ) ) ) + b - ( L / 2 )
//
// m = f'(0)
// b = f(0)
// ( b - ( L / 2 ) ) < y < ( b + ( L / 2 ) )
//
// X_n - 1
// Y_n - 1
// C_n - 3
//
// c[0] = m
// c[1] = L
// c[2] = b
#include <math.h>
#include "ts.h"
typedef struct {
fp_t m;
fp_t L;
fp_t b;
} ts_sigmoid_params_t;
ts_parameter_t const ts_sigmoid_p = { .n_inputs = 1, .n_outputs = 1, .n_constants = 3, .params_size = sizeof(ts_sigmoid_params_t) };
uint_t ts_sigmoid_offset( ts_constant_t const *c )
{
( void )c;
return ( 0 );
}
int ts_sigmoid(
fp_t * const *y, uint_t y_n,
fp_t const * const *x, uint_t x_n,
uint_t n, ts_constant_t const *c
)
{
TS_PROLOGUE( ts_sigmoid_p );
if ( !c ) return -1;
fp_t m = ((ts_sigmoid_params_t const *)c)->m;
fp_t L = ((ts_sigmoid_params_t const *)c)->L;
fp_t b = ((ts_sigmoid_params_t const *)c)->b;
if (L == 0 || isnan(L) || isinf(L) || isnan(m) || isinf(m)) return -1;
m = -( 4.0 * m ) / L;
b = b - ( 0.5 * L );
for( uint_t i = 0; i < n; ++i )
{
fp_t xv = m * x[0][i];
if( xv > 500.0 ) { y[0][i] = b; continue; }
if( xv < -500.0 ) { y[0][i] = L + b; continue; }
y[0][i] = ( L / ( 1.0 + exp( xv ) ) ) + b;
}
return 0;
}
|