pipeline-hetero-multi-model.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.utils.tools import load_job_config
  18. from pipeline.backend.pipeline import PipeLine
  19. from pipeline.component import DataTransform
  20. from pipeline.component import Evaluation
  21. from pipeline.component import FeatureScale
  22. from pipeline.component import FederatedSample
  23. from pipeline.component import HeteroFeatureBinning
  24. from pipeline.component import HeteroFeatureSelection
  25. from pipeline.component import HeteroLR
  26. from pipeline.component import HeteroSecureBoost
  27. from pipeline.component import Intersection
  28. from pipeline.component import OneHotEncoder
  29. from pipeline.component import Union
  30. from pipeline.component import LocalBaseline
  31. from pipeline.component import HeteroLinR
  32. from pipeline.component import HeteroPoisson
  33. from pipeline.component import HeteroSSHELR
  34. from pipeline.component import HeteroSSHELinR
  35. from pipeline.component import Reader
  36. from pipeline.interface import Data
  37. from pipeline.interface import Model
  38. """Note: This script is used for components regression only"""
  39. def main(config="../../config.yaml", namespace=""):
  40. # obtain config
  41. if isinstance(config, str):
  42. config = load_job_config(config)
  43. parties = config.parties
  44. guest = parties.guest[0]
  45. host = parties.host[0]
  46. arbiter = parties.arbiter[0]
  47. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  48. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  49. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host, arbiter=arbiter)
  50. reader_0 = Reader(name="reader_0")
  51. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  52. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  53. reader_1 = Reader(name="reader_1")
  54. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  55. reader_1.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  56. reader_2 = Reader(name="reader_2")
  57. reader_2.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  58. reader_2.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  59. data_transform_0 = DataTransform(name="data_transform_0")
  60. data_transform_0.get_party_instance(
  61. role='guest',
  62. party_id=guest).component_param(
  63. with_label=True,
  64. missing_fill=True,
  65. outlier_replace=True)
  66. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False, missing_fill=True,
  67. outlier_replace=True)
  68. data_transform_1 = DataTransform(name="data_transform_1")
  69. data_transform_2 = DataTransform(name="data_transform_2")
  70. intersection_0 = Intersection(name="intersection_0")
  71. intersection_1 = Intersection(name="intersection_1")
  72. intersection_2 = Intersection(name="intersection_2")
  73. union_0 = Union(name="union_0")
  74. federated_sample_0 = FederatedSample(name="federated_sample_0", mode="stratified", method="downsample",
  75. fractions=[[0, 1.0], [1, 1.0]])
  76. feature_scale_0 = FeatureScale(name="feature_scale_0")
  77. feature_scale_1 = FeatureScale(name="feature_scale_1")
  78. hetero_feature_binning_0 = HeteroFeatureBinning(name="hetero_feature_binning_0")
  79. hetero_feature_binning_1 = HeteroFeatureBinning(name="hetero_feature_binning_1")
  80. hetero_feature_selection_0 = HeteroFeatureSelection(name="hetero_feature_selection_0")
  81. hetero_feature_selection_1 = HeteroFeatureSelection(name="hetero_feature_selection_1")
  82. one_hot_0 = OneHotEncoder(name="one_hot_0")
  83. one_hot_1 = OneHotEncoder(name="one_hot_1")
  84. hetero_lr_0 = HeteroLR(name="hetero_lr_0", penalty="L2", optimizer="rmsprop", tol=1e-5,
  85. init_param={"init_method": "random_uniform"},
  86. alpha=0.01, max_iter=3, early_stop="diff", batch_size=320, learning_rate=0.15)
  87. hetero_lr_1 = HeteroLR(name="hetero_lr_1")
  88. hetero_lr_2 = HeteroLR(name="hetero_lr_2", penalty="L2", optimizer="rmsprop", tol=1e-5,
  89. init_param={"init_method": "random_uniform"},
  90. alpha=0.01, max_iter=3, early_stop="diff", batch_size=320, learning_rate=0.15,
  91. cv_param={"n_splits": 5,
  92. "shuffle": True,
  93. "random_seed": 103,
  94. "need_cv": True})
  95. hetero_sshe_lr_0 = HeteroSSHELR(name="hetero_sshe_lr_0", reveal_every_iter=True, reveal_strategy="respectively",
  96. penalty="L2", optimizer="rmsprop", tol=1e-5, batch_size=320, learning_rate=0.15,
  97. init_param={"init_method": "random_uniform"}, alpha=0.01, max_iter=3)
  98. hetero_sshe_lr_1 = HeteroSSHELR(name="hetero_sshe_lr_1")
  99. local_baseline_0 = LocalBaseline(name="local_baseline_0", model_name="LogisticRegression",
  100. model_opts={"penalty": "l2", "tol": 0.0001, "C": 1.0, "fit_intercept": True,
  101. "solver": "lbfgs", "max_iter": 5, "multi_class": "ovr"})
  102. local_baseline_0.get_party_instance(role='guest', party_id=guest).component_param(need_run=True)
  103. local_baseline_0.get_party_instance(role='host', party_id=host).component_param(need_run=False)
  104. local_baseline_1 = LocalBaseline(name="local_baseline_1")
  105. hetero_secureboost_0 = HeteroSecureBoost(name="hetero_secureboost_0", num_trees=3)
  106. hetero_secureboost_1 = HeteroSecureBoost(name="hetero_secureboost_1")
  107. hetero_secureboost_2 = HeteroSecureBoost(name="hetero_secureboost_2", num_trees=3,
  108. cv_param={"shuffle": False, "need_cv": True})
  109. hetero_linr_0 = HeteroLinR(name="hetero_linr_0", penalty="L2", optimizer="sgd", tol=0.001,
  110. alpha=0.01, max_iter=3, early_stop="weight_diff", batch_size=-1,
  111. learning_rate=0.15, decay=0.0, decay_sqrt=False,
  112. init_param={"init_method": "zeros"},
  113. floating_point_precision=23)
  114. hetero_linr_1 = HeteroLinR(name="hetero_linr_1")
  115. hetero_sshe_linr_0 = HeteroSSHELinR(name="hetero_sshe_linr_0", max_iter=5, early_stop="weight_diff", batch_size=-1)
  116. hetero_sshe_linr_1 = HeteroSSHELinR(name="hetero_sshe_linr_1")
  117. hetero_poisson_0 = HeteroPoisson(name="hetero_poisson_0", early_stop="weight_diff", max_iter=10,
  118. alpha=100.0, batch_size=-1, learning_rate=0.01, optimizer="rmsprop",
  119. exposure_colname="exposure", decay_sqrt=False, tol=0.001,
  120. init_param={"init_method": "zeros"}, penalty="L2")
  121. hetero_poisson_1 = HeteroPoisson(name="hetero_poisson_1")
  122. evaluation_0 = Evaluation(name="evaluation_0")
  123. evaluation_1 = Evaluation(name="evaluation_1")
  124. evaluation_2 = Evaluation(name="evaluation_2")
  125. pipeline.add_component(reader_0)
  126. pipeline.add_component(reader_1)
  127. pipeline.add_component(reader_2)
  128. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  129. pipeline.add_component(data_transform_1, data=Data(data=reader_1.output.data),
  130. model=Model(model=data_transform_0.output.model))
  131. pipeline.add_component(data_transform_2, data=Data(data=reader_2.output.data),
  132. model=Model(model=data_transform_0.output.model))
  133. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  134. pipeline.add_component(intersection_1, data=Data(data=data_transform_1.output.data))
  135. pipeline.add_component(intersection_2, data=Data(data=data_transform_2.output.data))
  136. pipeline.add_component(union_0, data=Data(data=[intersection_0.output.data, intersection_2.output.data]))
  137. pipeline.add_component(federated_sample_0, data=Data(data=intersection_1.output.data))
  138. pipeline.add_component(feature_scale_0, data=Data(data=union_0.output.data))
  139. pipeline.add_component(feature_scale_1, data=Data(data=federated_sample_0.output.data),
  140. model=Model(model=feature_scale_0.output.model))
  141. pipeline.add_component(hetero_feature_binning_0, data=Data(data=feature_scale_0.output.data))
  142. pipeline.add_component(hetero_feature_binning_1, data=Data(data=feature_scale_1.output.data),
  143. model=Model(model=hetero_feature_binning_0.output.model))
  144. pipeline.add_component(hetero_feature_selection_0, data=Data(data=hetero_feature_binning_0.output.data))
  145. pipeline.add_component(hetero_feature_selection_1, data=Data(data=hetero_feature_binning_1.output.data),
  146. model=Model(model=hetero_feature_selection_0.output.model))
  147. pipeline.add_component(one_hot_0, data=Data(data=hetero_feature_selection_0.output.data))
  148. pipeline.add_component(one_hot_1, data=Data(data=hetero_feature_selection_1.output.data),
  149. model=Model(model=one_hot_0.output.model))
  150. pipeline.add_component(hetero_lr_0, data=Data(train_data=one_hot_0.output.data))
  151. pipeline.add_component(hetero_lr_1, data=Data(test_data=one_hot_1.output.data),
  152. model=Model(model=hetero_lr_0.output.model))
  153. pipeline.add_component(hetero_lr_2, data=Data(train_data=one_hot_0.output.data))
  154. pipeline.add_component(local_baseline_0, data=Data(train_data=one_hot_0.output.data))
  155. pipeline.add_component(local_baseline_1, data=Data(test_data=one_hot_1.output.data),
  156. model=Model(model=local_baseline_0.output.model))
  157. pipeline.add_component(hetero_sshe_lr_0, data=Data(train_data=one_hot_0.output.data))
  158. pipeline.add_component(hetero_sshe_lr_1, data=Data(test_data=one_hot_1.output.data),
  159. model=Model(model=hetero_sshe_lr_0.output.model))
  160. pipeline.add_component(hetero_secureboost_0, data=Data(train_data=one_hot_0.output.data))
  161. pipeline.add_component(hetero_secureboost_1, data=Data(test_data=one_hot_1.output.data),
  162. model=Model(model=hetero_secureboost_0.output.model))
  163. pipeline.add_component(hetero_secureboost_2, data=Data(train_data=one_hot_0.output.data))
  164. pipeline.add_component(hetero_linr_0, data=Data(train_data=one_hot_0.output.data))
  165. pipeline.add_component(hetero_linr_1, data=Data(test_data=one_hot_1.output.data),
  166. model=Model(model=hetero_linr_0.output.model))
  167. pipeline.add_component(hetero_sshe_linr_0, data=Data(train_data=one_hot_0.output.data))
  168. pipeline.add_component(hetero_sshe_linr_1, data=Data(test_data=one_hot_1.output.data),
  169. model=Model(model=hetero_sshe_linr_0.output.model))
  170. pipeline.add_component(hetero_poisson_0, data=Data(train_data=one_hot_0.output.data))
  171. pipeline.add_component(hetero_poisson_1, data=Data(test_data=one_hot_1.output.data),
  172. model=Model(model=hetero_poisson_0.output.model))
  173. pipeline.add_component(evaluation_0, data=Data(data=[hetero_lr_0.output.data, hetero_lr_1.output.data,
  174. hetero_sshe_lr_0.output.data, hetero_sshe_lr_1.output.data,
  175. local_baseline_0.output.data, local_baseline_1.output.data]))
  176. pipeline.add_component(evaluation_1,
  177. data=Data(
  178. data=[hetero_linr_0.output.data, hetero_linr_1.output.data,
  179. hetero_sshe_linr_0.output.data, hetero_linr_1.output.data]))
  180. pipeline.add_component(evaluation_2,
  181. data=Data(
  182. data=[hetero_poisson_0.output.data, hetero_poisson_1.output.data]))
  183. pipeline.compile()
  184. pipeline.fit()
  185. print(pipeline.get_component("evaluation_0").get_summary())
  186. print(pipeline.get_component("evaluation_1").get_summary())
  187. print(pipeline.get_component("evaluation_2").get_summary())
  188. if __name__ == "__main__":
  189. parser = argparse.ArgumentParser("PIPELINE DEMO")
  190. parser.add_argument("-config", type=str,
  191. help="config file")
  192. args = parser.parse_args()
  193. if args.config is not None:
  194. main(args.config)
  195. else:
  196. main()