@@ -37,68 +37,14 @@ public class constant_op
3737 /// <param name="shape">Optional dimensions of resulting tensor.</param>
3838 /// <param name="name">Optional name for the tensor.</param>
3939 /// <returns></returns>
40- public static Tensor constant ( object value , TF_DataType dtype = TF_DataType . DtInvalid , int [ ] shape = null , string name = "Const" )
40+ public static Tensor constant ( object value , TF_DataType dtype = TF_DataType . DtInvalid ,
41+ int [ ] shape = null , bool verify_shape = false ,
42+ bool allow_broadcast = true , string name = "Const" )
4143 {
42- return _constant_impl ( value , dtype , shape , name , verify_shape : false , allow_broadcast : true ) ;
43- }
44-
45- /// <param name="verify_shape">Boolean that enables verification of a shape of values.</param>
46- public static Tensor _constant_impl ( object value ,
47- TF_DataType dtype ,
48- TensorShape shape ,
49- string name ,
50- bool verify_shape ,
51- bool allow_broadcast )
52- {
53- if ( tf . Context . executing_eagerly ( ) )
54- {
55- var t = convert_to_eager_tensor ( value , tf . Context , dtype : dtype ) ;
56- if ( shape == null )
57- return t ;
58-
59- if ( t . shape . Select ( x => Convert . ToInt64 ( x ) ) . SequenceEqual ( shape . dims ) )
60- return t ;
61-
62- if ( verify_shape )
63- throw new TypeError ( $ "Expected Tensor's shape: { shape } , got { t . shape } .") ;
64-
65- var num_t = t . TensorShape . num_elements ( ) ;
66- if ( num_t == shape . num_elements ( ) )
67- return _eager_reshape ( t , shape , tf . Context ) ;
68- if ( num_t == 1 )
69- {
70- if ( t . dtype == dtypes . @bool )
71- throw new NotImplementedException ( "" ) ;
72- else
73- return _eager_fill ( shape , t , tf . Context ) ;
74- }
75- }
76-
77- // graph mode
78- Graph g = ops . get_default_graph ( ) ;
79- var tensor_value = new AttrValue ( ) ;
80- tensor_value . Tensor = tensor_util . make_tensor_proto ( value ,
81- dtype : dtype ,
82- shape : shape ,
83- verify_shape : verify_shape ,
84- allow_broadcast : allow_broadcast ) ;
85-
86- var dtype_value = new AttrValue
87- {
88- Type = tensor_value . Tensor . Dtype ,
89- } ;
90-
91- var attrs = new Dictionary < string , AttrValue > ( ) ;
92- attrs [ "value" ] = tensor_value ;
93- attrs [ "dtype" ] = dtype_value ;
94-
95- var op = g . create_op ( "Const" ,
96- new Tensor [ 0 ] ,
97- new TF_DataType [ ] { dtype_value . Type . as_tf_dtype ( ) } ,
98- attrs : attrs ,
99- name : name ) ;
100-
101- return op . outputs [ 0 ] ;
44+ if ( tf . executing_eagerly ( ) )
45+ return convert_to_eager_tensor ( value , dtype , shape , name , verify_shape : verify_shape , allow_broadcast : allow_broadcast ) ;
46+ else
47+ return convert_to_graph_tensor ( value , dtype , shape , name , verify_shape : verify_shape , allow_broadcast : allow_broadcast ) ;
10248 }
10349
10450 private static Tensor _eager_reshape ( Tensor tensor , int [ ] shape , Context ctx )
@@ -189,6 +135,70 @@ value is NDArray nd &&
189135 }
190136 }
191137
138+ static Tensor convert_to_eager_tensor ( object value ,
139+ TF_DataType dtype ,
140+ TensorShape shape ,
141+ string name ,
142+ bool verify_shape ,
143+ bool allow_broadcast )
144+ {
145+ var t = convert_to_eager_tensor ( value , tf . Context , dtype : dtype ) ;
146+ if ( shape == null )
147+ return t ;
148+
149+ if ( t . shape . Select ( x => Convert . ToInt64 ( x ) ) . SequenceEqual ( shape . dims ) )
150+ return t ;
151+
152+ if ( verify_shape )
153+ throw new TypeError ( $ "Expected Tensor's shape: { shape } , got { t . shape } .") ;
154+
155+ var num_t = t . TensorShape . num_elements ( ) ;
156+ if ( num_t == shape . num_elements ( ) )
157+ return _eager_reshape ( t , shape , tf . Context ) ;
158+ if ( num_t == 1 )
159+ {
160+ if ( t . dtype == dtypes . @bool )
161+ throw new NotImplementedException ( "" ) ;
162+ else
163+ return _eager_fill ( shape , t , tf . Context ) ;
164+ }
165+
166+ throw new NotImplementedException ( "" ) ;
167+ }
168+
169+ static Tensor convert_to_graph_tensor ( object value ,
170+ TF_DataType dtype ,
171+ TensorShape shape ,
172+ string name ,
173+ bool verify_shape ,
174+ bool allow_broadcast )
175+ {
176+ Graph g = ops . get_default_graph ( ) ;
177+ var tensor_value = new AttrValue ( ) ;
178+ tensor_value . Tensor = tensor_util . make_tensor_proto ( value ,
179+ dtype : dtype ,
180+ shape : shape ,
181+ verify_shape : verify_shape ,
182+ allow_broadcast : allow_broadcast ) ;
183+
184+ var dtype_value = new AttrValue
185+ {
186+ Type = tensor_value . Tensor . Dtype ,
187+ } ;
188+
189+ var attrs = new Dictionary < string , AttrValue > ( ) ;
190+ attrs [ "value" ] = tensor_value ;
191+ attrs [ "dtype" ] = dtype_value ;
192+
193+ var op = g . create_op ( "Const" ,
194+ new Tensor [ 0 ] ,
195+ new TF_DataType [ ] { dtype_value . Type . as_tf_dtype ( ) } ,
196+ attrs : attrs ,
197+ name : name ) ;
198+
199+ return op . outputs [ 0 ] ;
200+ }
201+
192202 /// <summary>
193203 /// Function to convert TensorShape to Tensor.
194204 /// </summary>
0 commit comments