@@ -14,20 +14,192 @@ You may obtain a copy of the License at
1414 limitations under the License.
1515******************************************************************************/
1616
17+ using System ;
18+ using static Tensorflow . Binding ;
19+
1720namespace Tensorflow . Operations . Activation
1821{
22+ public class sigmoid : IActivation
23+ {
24+ public Tensor Activate ( Tensor x , string name = null )
25+ {
26+ return tf . sigmoid ( x ) ;
27+ }
28+ }
29+
30+ public class tanh : IActivation
31+ {
32+ public Tensor Activate ( Tensor x , string name = null )
33+ {
34+ return tf . tanh ( x ) ;
35+ }
36+ }
37+
38+ public class leakyrelu : IActivation
39+ {
40+ private readonly float _alpha ;
41+
42+ public leakyrelu ( float alpha = 0.3f ) {
43+ _alpha = alpha ;
44+ }
45+
46+ public Tensor Activate ( Tensor x , string name = null )
47+ {
48+ return nn_ops . leaky_relu ( x , _alpha ) ;
49+ }
50+ }
51+
52+ public class elu : IActivation
53+ {
54+ private readonly float _alpha ;
55+
56+ public elu ( float alpha = 0.1f )
57+ {
58+ _alpha = alpha ;
59+ }
60+
61+ public Tensor Activate ( Tensor x , string name = null )
62+ {
63+ var res = gen_ops . elu ( x ) ;
64+ if ( Math . Abs ( _alpha - 0.1f ) < 0.00001f )
65+ {
66+ return res ;
67+ }
68+
69+ return array_ops . @where ( x > 0 , res , _alpha * res ) ;
70+ }
71+ }
72+
73+ public class softmax : IActivation
74+ {
75+ private readonly int _axis ;
76+
77+ /// <summary>Initializes a new instance of the <see cref="T:System.Object"></see> class.</summary>
78+ public softmax ( int axis = - 1 )
79+ {
80+ _axis = axis ;
81+ }
82+
83+ public Tensor Activate ( Tensor x , string name = null )
84+ {
85+ return nn_ops . softmax ( x , _axis ) ;
86+ }
87+ }
88+
89+ public class softplus : IActivation
90+ {
91+ public Tensor Activate ( Tensor x , string name = null )
92+ {
93+ return gen_ops . softplus ( x ) ;
94+ }
95+ }
96+
97+ public class softsign : IActivation
98+ {
99+ public Tensor Activate ( Tensor x , string name = null )
100+ {
101+ return gen_ops . softsign ( x ) ;
102+ }
103+ }
104+
105+ public class linear : IActivation
106+ {
107+ public Tensor Activate ( Tensor x , string name = null )
108+ {
109+ return x ;
110+ }
111+ }
112+
113+
114+ public class exponential : IActivation
115+ {
116+ public Tensor Activate ( Tensor x , string name = null )
117+ {
118+ return tf . exp ( x , name : name ) ;
119+ }
120+ }
121+
122+
19123 public class relu : IActivation
20124 {
21- public Tensor Activate ( Tensor features , string name = null )
125+ private readonly float _threshold ;
126+ private readonly float _alpha ;
127+ private readonly float ? _maxValue ;
128+
129+ public relu ( float threshold = 0f , float alpha = 0.2f , float ? max_value = null )
130+ {
131+ _threshold = threshold ;
132+ _alpha = alpha ;
133+ _maxValue = max_value ;
134+ }
135+
136+ public Tensor Activate ( Tensor x , string name = null )
22137 {
23- OpDefLibrary _op_def_lib = new OpDefLibrary ( ) ;
138+ //based on keras/backend.py
139+ if ( Math . Abs ( _alpha ) > 0.000001f )
140+ {
141+ if ( ! _maxValue . HasValue && Math . Abs ( _threshold ) < 0.0001 )
142+ {
143+ return nn_ops . leaky_relu ( x , _alpha ) ;
144+ }
145+ }
146+
147+ Tensor negative_part ;
148+ if ( Math . Abs ( _threshold ) > 0.000001f )
149+ {
150+ negative_part = gen_ops . relu ( - x + _threshold ) ;
151+ } else
152+ {
153+ negative_part = gen_ops . relu ( - x + _threshold ) ;
154+ }
155+
156+ if ( Math . Abs ( _threshold ) > 0.000001f )
157+ {
158+ x = x * math_ops . cast ( tf . greater ( x , _threshold ) , TF_DataType . TF_FLOAT ) ;
159+ } else if ( Math . Abs ( _maxValue . Value - 6f ) < 0.0001f )
160+ {
161+ x = gen_ops . relu6 ( x ) ;
162+ } else
163+ {
164+ x = gen_ops . relu ( x ) ;
165+ }
166+
167+ bool clip_max = _maxValue . HasValue ;
168+ if ( clip_max )
169+ {
170+ Tensor maxval = constant_op . constant ( _maxValue , x . dtype . as_base_dtype ( ) ) ;
171+ var zero = constant_op . constant ( 0.0f , x . dtype . as_base_dtype ( ) ) ;
172+ x = gen_ops . clip_by_value ( x , zero , maxval ) ;
173+ }
24174
25- var _op = _op_def_lib . _apply_op_helper ( "Relu" , name : name , args : new
175+ if ( Math . Abs ( _alpha ) > 0.00001 )
26176 {
27- features
28- } ) ;
177+ var a = constant_op . constant ( _alpha , x . dtype . as_base_dtype ( ) ) ;
178+ x -= a * negative_part ;
179+ }
29180
30- return _op . outputs [ 0 ] ;
181+ return x ;
182+ }
183+ }
184+
185+ public class selu : IActivation
186+ {
187+ public Tensor Activate ( Tensor x , string name = null )
188+ {
189+ const float alpha = 1.6732632423543772848170429916717f ;
190+ const float scale = 1.0507009873554804934193349852946f ;
191+ return scale * new elu ( alpha ) . Activate ( x , name ) ;
192+ }
193+ }
194+
195+ public class hard_sigmoid : IActivation
196+ {
197+ public Tensor Activate ( Tensor x , string name = null )
198+ {
199+ x = ( 0.2 * x ) + 0.5 ;
200+ var zero = tf . convert_to_tensor ( 0.0f , x . dtype . as_base_dtype ( ) ) ;
201+ var one = tf . convert_to_tensor ( 1.0f , x . dtype . as_base_dtype ( ) ) ;
202+ return tf . clip_by_value ( x , zero , one ) ;
31203 }
32204 }
33- }
205+ }
0 commit comments