6
6
7
7
namespace Tensorflow . Keras
8
8
{
9
- public class Activations
9
+ public class Activations : IActivationsApi
10
10
{
11
11
private static Dictionary < string , Activation > _nameActivationMap ;
12
- private static Dictionary < Activation , string > _activationNameMap ;
13
12
14
- private static Activation _linear = ( features , name ) => features ;
15
- private static Activation _relu = ( features , name )
16
- => tf . Context . ExecuteOp ( "Relu" , name , new ExecuteOpArgs ( features ) ) ;
17
- private static Activation _sigmoid = ( features , name )
18
- => tf . Context . ExecuteOp ( "Sigmoid" , name , new ExecuteOpArgs ( features ) ) ;
19
- private static Activation _softmax = ( features , name )
20
- => tf . Context . ExecuteOp ( "Softmax" , name , new ExecuteOpArgs ( features ) ) ;
21
- private static Activation _tanh = ( features , name )
22
- => tf . Context . ExecuteOp ( "Tanh" , name , new ExecuteOpArgs ( features ) ) ;
23
- private static Activation _mish = ( features , name )
24
- => features * tf . math . tanh ( tf . math . softplus ( features ) ) ;
13
+ private static Activation _linear = new Activation ( )
14
+ {
15
+ Name = "linear" ,
16
+ ActivationFunction = ( features , name ) => features
17
+ } ;
18
+ private static Activation _relu = new Activation ( )
19
+ {
20
+ Name = "relu" ,
21
+ ActivationFunction = ( features , name ) => tf . Context . ExecuteOp ( "Relu" , name , new ExecuteOpArgs ( features ) )
22
+ } ;
23
+ private static Activation _sigmoid = new Activation ( )
24
+ {
25
+ Name = "sigmoid" ,
26
+ ActivationFunction = ( features , name ) => tf . Context . ExecuteOp ( "Sigmoid" , name , new ExecuteOpArgs ( features ) )
27
+ } ;
28
+ private static Activation _softmax = new Activation ( )
29
+ {
30
+ Name = "softmax" ,
31
+ ActivationFunction = ( features , name ) => tf . Context . ExecuteOp ( "Softmax" , name , new ExecuteOpArgs ( features ) )
32
+ } ;
33
+ private static Activation _tanh = new Activation ( )
34
+ {
35
+ Name = "tanh" ,
36
+ ActivationFunction = ( features , name ) => tf . Context . ExecuteOp ( "Tanh" , name , new ExecuteOpArgs ( features ) )
37
+ } ;
38
+ private static Activation _mish = new Activation ( )
39
+ {
40
+ Name = "mish" ,
41
+ ActivationFunction = ( features , name ) => features * tf . math . tanh ( tf . math . softplus ( features ) )
42
+ } ;
25
43
26
44
/// <summary>
27
45
/// Register the name-activation mapping in this static class.
28
46
/// </summary>
29
47
/// <param name="name"></param>
30
48
/// <param name="activation"></param>
31
- private static void RegisterActivation ( string name , Activation activation )
49
+ private static void RegisterActivation ( Activation activation )
32
50
{
33
- _nameActivationMap [ name ] = activation ;
34
- _activationNameMap [ activation ] = name ;
51
+ _nameActivationMap [ activation . Name ] = activation ;
35
52
}
36
53
37
54
static Activations ( )
38
55
{
39
56
_nameActivationMap = new Dictionary < string , Activation > ( ) ;
40
- _activationNameMap = new Dictionary < Activation , string > ( ) ;
41
57
42
- RegisterActivation ( "relu" , _relu ) ;
43
- RegisterActivation ( "linear" , _linear ) ;
44
- RegisterActivation ( "sigmoid" , _sigmoid ) ;
45
- RegisterActivation ( "softmax" , _softmax ) ;
46
- RegisterActivation ( "tanh" , _tanh ) ;
47
- RegisterActivation ( "mish" , _mish ) ;
58
+ RegisterActivation ( _relu ) ;
59
+ RegisterActivation ( _linear ) ;
60
+ RegisterActivation ( _sigmoid ) ;
61
+ RegisterActivation ( _softmax ) ;
62
+ RegisterActivation ( _tanh ) ;
63
+ RegisterActivation ( _mish ) ;
48
64
}
49
65
50
66
public Activation Linear => _linear ;
@@ -59,7 +75,7 @@ static Activations()
59
75
60
76
public Activation Mish => _mish ;
61
77
62
- public static Activation GetActivationByName ( string name )
78
+ public Activation GetActivationFromName ( string name )
63
79
{
64
80
if ( ! _nameActivationMap . TryGetValue ( name , out var res ) )
65
81
{
@@ -70,17 +86,5 @@ public static Activation GetActivationByName(string name)
70
86
return res ;
71
87
}
72
88
}
73
-
74
- public static string GetNameByActivation ( Activation activation )
75
- {
76
- if ( ! _activationNameMap . TryGetValue ( activation , out var name ) )
77
- {
78
- throw new Exception ( $ "Activation { activation } not found") ;
79
- }
80
- else
81
- {
82
- return name ;
83
- }
84
- }
85
89
}
86
90
}
0 commit comments