pipeline_train_sbt.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import argparse
  17. from pipeline.backend.pipeline import PipeLine
  18. from pipeline.component import HeteroFeatureBinning, HeteroFeatureSelection, DataStatistics, Evaluation
  19. from pipeline.component.hetero_secureboost import HeteroSecureBoost
  20. from pipeline.component import DataTransform
  21. from pipeline.component import Intersection
  22. from pipeline.component import Reader
  23. from pipeline.interface import Data
  24. from pipeline.interface import Model
  25. from pipeline.utils.tools import load_job_config
  26. def main(config="../../config.yaml", namespace=""):
  27. # obtain config
  28. if isinstance(config, str):
  29. config = load_job_config(config)
  30. parties = config.parties
  31. guest = parties.guest[0]
  32. host = parties.host[0]
  33. guest_train_data = {"name": "breast_hetero_guest", "namespace": "experiment"}
  34. guest_test_data = {"name": "breast_hetero_guest", "namespace": "experiment"}
  35. host_train_data = {"name": "breast_hetero_host_tag_value", "namespace": "experiment"}
  36. host_test_data = {"name": "breast_hetero_host_tag_value", "namespace": "experiment"}
  37. # initialize pipeline
  38. pipeline = PipeLine()
  39. # set job initiator
  40. pipeline.set_initiator(role='guest', party_id=guest)
  41. # set participants information
  42. pipeline.set_roles(guest=guest, host=host)
  43. # define Reader components to read in data
  44. reader_0 = Reader(name="reader_0")
  45. reader_1 = Reader(name="reader_1")
  46. # configure Reader for guest
  47. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  48. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_test_data)
  49. # configure Reader for host
  50. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  51. reader_1.get_party_instance(role='host', party_id=host).component_param(table=host_test_data)
  52. # define DataTransform components
  53. data_transform_0 = DataTransform(name="data_transform_0") # start component numbering at 0
  54. data_transform_1 = DataTransform(name="data_transform_1") # start component numbering at 1
  55. param = {
  56. "with_label": True,
  57. "label_name": "y",
  58. "label_type": "int",
  59. "output_format": "dense",
  60. "missing_fill": True,
  61. "missing_fill_method": "mean",
  62. "outlier_replace": False,
  63. "outlier_replace_method": "designated",
  64. "outlier_replace_value": 0.66,
  65. "outlier_impute": "-9999"
  66. }
  67. # get DataTransform party instance of guest
  68. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  69. # configure DataTransform for guest
  70. data_transform_0_guest_party_instance.component_param(**param)
  71. # get and configure DataTransform party instance of host
  72. data_transform_1.get_party_instance(role='guest', party_id=guest).component_param(**param)
  73. param = {
  74. "input_format": "tag",
  75. "with_label": False,
  76. "tag_with_value": True,
  77. "delimitor": ";",
  78. "output_format": "dense"
  79. }
  80. data_transform_0.get_party_instance(role='host', party_id=host).component_param(**param)
  81. data_transform_1.get_party_instance(role='host', party_id=host).component_param(**param)
  82. # define Intersection components
  83. intersection_0 = Intersection(name="intersection_0", intersect_method="raw")
  84. intersection_1 = Intersection(name="intersection_1", intersect_method="raw")
  85. param = {
  86. "name": 'hetero_feature_binning_0',
  87. "method": 'optimal',
  88. "optimal_binning_param": {
  89. "metric_method": "iv",
  90. "init_bucket_method": "quantile"
  91. },
  92. "bin_indexes": -1
  93. }
  94. hetero_feature_binning_0 = HeteroFeatureBinning(**param)
  95. statistic_0 = DataStatistics(name='statistic_0')
  96. param = {
  97. "name": 'hetero_feature_selection_0',
  98. "filter_methods": ["unique_value", "iv_filter", "statistic_filter"],
  99. "unique_param": {
  100. "eps": 1e-6
  101. },
  102. "iv_param": {
  103. "metrics": ["iv", "iv"],
  104. "filter_type": ["top_k", "threshold"],
  105. "take_high": [True, True],
  106. "threshold": [10, 0.1]
  107. },
  108. "statistic_param": {
  109. "metrics": ["coefficient_of_variance", "skewness"],
  110. "filter_type": ["threshold", "threshold"],
  111. "take_high": [True, False],
  112. "threshold": [0.001, -0.01]
  113. },
  114. "select_col_indexes": -1
  115. }
  116. hetero_feature_selection_0 = HeteroFeatureSelection(**param)
  117. hetero_feature_selection_1 = HeteroFeatureSelection(name='hetero_feature_selection_1')
  118. param = {
  119. "task_type": "classification",
  120. "learning_rate": 0.1,
  121. "num_trees": 10,
  122. "subsample_feature_rate": 0.5,
  123. "n_iter_no_change": False,
  124. "tol": 0.0002,
  125. "bin_num": 50,
  126. "objective_param": {
  127. "objective": "cross_entropy"
  128. },
  129. "encrypt_param": {
  130. "method": "paillier"
  131. },
  132. "predict_param": {
  133. "threshold": 0.5
  134. },
  135. "tree_param": {
  136. "max_depth": 2
  137. },
  138. "cv_param": {
  139. "n_splits": 5,
  140. "shuffle": False,
  141. "random_seed": 103,
  142. "need_cv": False
  143. },
  144. "validation_freqs": 2,
  145. "early_stopping_rounds": 5,
  146. "metrics": ["auc", "ks"]
  147. }
  148. hetero_secureboost_0 = HeteroSecureBoost(name='hetero_secureboost_0', **param)
  149. evaluation_0 = Evaluation(name='evaluation_0')
  150. # add components to pipeline, in order of task execution
  151. pipeline.add_component(reader_0)
  152. pipeline.add_component(reader_1)
  153. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  154. pipeline.add_component(data_transform_1,
  155. data=Data(data=reader_1.output.data), model=Model(data_transform_0.output.model))
  156. # set data input sources of intersection components
  157. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  158. pipeline.add_component(intersection_1, data=Data(data=data_transform_1.output.data))
  159. pipeline.add_component(hetero_feature_binning_0, data=Data(data=intersection_0.output.data))
  160. pipeline.add_component(statistic_0, data=Data(data=intersection_0.output.data))
  161. pipeline.add_component(hetero_feature_selection_0, data=Data(data=intersection_0.output.data),
  162. model=Model(isometric_model=[hetero_feature_binning_0.output.model,
  163. statistic_0.output.model]))
  164. pipeline.add_component(hetero_feature_selection_1, data=Data(data=intersection_1.output.data),
  165. model=Model(hetero_feature_selection_0.output.model))
  166. # set train & validate data of hetero_secureboost_0 component
  167. pipeline.add_component(hetero_secureboost_0, data=Data(train_data=hetero_feature_selection_0.output.data,
  168. validate_data=hetero_feature_selection_1.output.data))
  169. pipeline.add_component(evaluation_0, data=Data(data=[hetero_secureboost_0.output.data]))
  170. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  171. pipeline.compile()
  172. # fit model
  173. pipeline.fit()
  174. # query component summary
  175. print(pipeline.get_component("hetero_secureboost_0").get_summary())
  176. if __name__ == "__main__":
  177. parser = argparse.ArgumentParser("PIPELINE DEMO")
  178. parser.add_argument("-config", type=str,
  179. help="config file")
  180. args = parser.parse_args()
  181. if args.config is not None:
  182. main(args.config)
  183. else:
  184. main()