pipeline-hetero-sbt-warm-start.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. import json
  18. from pipeline.backend.pipeline import PipeLine
  19. from pipeline.component import HeteroSecureBoost
  20. from pipeline.component import DataTransform
  21. from pipeline.component import Evaluation
  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 prettify(response, verbose=True):
  28. if verbose:
  29. print(json.dumps(response, indent=4, ensure_ascii=False))
  30. print()
  31. return response
  32. def main(config="../../config.yaml", namespace=""):
  33. if isinstance(config, str):
  34. config = load_job_config(config)
  35. parties = config.parties
  36. guest = parties.guest[0]
  37. hosts = parties.host[0]
  38. arbiter = parties.arbiter[0]
  39. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  40. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  41. # initialize pipeline
  42. pipeline = PipeLine()
  43. # set job initiator
  44. pipeline.set_initiator(role='guest', party_id=guest)
  45. # set participants information
  46. pipeline.set_roles(guest=guest, host=hosts, arbiter=arbiter)
  47. # define Reader components to read in data
  48. reader_0 = Reader(name="reader_0")
  49. # configure Reader for guest
  50. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  51. # configure Reader for host
  52. reader_0.get_party_instance(role='host', party_id=hosts).component_param(table=host_train_data)
  53. data_transform_0 = DataTransform(name="data_transform_0", output_format='dense')
  54. # get DataTransform party instance of guest
  55. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  56. # configure DataTransform for guest
  57. data_transform_0_guest_party_instance.component_param(with_label=True)
  58. # get and configure DataTransform party instance of host
  59. data_transform_0.get_party_instance(role='host', party_id=hosts).component_param(with_label=False)
  60. # define Intersection components
  61. intersection_0 = Intersection(name="intersection_0")
  62. pipeline.add_component(reader_0)
  63. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  64. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  65. # secure boost component
  66. hetero_secure_boost_0 = HeteroSecureBoost(name="hetero_secure_boost_0",
  67. num_trees=3,
  68. task_type="classification",
  69. objective_param={"objective": "cross_entropy"},
  70. encrypt_param={"method": "Paillier"},
  71. tree_param={"max_depth": 3},
  72. validation_freqs=1)
  73. hetero_secure_boost_1 = HeteroSecureBoost(name="hetero_secure_boost_1",
  74. num_trees=3,
  75. task_type="classification",
  76. objective_param={"objective": "cross_entropy"},
  77. encrypt_param={"method": "Paillier"},
  78. tree_param={"max_depth": 3},
  79. validation_freqs=1)
  80. pipeline.add_component(hetero_secure_boost_0, data=Data(train_data=intersection_0.output.data))
  81. pipeline.add_component(hetero_secure_boost_1, data=Data(train_data=intersection_0.output.data),
  82. model=Model(model=hetero_secure_boost_0.output.model))
  83. evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary")
  84. pipeline.add_component(evaluation_0, data=Data(data=hetero_secure_boost_1.output.data))
  85. pipeline.compile()
  86. # fit model
  87. pipeline.fit()
  88. # query component summary
  89. prettify(pipeline.get_component("hetero_secure_boost_0").get_summary())
  90. prettify(pipeline.get_component("hetero_secure_boost_1").get_summary())
  91. prettify(pipeline.get_component("evaluation_0").get_summary())
  92. return pipeline
  93. if __name__ == "__main__":
  94. parser = argparse.ArgumentParser("PIPELINE DEMO")
  95. parser.add_argument("-config", type=str,
  96. help="config file")
  97. args = parser.parse_args()
  98. if args.config is not None:
  99. main(args.config)
  100. else:
  101. main()