@@ -53,6 +53,11 @@ private static Tensor _constant_if_small<T>(T value, Shape shape, TF_DataType dt
5353 }
5454 }
5555
56+ public static Tensor rank ( Tensor input , string name = "" )
57+ {
58+ return math_ops . rank_internal ( input , name , optimize : true ) ;
59+ }
60+
5661 /// <summary>
5762 /// Returns the shape of a tensor.
5863 /// </summary>
@@ -68,11 +73,14 @@ public static Tensor shape(Tensor input, string name = "", TF_DataType out_type
6873 return shape_internal ( input , name , optimize : true , out_type : out_type ) ;
6974 }
7075
71- private static Tensor shape_internal ( Tensor input , string name = "" , bool optimize = true , TF_DataType out_type = TF_DataType . TF_INT32 )
76+ public static Tensor size ( Tensor input , string name = "" , TF_DataType out_type = TF_DataType . TF_INT32 )
7277 {
73- Tensor result = null ;
78+ return size_internal ( input , name , optimize : true , out_type : out_type ) ;
79+ }
7480
75- Python . with < ops . name_scope > ( new ops . name_scope ( name , "Shape" , new Tensor [ ] { input } ) , scope =>
81+ private static Tensor shape_internal ( Tensor input , string name = "" , bool optimize = true , TF_DataType out_type = TF_DataType . TF_INT32 )
82+ {
83+ return Python . with < ops . name_scope , Tensor > ( new ops . name_scope ( name , "Shape" , new Tensor [ ] { input } ) , scope =>
7684 {
7785 name = scope ;
7886
@@ -83,16 +91,46 @@ private static Tensor shape_internal(Tensor input, string name = "", bool optimi
8391 if ( optimize && input_shape . is_fully_defined ( ) )
8492 {
8593 var nd = np . array ( input_tensor . shape , out_type . as_numpy_datatype ( ) ) ;
86- result = constant_op . constant ( nd , name ) ;
94+ return constant_op . constant ( nd , name ) ;
8795 }
8896 }
8997 else
9098 {
9199 // result = gen_array_ops.shape();
92100 }
101+
102+ return null ;
93103 } ) ;
104+ }
94105
95- return result ;
106+ private static Tensor size_internal ( Tensor input , string name = "" , bool optimize = true , TF_DataType out_type = TF_DataType . TF_INT32 )
107+ {
108+ return Python . with < ops . name_scope , Tensor > ( new ops . name_scope ( name , "Size" , new Tensor [ ] { input } ) , scope =>
109+ {
110+ name = scope ;
111+
112+ if ( ! tf . context . executing_eagerly ( ) )
113+ {
114+ var input_tensor = ops . convert_to_tensor ( input ) ;
115+ var input_shape = tensor_util . to_shape ( input_tensor . shape ) ;
116+ if ( optimize )
117+ {
118+ if ( input_shape . is_fully_defined ( ) )
119+ {
120+ var nd = np . array ( input_tensor . shape , out_type . as_numpy_datatype ( ) ) ;
121+ return constant_op . constant ( nd , name ) ;
122+ }
123+ }
124+
125+ return gen_array_ops . size ( input , name : name , out_type : out_type ) ;
126+ }
127+ else
128+ {
129+ // result = gen_array_ops.shape();
130+ }
131+
132+ return null ;
133+ } ) ;
96134 }
97135 }
98136}
0 commit comments