@@ -24,6 +24,7 @@ limitations under the License.
2424using System . Threading ;
2525using Tensorflow . Contexts ;
2626using Tensorflow . Eager ;
27+ using Tensorflow . Graphs ;
2728using Tensorflow . Util ;
2829using static Tensorflow . Binding ;
2930
@@ -101,14 +102,44 @@ public static Graph _get_graph_from_inputs(Tensors op_input_list, Graph graph =
101102 public static Tensor convert_to_tensor ( object value ,
102103 TF_DataType dtype = TF_DataType . DtInvalid ,
103104 string name = null ,
105+ bool as_ref = false ,
104106 TF_DataType preferred_dtype = TF_DataType . DtInvalid ,
105107 Context ctx = null )
106108 {
107- return internal_convert_to_tensor ( value ,
108- dtype : dtype ,
109- name : name ,
110- preferred_dtype : preferred_dtype ,
111- as_ref : false ) ;
109+ if ( dtype == TF_DataType . DtInvalid )
110+ dtype = preferred_dtype ;
111+
112+ if ( value is EagerTensor eager_tensor )
113+ {
114+ if ( tf . executing_eagerly ( ) )
115+ return eager_tensor ;
116+ /*else
117+ {
118+ var graph = get_default_graph();
119+ if (!graph.building_function)
120+ throw new RuntimeError("Attempting to capture an EagerTensor without building a function.");
121+ return (graph as FuncGraph).capture(eager_tensor, name: name);
122+ }*/
123+ }
124+
125+ Tensor ret = value switch
126+ {
127+ NDArray nd => constant_op . constant ( nd , dtype : dtype , name : name ) ,
128+ EagerTensor tensor => tensor . dtype == TF_DataType . TF_RESOURCE
129+ ? tensor . AsPlaceholder ( name : name )
130+ : tensor . AsConstant ( name : name ) ,
131+ Tensor tensor => tensor ,
132+ Tensor [ ] tensors => array_ops . _autopacking_helper ( tensors , dtype , name == null ? "packed" : name ) ,
133+ RefVariable varVal => varVal . _TensorConversionFunction ( dtype : dtype , name : name , as_ref : as_ref ) ,
134+ ResourceVariable varVal => varVal . _TensorConversionFunction ( dtype : dtype , name : name , as_ref : as_ref ) ,
135+ TensorShape ts => constant_op . constant ( ts . dims , dtype : dtype , name : name ) ,
136+ int [ ] dims => constant_op . constant ( dims , dtype : dtype , name : name ) ,
137+ string str => constant_op . constant ( str , dtype : tf . @string , name : name ) ,
138+ object [ ] objects => array_ops . _autopacking_conversion_function ( objects , dtype : dtype , name : name ) ,
139+ _ => constant_op . constant ( value , dtype : dtype , name : name )
140+ } ;
141+
142+ return ret ;
112143 }
113144
114145
@@ -118,9 +149,7 @@ public static Tensor convert_to_tensor_or_composite(Tensor value, TF_DataType dt
118149 }
119150
120151 public static Tensor internal_convert_to_tensor_or_composite ( Tensor value , TF_DataType dtype = TF_DataType . DtInvalid , string name = null , bool as_ref = false )
121- {
122- return internal_convert_to_tensor ( value , dtype : dtype , name : name , as_ref : as_ref ) ;
123- }
152+ => convert_to_tensor ( value , dtype : dtype , name : name , as_ref : as_ref ) ;
124153
125154 /// <summary>
126155 /// Wrapper for `Graph.control_dependencies()` using the default graph.
@@ -460,52 +489,12 @@ public static Tensor[] internal_convert_n_to_tensor(object values, TF_DataType d
460489 foreach ( ( int i , object value ) in enumerate ( values as object [ ] ) )
461490 {
462491 string n = string . IsNullOrEmpty ( name ) ? "" : $ "{ name } _{ i } ";
463- ret . Add ( internal_convert_to_tensor ( value , dtype : dtype , name : n , as_ref : as_ref , preferred_dtype : preferred_dtype ) ) ;
492+ ret . Add ( convert_to_tensor ( value , dtype : dtype , name : n , as_ref : as_ref , preferred_dtype : preferred_dtype ) ) ;
464493 }
465494
466495 return ret . ToArray ( ) ;
467496 }
468497
469- public static Tensor internal_convert_to_tensor ( object value , TF_DataType dtype = TF_DataType . DtInvalid ,
470- string name = null , TF_DataType preferred_dtype = TF_DataType . DtInvalid ,
471- bool as_ref = false ,
472- string scope = null )
473- {
474- if ( dtype == TF_DataType . DtInvalid )
475- dtype = preferred_dtype ;
476-
477- switch ( value )
478- {
479- case NDArray nd :
480- return constant_op . constant ( nd , dtype : dtype , name : name ) ;
481- case EagerTensor tensor :
482- if ( tf . executing_eagerly ( ) )
483- return tensor ;
484- else
485- return tensor . dtype == TF_DataType . TF_RESOURCE
486- ? tensor . AsPlaceholder ( name : name )
487- : tensor . AsContatnt ( name : name ) ;
488- case Tensor tensor :
489- return tensor ;
490- case Tensor [ ] tensors :
491- return array_ops . _autopacking_helper ( tensors , dtype , name == null ? "packed" : name ) ;
492- case RefVariable varVal :
493- return varVal . _TensorConversionFunction ( dtype : dtype , name : name , as_ref : as_ref ) ;
494- case ResourceVariable varVal :
495- return varVal . _TensorConversionFunction ( dtype : dtype , name : name , as_ref : as_ref ) ;
496- case TensorShape ts :
497- return constant_op . constant ( ts . dims , dtype : dtype , name : name ) ;
498- case string str :
499- return constant_op . constant ( value , dtype : tf . @string , name : name ) ;
500- case int [ ] dims :
501- return constant_op . constant ( dims , dtype : dtype , name : name ) ;
502- case object [ ] objects :
503- return array_ops . _autopacking_conversion_function ( objects , dtype : dtype , name : name ) ;
504- default :
505- return constant_op . constant ( value , dtype : dtype , name : name ) ;
506- }
507- }
508-
509498 public static string strip_name_scope ( string name , string export_scope = "" )
510499 {
511500 if ( ! string . IsNullOrEmpty ( export_scope ) )
0 commit comments