pipeline_train_lr.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 import FeatureScale
  20. from pipeline.component import DataTransform
  21. from pipeline.component import HeteroLR
  22. from pipeline.component import Intersection
  23. from pipeline.component import Reader
  24. from pipeline.interface import Data
  25. from pipeline.interface import Model
  26. from pipeline.utils.tools import load_job_config
  27. def main(config="../../config.yaml", namespace=""):
  28. # obtain config
  29. if isinstance(config, str):
  30. config = load_job_config(config)
  31. parties = config.parties
  32. guest = parties.guest[0]
  33. host = parties.host[0]
  34. arbiter = parties.arbiter[0]
  35. guest_train_data = {"name": "breast_hetero_guest", "namespace": "experiment"}
  36. guest_test_data = {"name": "breast_hetero_guest", "namespace": "experiment"}
  37. host_train_data = {"name": "breast_hetero_host_tag_value", "namespace": "experiment"}
  38. host_test_data = {"name": "breast_hetero_host_tag_value", "namespace": "experiment"}
  39. # initialize pipeline
  40. pipeline = PipeLine()
  41. # set job initiator
  42. pipeline.set_initiator(role='guest', party_id=guest)
  43. # set participants information
  44. pipeline.set_roles(guest=guest, host=host, arbiter=arbiter)
  45. # define Reader components to read in data
  46. reader_0 = Reader(name="reader_0")
  47. reader_1 = Reader(name="reader_1")
  48. # configure Reader for guest
  49. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  50. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_test_data)
  51. # configure Reader for host
  52. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  53. reader_1.get_party_instance(role='host', party_id=host).component_param(table=host_test_data)
  54. # define DataTransform components
  55. data_transform_0 = DataTransform(name="data_transform_0") # start component numbering at 0
  56. data_transform_1 = DataTransform(name="data_transform_1") # start component numbering at 1
  57. param = {
  58. "with_label": True,
  59. "label_name": "y",
  60. "label_type": "int",
  61. "output_format": "dense",
  62. "missing_fill": True,
  63. "missing_fill_method": "mean",
  64. "outlier_replace": False,
  65. "outlier_replace_method": "designated",
  66. "outlier_replace_value": 0.66,
  67. "outlier_impute": "-9999"
  68. }
  69. # get DataTransform party instance of guest
  70. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  71. # configure DataTransform for guest
  72. data_transform_0_guest_party_instance.component_param(**param)
  73. # get and configure DataTransform party instance of host
  74. data_transform_1.get_party_instance(role='guest', party_id=guest).component_param(**param)
  75. param = {
  76. "input_format": "tag",
  77. "with_label": False,
  78. "tag_with_value": True,
  79. "delimitor": ";",
  80. "output_format": "dense"
  81. }
  82. data_transform_0.get_party_instance(role='host', party_id=host).component_param(**param)
  83. data_transform_1.get_party_instance(role='host', party_id=host).component_param(**param)
  84. # define Intersection components
  85. intersection_0 = Intersection(name="intersection_0", intersect_method="raw")
  86. intersection_1 = Intersection(name="intersection_1", intersect_method="raw")
  87. param = {
  88. "name": 'hetero_feature_binning_0',
  89. "method": 'optimal',
  90. "optimal_binning_param": {
  91. "metric_method": "iv",
  92. "init_bucket_method": "quantile"
  93. },
  94. "bin_indexes": -1
  95. }
  96. hetero_feature_binning_0 = HeteroFeatureBinning(**param)
  97. statistic_0 = DataStatistics(name='statistic_0')
  98. param = {
  99. "name": 'hetero_feature_selection_0',
  100. "filter_methods": ["unique_value", "iv_filter", "statistic_filter"],
  101. "unique_param": {
  102. "eps": 1e-6
  103. },
  104. "iv_param": {
  105. "metrics": ["iv", "iv"],
  106. "filter_type": ["top_k", "threshold"],
  107. "take_high": [True, True],
  108. "threshold": [10, 0.1]
  109. },
  110. "statistic_param": {
  111. "metrics": ["coefficient_of_variance", "skewness"],
  112. "filter_type": ["threshold", "threshold"],
  113. "take_high": [True, False],
  114. "threshold": [0.001, -0.01]
  115. },
  116. "select_col_indexes": -1
  117. }
  118. hetero_feature_selection_0 = HeteroFeatureSelection(**param)
  119. hetero_feature_selection_1 = HeteroFeatureSelection(name='hetero_feature_selection_1')
  120. param = {
  121. "name": "hetero_scale_0",
  122. "method": "standard_scale"
  123. }
  124. hetero_scale_0 = FeatureScale(**param)
  125. hetero_scale_1 = FeatureScale(name='hetero_scale_1')
  126. param = {
  127. "penalty": "L2",
  128. "optimizer": "nesterov_momentum_sgd",
  129. "tol": 1e-4,
  130. "alpha": 0.01,
  131. "max_iter": 5,
  132. "early_stop": "diff",
  133. "batch_size": -1,
  134. "learning_rate": 0.15,
  135. "init_param": {
  136. "init_method": "zeros"
  137. },
  138. "validation_freqs": None,
  139. "early_stopping_rounds": None
  140. }
  141. hetero_lr_0 = HeteroLR(name='hetero_lr_0', **param)
  142. evaluation_0 = Evaluation(name='evaluation_0')
  143. # add components to pipeline, in order of task execution
  144. pipeline.add_component(reader_0)
  145. pipeline.add_component(reader_1)
  146. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  147. pipeline.add_component(data_transform_1,
  148. data=Data(data=reader_1.output.data), model=Model(data_transform_0.output.model))
  149. # set data input sources of intersection components
  150. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  151. pipeline.add_component(intersection_1, data=Data(data=data_transform_1.output.data))
  152. # set train & validate data of hetero_lr_0 component
  153. pipeline.add_component(hetero_feature_binning_0, data=Data(data=intersection_0.output.data))
  154. pipeline.add_component(statistic_0, data=Data(data=intersection_0.output.data))
  155. pipeline.add_component(hetero_feature_selection_0, data=Data(data=intersection_0.output.data),
  156. model=Model(isometric_model=[hetero_feature_binning_0.output.model,
  157. statistic_0.output.model]))
  158. pipeline.add_component(hetero_feature_selection_1, data=Data(data=intersection_1.output.data),
  159. model=Model(hetero_feature_selection_0.output.model))
  160. pipeline.add_component(hetero_scale_0, data=Data(data=hetero_feature_selection_0.output.data))
  161. pipeline.add_component(hetero_scale_1, data=Data(data=hetero_feature_selection_1.output.data),
  162. model=Model(hetero_scale_0.output.model))
  163. # set train & validate data of hetero_lr_0 component
  164. pipeline.add_component(hetero_lr_0, data=Data(train_data=hetero_scale_0.output.data,
  165. validate_data=hetero_scale_1.output.data))
  166. pipeline.add_component(evaluation_0, data=Data(data=[hetero_lr_0.output.data]))
  167. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  168. pipeline.compile()
  169. # fit model
  170. pipeline.fit()
  171. # query component summary
  172. print(pipeline.get_component("hetero_lr_0").get_summary())
  173. if __name__ == "__main__":
  174. parser = argparse.ArgumentParser("PIPELINE DEMO")
  175. parser.add_argument("-config", type=str,
  176. help="config file")
  177. args = parser.parse_args()
  178. if args.config is not None:
  179. main(args.config)
  180. else:
  181. main()