|
| 1 | +# Copyright 2020 The SQLFlow Authors. All rights reserved. |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +import unittest |
| 15 | + |
| 16 | +from runtime.feature.column import (BucketColumn, CategoryHashColumn, |
| 17 | + CategoryIDColumn, CrossColumn, |
| 18 | + EmbeddingColumn, IndicatorColumn, |
| 19 | + NumericColumn, SeqCategoryIDColumn) |
| 20 | +from runtime.feature.compile import compile_ir_feature_columns |
| 21 | +from runtime.feature.field_desc import DataType, FieldDesc |
| 22 | +from runtime.model import EstimatorType |
| 23 | + |
| 24 | +TENSORFLOW = EstimatorType.TENSORFLOW |
| 25 | +XGBOOST = EstimatorType.XGBOOST |
| 26 | + |
| 27 | + |
| 28 | +class TestFeatureColumnCompilation(unittest.TestCase): |
| 29 | + def compile_fc(self, fc, model_type): |
| 30 | + fc_dict = {"feature_columns": [fc]} |
| 31 | + rt_fc_dict = compile_ir_feature_columns(fc_dict, model_type) |
| 32 | + self.assertEqual(len(rt_fc_dict), 1) |
| 33 | + self.assertTrue("feature_columns" in rt_fc_dict) |
| 34 | + fc_list = rt_fc_dict.get("feature_columns") |
| 35 | + self.assertEqual(len(fc_list), 1) |
| 36 | + return fc_list[0] |
| 37 | + |
| 38 | + def test_numeric_column(self): |
| 39 | + nc = NumericColumn(FieldDesc(name='c1', shape=(2, 3))) |
| 40 | + |
| 41 | + for model_type in [TENSORFLOW, XGBOOST]: |
| 42 | + compiled_nc = self.compile_fc(nc, model_type) |
| 43 | + self.assertEqual(compiled_nc.key, 'c1') |
| 44 | + self.assertEqual(compiled_nc.shape, (2, 3)) |
| 45 | + |
| 46 | + def test_bucket_column(self): |
| 47 | + nc = NumericColumn(FieldDesc(name='c1', shape=(1, ))) |
| 48 | + bc = BucketColumn(nc, (-10, -5, 3, 7)) |
| 49 | + |
| 50 | + for model_type in [TENSORFLOW, XGBOOST]: |
| 51 | + compiled_bc = self.compile_fc(bc, model_type) |
| 52 | + self.assertEqual(compiled_bc.source_column.key, 'c1') |
| 53 | + self.assertEqual(compiled_bc.boundaries, (-10, -5, 3, 7)) |
| 54 | + |
| 55 | + def test_category_id_column(self): |
| 56 | + cc = CategoryIDColumn(FieldDesc(name='c1'), 128) |
| 57 | + |
| 58 | + for model_type in [TENSORFLOW, XGBOOST]: |
| 59 | + compiled_cc = self.compile_fc(cc, model_type) |
| 60 | + self.assertEqual(compiled_cc.key, 'c1') |
| 61 | + self.assertEqual(compiled_cc.num_buckets, 128) |
| 62 | + |
| 63 | + cc = CategoryIDColumn(FieldDesc(name='c1', vocabulary=set(['a', 'b'])), |
| 64 | + 128) |
| 65 | + for model_type in [TENSORFLOW, XGBOOST]: |
| 66 | + compiled_cc = self.compile_fc(cc, model_type) |
| 67 | + vocab = sorted(compiled_cc.vocabulary_list) |
| 68 | + self.assertEqual(vocab, ['a', 'b']) |
| 69 | + |
| 70 | + def test_seq_category_id_column(self): |
| 71 | + scc = SeqCategoryIDColumn(FieldDesc(name='c1'), 64) |
| 72 | + compiled_scc = self.compile_fc(scc, TENSORFLOW) |
| 73 | + # NOTE: TensorFlow SeqCategoryIDColumn does not have key |
| 74 | + # attribute |
| 75 | + # self.assertEqual(compiled_scc.key, 'c1') |
| 76 | + self.assertEqual(compiled_scc.num_buckets, 64) |
| 77 | + |
| 78 | + with self.assertRaises(AssertionError): |
| 79 | + self.compile_fc(scc, XGBOOST) |
| 80 | + |
| 81 | + def test_category_hash_column(self): |
| 82 | + chc = CategoryHashColumn(FieldDesc(name='c1', dtype=DataType.STRING), |
| 83 | + 32) |
| 84 | + for model_type in [TENSORFLOW, XGBOOST]: |
| 85 | + compiled_chc = self.compile_fc(chc, model_type) |
| 86 | + self.assertEqual(compiled_chc.key, 'c1') |
| 87 | + self.assertEqual(compiled_chc.hash_bucket_size, 32) |
| 88 | + |
| 89 | + def test_cross_column(self): |
| 90 | + cc = CrossColumn(['c1', NumericColumn(FieldDesc(name='c2'))], 4096) |
| 91 | + compiled_cc = self.compile_fc(cc, TENSORFLOW) |
| 92 | + self.assertEqual(list(compiled_cc.keys), ['c1', 'c2']) |
| 93 | + self.assertEqual(compiled_cc.hash_bucket_size, 4096) |
| 94 | + |
| 95 | + with self.assertRaises(AssertionError): |
| 96 | + self.compile_fc(cc, XGBOOST) |
| 97 | + |
| 98 | + def test_embedding_column(self): |
| 99 | + chc = CategoryHashColumn(FieldDesc(name='c1', dtype=DataType.STRING), |
| 100 | + 32) |
| 101 | + ec = EmbeddingColumn(category_column=chc, combiner='sum', dimension=23) |
| 102 | + |
| 103 | + compiled_ec = self.compile_fc(ec, TENSORFLOW) |
| 104 | + self.assertEqual(compiled_ec.combiner, 'sum') |
| 105 | + self.assertEqual(compiled_ec.dimension, 23) |
| 106 | + |
| 107 | + compiled_chc = compiled_ec.categorical_column |
| 108 | + self.assertEqual(compiled_chc.key, 'c1') |
| 109 | + self.assertEqual(compiled_chc.hash_bucket_size, 32) |
| 110 | + |
| 111 | + with self.assertRaises(AssertionError): |
| 112 | + self.compile_fc(ec, XGBOOST) |
| 113 | + |
| 114 | + def test_indicator_column(self): |
| 115 | + cc = CategoryIDColumn(FieldDesc(name='c1'), 128) |
| 116 | + ic = IndicatorColumn(category_column=cc) |
| 117 | + |
| 118 | + for model_type in [TENSORFLOW, XGBOOST]: |
| 119 | + compiled_chc = self.compile_fc(ic, model_type) |
| 120 | + compiled_cc = compiled_chc.categorical_column |
| 121 | + self.assertEqual(compiled_cc.key, 'c1') |
| 122 | + self.assertEqual(compiled_cc.num_buckets, 128) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == '__main__': |
| 126 | + unittest.main() |
0 commit comments