@@ -14,6 +14,7 @@ You may obtain a copy of the License at
14
14
limitations under the License.
15
15
******************************************************************************/
16
16
17
+ using System . Collections . Generic ;
17
18
using Tensorflow . Keras . Layers ;
18
19
using Tensorflow . Operations . Activation ;
19
20
using static Tensorflow . Binding ;
@@ -163,6 +164,39 @@ public Tensor dense(Tensor inputs,
163
164
164
165
return layer . apply ( inputs ) ;
165
166
}
167
+
168
+ /// <summary>
169
+ /// Flattens an input tensor while preserving the batch axis (axis 0).
170
+ /// </summary>
171
+ /// <param name="inputs">Tensor input.</param>
172
+ /// <param name="name">The name of the layer.</param>
173
+ /// <param name="data_format">
174
+ /// A string, one of `channels_last` (default) or `channels_first`. <br></br>
175
+ /// The ordering of the dimensions in the inputs. <br></br>
176
+ /// `channels_last` corresponds to inputs with shape <br></br>
177
+ /// `(batch, height, width, channels)` while `channels_first` corresponds to <br></br>
178
+ /// inputs with shape `(batch, channels, height, width)`.
179
+ /// </param>
180
+ /// <returns></returns>
181
+ public Tensor flatten ( Tensor inputs ,
182
+ string name = null ,
183
+ string data_format = "channels_last" )
184
+ {
185
+ if ( inputs . shape . Length == 0 )
186
+ throw new ValueError ( $ "Input 0 of layer flatten is incompatible with the layer: : expected min_ndim={ 1 } , found ndim={ 0 } . Full shape received: ()") ;
187
+
188
+ var premutation = new List < int > ( ) { 0 } ;
189
+ if ( data_format == "channels_first" && inputs . NDims > 1 )
190
+ {
191
+ premutation . AddRange ( Binding . range ( 2 , inputs . NDims ) ) ;
192
+ premutation . Add ( 1 ) ;
193
+ inputs = array_ops . transpose ( inputs , premutation . ToArray ( ) ) ;
194
+ }
195
+
196
+ var ret = array_ops . reshape ( inputs , new int [ ] { inputs . shape [ 0 ] , - 1 } ) ;
197
+ ret . set_shape ( new int [ ] { inputs . shape [ 0 ] , - 1 } ) ;
198
+ return ret ;
199
+ }
166
200
}
167
201
}
168
202
}
0 commit comments