@@ -15,6 +15,7 @@ limitations under the License.
1515******************************************************************************/
1616
1717using System ;
18+ using System . Collections ;
1819using System . Collections . Generic ;
1920using System . Linq ;
2021using System . Runtime . InteropServices ;
@@ -72,7 +73,7 @@ that are identified by name. For convenience when building a large
7273 all variables that are created during the construction of a graph. The caller
7374 may define additional collections by specifying a new name.
7475 */
75- public partial class Graph : IPython , IDisposable
76+ public partial class Graph : IPython , IDisposable , IEnumerable < Operation >
7677 {
7778 private IntPtr _handle ;
7879 private Dictionary < int , ITensorOrOperation > _nodes_by_id ;
@@ -121,6 +122,10 @@ public Graph(IntPtr handle)
121122 _nodes_by_name = new Dictionary < string , ITensorOrOperation > ( ) ;
122123 _names_in_use = new Dictionary < string , int > ( ) ;
123124 _graph_key = $ "grap-key-{ ops . uid ( ) } /";
125+ }
126+
127+ public void __enter__ ( )
128+ {
124129 }
125130
126131 public ITensorOrOperation as_graph_element ( object obj , bool allow_tensor = true , bool allow_operation = true )
@@ -409,31 +414,6 @@ public TF_Output[] ReturnOutputs(IntPtr results)
409414 return return_outputs ;
410415 }
411416
412- public unsafe Operation [ ] ReturnOperations ( IntPtr results )
413- {
414- TF_Operation return_oper_handle = new TF_Operation ( ) ;
415- int num_return_opers = 0 ;
416- c_api . TF_ImportGraphDefResultsReturnOperations ( results , ref num_return_opers , ref return_oper_handle ) ;
417- Operation [ ] return_opers = new Operation [ num_return_opers ] ;
418- for ( int i = 0 ; i < num_return_opers ; i ++ )
419- {
420- var handle = return_oper_handle . node + Marshal . SizeOf < TF_Operation > ( ) * i ;
421- return_opers [ i ] = new Operation ( * ( IntPtr * ) handle ) ;
422- }
423-
424- return return_opers;
425- }
426-
427- public Operation OperationByName( string operName )
428- {
429- return c_api. TF_GraphOperationByName ( _handle , operName ) ;
430- }
431-
432- public ITensorOrOperation[ ] get_operations ( )
433- {
434- return _nodes_by_name. Values . Select ( x => x ) . ToArray ( ) ;
435- }
436-
437417 public string [ ] get_all_collection_keys ( )
438418 {
439419 return _collections . Keys . Where ( x => ! x . StartsWith ( "__" ) ) . ToArray ( ) ;
@@ -481,17 +461,46 @@ public void Dispose()
481461 public Tensor get_tensor_by_name ( string name )
482462 {
483463 return ( Tensor ) this . as_graph_element ( name , allow_tensor : true , allow_operation : false ) ;
484- }
485-
486- public void __enter__ ( )
487- {
464+ }
465+
466+ public TensorShape GetTensorShape ( TF_Output output )
467+ {
468+ var status = new Status ( ) ;
469+ var ndim = c_api . TF_GraphGetTensorNumDims ( _handle , output , status ) ;
470+ status . Check ( ) ;
471+
472+ if ( ndim == - 1 )
473+ return new TensorShape ( ) ;
474+
475+ var dims = new long [ ndim ] ;
476+ c_api . TF_GraphGetTensorShape ( _handle , output , dims , dims . Length , status ) ;
477+ status . Check ( ) ;
478+
479+ return new TensorShape ( dims . Select ( x => ( int ) x ) . ToArray ( ) ) ;
480+ }
481+
482+ public override string ToString ( )
483+ {
484+ int len = 0 ;
485+ return c_api . TF_GraphDebugString ( _handle , out len ) ;
488486 }
489487
490488 public void __exit__ ( )
491489 {
492490
493- }
491+ }
492+
493+ private IEnumerable < Operation > GetEnumerable ( )
494+ => c_api_util . tf_operations ( this ) ;
494495
496+ IEnumerator < Operation > IEnumerable < Operation > . GetEnumerator ( )
497+ => GetEnumerable ( ) . GetEnumerator ( ) ;
498+
499+ IEnumerator IEnumerable . GetEnumerator ( )
500+ {
501+ throw new NotImplementedException ( ) ;
502+ }
503+
495504 public static implicit operator IntPtr ( Graph graph )
496505 {
497506 return graph . _handle ;
0 commit comments