pipeline-homo-multi-model.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 HomoOneHotEncoder
  22. from pipeline.component.homo_feature_binning import HomoFeatureBinning
  23. from pipeline.component import FederatedSample
  24. from pipeline.component import HomoLR
  25. from pipeline.component import HomoSecureBoost
  26. from pipeline.component import LocalBaseline
  27. from pipeline.component import Reader
  28. from pipeline.interface import Data
  29. from pipeline.interface import Model
  30. def main(config="../../config.yaml", namespace=""):
  31. # obtain config
  32. if isinstance(config, str):
  33. config = load_job_config(config)
  34. parties = config.parties
  35. guest = parties.guest[0]
  36. host = parties.host[0]
  37. arbiter = parties.arbiter[0]
  38. guest_train_data = {"name": "breast_homo_guest", "namespace": f"experiment{namespace}"}
  39. host_train_data = {"name": "breast_homo_host", "namespace": f"experiment{namespace}"}
  40. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host, arbiter=arbiter)
  41. reader_0 = Reader(name="reader_0")
  42. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  43. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  44. reader_1 = Reader(name="reader_1")
  45. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  46. reader_1.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  47. data_transform_0 = DataTransform(name="data_transform_0", with_label=True)
  48. data_transform_1 = DataTransform(name="data_transform_1")
  49. federated_sample_0 = FederatedSample(name="federated_sample_0", mode="stratified", method="downsample",
  50. fractions=[[0, 1.0], [1, 1.0]], task_type="homo")
  51. homo_binning_0 = HomoFeatureBinning(name='homo_binning_0', sample_bins=10, method="recursive_query")
  52. homo_binning_1 = HomoFeatureBinning(name='homo_binning_1')
  53. homo_onehot_0 = HomoOneHotEncoder(name='homo_onehot_0', need_alignment=True)
  54. homo_onehot_1 = HomoOneHotEncoder(name='homo_onehot_1')
  55. homo_lr_0 = HomoLR(name="homo_lr_0", penalty="L2", tol=0.0001, alpha=1.0,
  56. optimizer="rmsprop", max_iter=5)
  57. homo_lr_1 = HomoLR(name="homo_lr_1")
  58. local_baseline_0 = LocalBaseline(name="local_baseline_0", model_name="LogisticRegression",
  59. model_opts={"penalty": "l2", "tol": 0.0001, "C": 1.0, "fit_intercept": True,
  60. "solver": "lbfgs", "max_iter": 5, "multi_class": "ovr"})
  61. local_baseline_0.get_party_instance(role='guest', party_id=guest).component_param(need_run=True)
  62. local_baseline_0.get_party_instance(role='host', party_id=host).component_param(need_run=True)
  63. local_baseline_1 = LocalBaseline(name="local_baseline_1")
  64. homo_secureboost_0 = HomoSecureBoost(name="homo_secureboost_0", num_trees=3)
  65. homo_secureboost_1 = HomoSecureBoost(name="homo_secureboost_1", num_trees=3)
  66. evaluation_0 = Evaluation(name="evaluation_0")
  67. evaluation_1 = Evaluation(name="evaluation_1")
  68. pipeline.add_component(reader_0)
  69. pipeline.add_component(reader_1)
  70. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  71. pipeline.add_component(data_transform_1, data=Data(data=reader_1.output.data),
  72. model=Model(model=data_transform_0.output.model))
  73. pipeline.add_component(federated_sample_0, data=Data(data=data_transform_0.output.data))
  74. pipeline.add_component(homo_binning_0, data=Data(data=federated_sample_0.output.data))
  75. pipeline.add_component(homo_binning_1, data=Data(data=data_transform_1.output.data),
  76. model=Model(model=homo_binning_0.output.model))
  77. pipeline.add_component(homo_onehot_0, data=Data(data=homo_binning_0.output.data))
  78. pipeline.add_component(homo_onehot_1, data=Data(data=homo_binning_1.output.data),
  79. model=Model(model=homo_onehot_0.output.model))
  80. pipeline.add_component(homo_lr_0, data=Data(data=homo_onehot_0.output.data))
  81. pipeline.add_component(homo_lr_1, data=Data(data=homo_onehot_1.output.data),
  82. model=Model(model=homo_lr_0.output.model))
  83. pipeline.add_component(local_baseline_0, data=Data(data=homo_onehot_0.output.data))
  84. pipeline.add_component(local_baseline_1, data=Data(data=homo_onehot_1.output.data),
  85. model=Model(model=local_baseline_0.output.model))
  86. pipeline.add_component(homo_secureboost_0, data=Data(data=homo_onehot_0.output.data))
  87. pipeline.add_component(homo_secureboost_1, data=Data(data=homo_onehot_1.output.data),
  88. model=Model(model=homo_secureboost_0.output.model))
  89. pipeline.add_component(evaluation_0,
  90. data=Data(
  91. data=[homo_lr_0.output.data, homo_lr_1.output.data,
  92. local_baseline_0.output.data, local_baseline_1.output.data]))
  93. pipeline.add_component(evaluation_1,
  94. data=Data(
  95. data=[homo_secureboost_0.output.data, homo_secureboost_1.output.data]))
  96. pipeline.compile()
  97. pipeline.fit()
  98. print(pipeline.get_component("evaluation_0").get_summary())
  99. print(pipeline.get_component("evaluation_1").get_summary())
  100. if __name__ == "__main__":
  101. parser = argparse.ArgumentParser("PIPELINE DEMO")
  102. parser.add_argument("-config", type=str,
  103. help="config file")
  104. args = parser.parse_args()
  105. if args.config is not None:
  106. main(args.config)
  107. else:
  108. main()