@@ -73,8 +73,8 @@ private Tensor _as_graph_element(object obj)
7373 return var . _as_graph_element ( ) ;
7474
7575 return null ;
76- }
77-
76+ }
77+
7878 private ITensorOrOperation _as_graph_element_locked ( object obj , bool allow_tensor = true , bool allow_operation = true )
7979 {
8080 string types_str = "" ;
@@ -99,15 +99,15 @@ private ITensorOrOperation _as_graph_element_locked(object obj, bool allow_tenso
9999 // If obj appears to be a name...
100100 if ( obj is string name )
101101 {
102- if ( name . Contains ( ":" ) && allow_tensor )
102+ if ( name . Contains ( ":" ) && allow_tensor )
103103 {
104104 string op_name = name . Split ( ':' ) [ 0 ] ;
105105 int out_n = int . Parse ( name . Split ( ':' ) [ 1 ] ) ;
106106
107107 if ( _nodes_by_name . ContainsKey ( op_name ) )
108108 return _nodes_by_name [ op_name ] . outputs [ out_n ] ;
109109 }
110- else if ( ! name . Contains ( ":" ) & allow_operation )
110+ else if ( ! name . Contains ( ":" ) & allow_operation )
111111 {
112112 if ( ! _nodes_by_name . ContainsKey ( name ) )
113113 throw new KeyError ( $ "The name { name } refers to an Operation not in the graph.") ;
@@ -166,8 +166,8 @@ private void _check_not_finalized()
166166 throw new RuntimeError ( "Graph is finalized and cannot be modified." ) ;
167167 }
168168
169- public unsafe Operation create_op ( string op_type , Tensor [ ] inputs , TF_DataType [ ] dtypes ,
170- TF_DataType [ ] input_types = null , string name = null ,
169+ public unsafe Operation create_op ( string op_type , Tensor [ ] inputs , TF_DataType [ ] dtypes ,
170+ TF_DataType [ ] input_types = null , string name = null ,
171171 Dictionary < string , AttrValue > attrs = null , OpDef op_def = null )
172172 {
173173 if ( inputs == null )
@@ -188,7 +188,7 @@ public unsafe Operation create_op(string op_type, Tensor[] inputs, TF_DataType[]
188188 var input_ops = inputs . Select ( x => x . op ) . ToArray ( ) ;
189189 var control_inputs = _control_dependencies_for_inputs ( input_ops ) ;
190190
191- var op = new Operation ( node_def ,
191+ var op = new Operation ( node_def ,
192192 this ,
193193 inputs : inputs ,
194194 output_types : dtypes ,
@@ -259,54 +259,61 @@ public string name_scope(string name)
259259 _name_stack = new_stack ;
260260
261261 return String . IsNullOrEmpty ( new_stack ) ? "" : new_stack + "/" ;
262- }
263-
262+ }
263+
264+ /// <summary>
265+ /// Return a unique operation name for `name`.
266+ ///
267+ /// Note: You rarely need to call `unique_name()` directly.Most of
268+ /// the time you just need to create `with g.name_scope()` blocks to
269+ /// generate structured names.
270+ ///
271+ /// `unique_name` is used to generate structured names, separated by
272+ /// `"/"`, to help identify operations when debugging a graph.
273+ /// Operation names are displayed in error messages reported by the
274+ /// TensorFlow runtime, and in various visualization tools such as
275+ /// TensorBoard.
276+ ///
277+ /// If `mark_as_used` is set to `True`, which is the default, a new
278+ /// unique name is created and marked as in use.If it's set to `False`,
279+ /// the unique name is returned without actually being marked as used.
280+ /// This is useful when the caller simply wants to know what the name
281+ /// to be created will be.
282+ /// </summary>
283+ /// <param name="name">The name for an operation.</param>
284+ /// <param name="mark_as_used"> Whether to mark this name as being used.</param>
285+ /// <returns>A string to be passed to `create_op()` that will be used
286+ /// to name the operation being created.</returns>
264287 public string unique_name ( string name , bool mark_as_used = true )
265288 {
266289 if ( ! String . IsNullOrEmpty ( _name_stack ) )
267- {
268290 name = _name_stack + "/" + name ;
269- }
270-
291+ // For the sake of checking for names in use, we treat names as case
292+ // insensitive (e.g. foo = Foo).
271293 var name_key = name . ToLower ( ) ;
272294 int i = 0 ;
273295 if ( _names_in_use . ContainsKey ( name_key ) )
274- {
275- foreach ( var item in _names_in_use )
276- {
277- if ( item . Key == name_key )
278- {
279- i = _names_in_use [ name_key ] ;
280- break ;
281- }
282-
283- i ++ ;
284- }
285- }
286-
296+ i = _names_in_use [ name_key ] ;
297+ // Increment the number for "name_key".
287298 if ( mark_as_used )
288- if ( _names_in_use . ContainsKey ( name_key ) )
289- _names_in_use [ name_key ] ++ ;
290- else
291- _names_in_use [ name_key ] = i + 1 ;
292-
299+ _names_in_use [ name_key ] = i + 1 ;
293300 if ( i > 0 )
294301 {
295- var base_name_key = name_key ;
296-
297302 // Make sure the composed name key is not already used.
298- if ( _names_in_use . ContainsKey ( name_key ) )
303+ var base_name_key = name_key ;
304+ while ( _names_in_use . ContainsKey ( name_key ) )
299305 {
300306 name_key = $ "{ base_name_key } _{ i } ";
301307 i += 1 ;
302308 }
303-
309+ // Mark the composed name_key as used in case someone wants
310+ // to call unique_name("name_1").
304311 if ( mark_as_used )
305312 _names_in_use [ name_key ] = 1 ;
306313
307- name = $ "{ name } _{ i - 1 } ";
314+ // Return the new name with the original capitalization of the given name.
315+ name = $ "{ name } _{ i - 1 } ";
308316 }
309-
310317 return name ;
311318 }
312319
@@ -375,8 +382,8 @@ public void prevent_feeding(Tensor tensor)
375382 public void prevent_fetching ( Operation op )
376383 {
377384 _unfetchable_ops . Add ( op ) ;
378- }
379-
385+ }
386+
380387 public void Dispose ( )
381388 {
382389 c_api . TF_DeleteGraph ( _handle ) ;
@@ -387,8 +394,8 @@ public void __enter__()
387394 }
388395
389396 public void __exit__ ( )
390- {
391-
397+ {
398+
392399 }
393400
394401 public static implicit operator IntPtr( Graph graph )
0 commit comments