@@ -177,5 +177,60 @@ public void LayerNormalization()
177177 Assert . AreEqual ( ( 5 , 2 ) , output . shape ) ;
178178 Assert . IsTrue ( output [ 0 ] . numpy ( ) . Equals ( new [ ] { - 0.99998f , 0.99998f } ) ) ;
179179 }
180+
181+ /// <summary>
182+ /// https://www.tensorflow.org/api_docs/python/tf/keras/layers/CategoryEncoding
183+ /// </summary>
184+ [ TestMethod ]
185+ public void CategoryEncoding ( )
186+ {
187+ // one-hot
188+ var inputs = np . array ( new [ ] { 3 , 2 , 0 , 1 } ) ;
189+ var layer = tf . keras . layers . CategoryEncoding ( 4 ) ;
190+
191+ Tensor output = layer . Apply ( inputs ) ;
192+ Assert . AreEqual ( ( 4 , 4 ) , output . shape ) ;
193+ Assert . IsTrue ( output [ 0 ] . numpy ( ) . Equals ( new [ ] { 0 , 0 , 0 , 1f } ) ) ;
194+ Assert . IsTrue ( output [ 1 ] . numpy ( ) . Equals ( new [ ] { 0 , 0 , 1 , 0f } ) ) ;
195+ Assert . IsTrue ( output [ 2 ] . numpy ( ) . Equals ( new [ ] { 1 , 0 , 0 , 0f } ) ) ;
196+ Assert . IsTrue ( output [ 3 ] . numpy ( ) . Equals ( new [ ] { 0 , 1 , 0 , 0f } ) ) ;
197+
198+ // multi-hot
199+ inputs = np . array ( new [ , ]
200+ {
201+ { 0 , 1 } ,
202+ { 0 , 0 } ,
203+ { 1 , 2 } ,
204+ { 3 , 1 }
205+ } ) ;
206+ layer = tf . keras . layers . CategoryEncoding ( 4 , output_mode : "multi_hot" ) ;
207+ output = layer . Apply ( inputs ) ;
208+ Assert . IsTrue ( output [ 0 ] . numpy ( ) . Equals ( new [ ] { 1 , 1 , 0 , 0f } ) ) ;
209+ Assert . IsTrue ( output [ 1 ] . numpy ( ) . Equals ( new [ ] { 1 , 0 , 0 , 0f } ) ) ;
210+ Assert . IsTrue ( output [ 2 ] . numpy ( ) . Equals ( new [ ] { 0 , 1 , 1 , 0f } ) ) ;
211+ Assert . IsTrue ( output [ 3 ] . numpy ( ) . Equals ( new [ ] { 0 , 1 , 0 , 1f } ) ) ;
212+
213+ // using weighted inputs in "count" mode
214+ inputs = np . array ( new [ , ]
215+ {
216+ { 0 , 1 } ,
217+ { 0 , 0 } ,
218+ { 1 , 2 } ,
219+ { 3 , 1 }
220+ } ) ;
221+ var weights = np . array ( new [ , ]
222+ {
223+ { 0.1f , 0.2f } ,
224+ { 0.1f , 0.1f } ,
225+ { 0.2f , 0.3f } ,
226+ { 0.4f , 0.2f }
227+ } ) ;
228+ layer = tf . keras . layers . CategoryEncoding ( 4 , output_mode : "count" , count_weights : weights ) ;
229+ output = layer . Apply ( inputs ) ;
230+ Assert . IsTrue ( output [ 0 ] . numpy ( ) . Equals ( new [ ] { 0.1f , 0.2f , 0f , 0f } ) ) ;
231+ Assert . IsTrue ( output [ 1 ] . numpy ( ) . Equals ( new [ ] { 0.2f , 0f , 0f , 0f } ) ) ;
232+ Assert . IsTrue ( output [ 2 ] . numpy ( ) . Equals ( new [ ] { 0f , 0.2f , 0.3f , 0f } ) ) ;
233+ Assert . IsTrue ( output [ 3 ] . numpy ( ) . Equals ( new [ ] { 0f , 0.2f , 0f , 0.4f } ) ) ;
234+ }
180235 }
181236}
0 commit comments