pipeline-hetero-sbt-multi-cv.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 DataTransform
  19. from pipeline.component import HeteroSecureBoost
  20. from pipeline.component import Intersection
  21. from pipeline.component import Reader
  22. from pipeline.interface import Data
  23. from pipeline.utils.tools import load_job_config
  24. def main(config="../../config.yaml", namespace=""):
  25. # obtain config
  26. if isinstance(config, str):
  27. config = load_job_config(config)
  28. parties = config.parties
  29. guest = parties.guest[0]
  30. host = parties.host[0]
  31. # data sets
  32. guest_train_data = {"name": "vehicle_scale_hetero_guest", "namespace": f"experiment{namespace}"}
  33. host_train_data = {"name": "vehicle_scale_hetero_host", "namespace": f"experiment{namespace}"}
  34. # init pipeline
  35. pipeline = PipeLine().set_initiator(role="guest", party_id=guest).set_roles(guest=guest, host=host,)
  36. # set data reader and data-io
  37. reader_0 = Reader(name="reader_0")
  38. reader_0.get_party_instance(role="guest", party_id=guest).component_param(table=guest_train_data)
  39. reader_0.get_party_instance(role="host", party_id=host).component_param(table=host_train_data)
  40. data_transform_0 = DataTransform(name="data_transform_0")
  41. data_transform_0.get_party_instance(
  42. role="guest", party_id=guest).component_param(
  43. with_label=True, output_format="dense")
  44. data_transform_0.get_party_instance(role="host", party_id=host).component_param(with_label=False)
  45. # data intersect component
  46. intersect_0 = Intersection(name="intersection_0")
  47. # secure boost component
  48. hetero_secure_boost_0 = HeteroSecureBoost(name="hetero_secure_boost_0",
  49. num_trees=3,
  50. task_type="classification",
  51. objective_param={"objective": "cross_entropy"},
  52. encrypt_param={"method": "Paillier"},
  53. tree_param={"max_depth": 3},
  54. validation_freqs=1,
  55. cv_param={
  56. "need_cv": True,
  57. "n_splits": 5,
  58. "shuffle": False,
  59. "random_seed": 103
  60. }
  61. )
  62. pipeline.add_component(reader_0)
  63. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  64. pipeline.add_component(intersect_0, data=Data(data=data_transform_0.output.data))
  65. pipeline.add_component(hetero_secure_boost_0, data=Data(train_data=intersect_0.output.data))
  66. pipeline.compile()
  67. pipeline.fit()
  68. print("fitting hetero secureboost done, result:")
  69. print(pipeline.get_component("hetero_secure_boost_0").get_summary())
  70. if __name__ == "__main__":
  71. parser = argparse.ArgumentParser("PIPELINE DEMO")
  72. parser.add_argument("-config", type=str,
  73. help="config file")
  74. args = parser.parse_args()
  75. if args.config is not None:
  76. main(args.config)
  77. else:
  78. main()