11using System ;
22using System . Collections . Generic ;
33using System . Text ;
4+ using System . Runtime . CompilerServices ;
5+ using static Tensorflow . Python ;
46
57namespace Tensorflow . Keras
68{
7- public class backend
9+ public class backend : BackendBase
810 {
11+ /* ---------------------------------------- KERAS BACKEND NATIVE OBJECTS ---------------------------------------- */
12+ public static Func < Array , double > py_sum = sum ;
13+ public static Func < Array , bool > py_all = all ;
14+ //Func<Array, bool> py_any = any;
15+ //Func<double, double, double, IEnumerable<double>> py_slice = slice;
16+
17+ public static Session _SESSION = Tensorflow . tf . defaultSession ;
18+ public static Graph _GRAPH = null ;
19+ public static Dictionary < Graph , GraphLearningPhase > _GRAPH_LEARNING_PHASES ;
20+ //Dictionary<Graph, Dictionary<string, int>> PER_GRAPH_LAYER_NAME_UIDS;
21+ public static bool _MANUAL_VAR_INIT = false ;
22+ public static List < string > _LOCAL_DEVICES = null ;
23+ /* -------------------------------------- KERAS BACKEND NATIVE OBJECTS END -------------------------------------- */
24+
925 /// <summary>
1026 /// A global dictionary mapping graph objects to an index of counters used
1127 /// for various layer names in each graph.
1228 /// Allows to give unique autogenerated names to layers, in a graph-specific way.
1329 /// </summary>
14- public static Dictionary < string , Dictionary < ( string , string ) , int > > PER_GRAPH_LAYER_NAME_UIDS = new Dictionary < string , Dictionary < ( string , string ) , int > > ( ) ;
30+ public static Dictionary < Graph , Dictionary < ( string , string ) , int > > PER_GRAPH_LAYER_NAME_UIDS = new Dictionary < Graph , Dictionary < ( string , string ) , int > > ( ) ;
1531 public static Dictionary < string , RefVariable > _GRAPH_VARIABLES = new Dictionary < string , RefVariable > ( ) ;
32+ public static Dictionary < string , Optimizer > _GRAPH_TF_OPTIMIZERS = new Dictionary < string , Optimizer > ( ) ;
33+
34+ public static _DummyEagerGraph _DUMMY_EAGER_GRAPH = new _DummyEagerGraph ( ) ;
35+
1636 public static void track_variable ( RefVariable v )
1737 {
1838 var graph = v . graph ;
1939 _GRAPH_VARIABLES [ graph . graph_key ] = v ;
2040 }
2141
22- public static Tensor placeholder ( int [ ] shape = null ,
23- int ndim = - 1 ,
24- TF_DataType dtype = TF_DataType . DtInvalid ,
25- bool sparse = false ,
42+ public static Tensor placeholder ( int [ ] shape = null ,
43+ int ndim = - 1 ,
44+ TF_DataType dtype = TF_DataType . DtInvalid ,
45+ bool sparse = false ,
2646 string name = null )
2747 {
28- if ( sparse )
48+ if ( sparse )
2949 {
3050 throw new NotImplementedException ( "placeholder sparse is true" ) ;
3151 }
@@ -39,5 +59,56 @@ public static Graph get_graph()
3959 {
4060 return ops . get_default_graph ( ) ;
4161 }
62+
63+ public static int get_uid ( string prefix , string @namespace = "" )
64+ {
65+ var graph = tf . get_default_graph ( ) ;
66+ if ( ! PER_GRAPH_LAYER_NAME_UIDS . ContainsKey ( graph ) )
67+ PER_GRAPH_LAYER_NAME_UIDS . Add ( graph , new defaultdict < ( string , string ) , int > ( ) ) ;
68+ PER_GRAPH_LAYER_NAME_UIDS [ graph ] [ ( @namespace , prefix ) ] += 1 ;
69+
70+ return PER_GRAPH_LAYER_NAME_UIDS [ graph ] [ ( @namespace , prefix ) ] ;
71+ }
72+ public static int get_uid ( ( string , string ) name )
73+ {
74+ var graph = tf . get_default_graph ( ) ;
75+ if ( ! PER_GRAPH_LAYER_NAME_UIDS . ContainsKey ( graph ) )
76+ PER_GRAPH_LAYER_NAME_UIDS . Add ( graph , new defaultdict < ( string , string ) , int > ( ) ) ;
77+ PER_GRAPH_LAYER_NAME_UIDS [ graph ] [ ( name ) ] += 1 ;
78+
79+ return PER_GRAPH_LAYER_NAME_UIDS [ graph ] [ name ] ;
80+ }
81+ public static void reset_uids ( ) => PER_GRAPH_LAYER_NAME_UIDS = new Dictionary < Graph , Dictionary < ( string , string ) , int > > ( ) ;
82+ public static void clear_session ( )
83+ {
84+ ops . reset_default_graph ( ) ;
85+ reset_uids ( ) ;
86+ _SESSION = null ;
87+ var phase = tf . placeholder_with_default ( false , new int [ ] { } , name : "keras_learning_phase" ) ;
88+ _GRAPH_LEARNING_PHASES = new Dictionary < Graph , GraphLearningPhase > ( ) ;
89+ _GRAPH_LEARNING_PHASES [ tf . get_default_graph ( ) ] = 0 ;
90+ }
91+ public static void manual_variable_initialization ( bool value )
92+ {
93+ _MANUAL_VAR_INIT = value ;
94+ }
95+ public static GraphLearningPhase learning_phase ( )
96+ {
97+ var graph = tf . get_default_graph ( ) ;
98+ if ( _GRAPH_LEARNING_PHASES . ContainsKey ( graph ) )
99+ {
100+ var phase = tf . placeholder_with_default ( false , shape : new int [ ] { } , name : "keras_learning_phase" ) ;
101+ _GRAPH_LEARNING_PHASES [ graph ] = 0 ;
102+ }
103+ return _GRAPH_LEARNING_PHASES [ graph ] ;
104+ }
105+ public static void set_learning_phase ( bool value )
106+ {
107+ _GRAPH_LEARNING_PHASES [ tf . get_default_graph ( ) ] = ( GraphLearningPhase ) ( ( value ) ? 1 : 0 ) ;
108+ }
109+
110+
111+ public class _DummyEagerGraph
112+ { }
42113 }
43114}
0 commit comments