def test_vocab_list_sparse_input(self): layer = categorical.CategoryLookup( vocabulary=self._wire_vocabulary_file_name, num_oov_tokens=0) inp = np.asarray([['omar', ''], ['stringer', 'marlo'], ['marlo', 'omar']]) indices = array_ops.where_v2(math_ops.not_equal(inp, '')) sp_inp = sparse_tensor.SparseTensor( indices, array_ops.gather_nd_v2(inp, indices), dense_shape=array_ops.shape_v2(inp, out_type=dtypes.int64)) output = layer(sp_inp) self.assertIsInstance(output, sparse_tensor.SparseTensor) self.assertAllClose( np.asarray([[0, 0], [1, 0], [1, 1], [2, 0], [2, 1]]), output.indices) self.assertAllClose(np.asarray([0, 1, 2, 2, 0]), output.values)
def test_vocab_list_sparse_input(self): vocabulary_list = ['A', 'B', 'C', 'D', 'E'] layer = categorical.CategoryLookup( vocabulary=vocabulary_list, num_oov_tokens=0) inp = np.asarray([['A', ''], ['E', 'C'], ['D', 'A']]) indices = array_ops.where_v2(math_ops.not_equal(inp, '')) sp_inp = sparse_tensor.SparseTensor( indices, array_ops.gather_nd_v2(inp, indices), dense_shape=array_ops.shape_v2(inp, out_type=dtypes.int64)) output = layer(sp_inp) self.assertIsInstance(output, sparse_tensor.SparseTensor) self.assertAllClose( np.asarray([[0, 0], [1, 0], [1, 1], [2, 0], [2, 1]]), output.indices) self.assertAllClose(np.asarray([0, 4, 2, 3, 0]), output.values)