pipeline-hetero-sbt-EINI-with-random-mask.py 5.8 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.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.component import Evaluation
  24. from pipeline.interface import Model
  25. from pipeline.utils.tools import load_job_config
  26. def main(config="../../config.yaml", namespace=""):
  27. # obtain config
  28. if isinstance(config, str):
  29. config = load_job_config(config)
  30. parties = config.parties
  31. guest = parties.guest[0]
  32. host = parties.host[0]
  33. # data sets
  34. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  35. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  36. guest_validate_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  37. host_validate_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  38. # init pipeline
  39. pipeline = PipeLine().set_initiator(role="guest", party_id=guest).set_roles(guest=guest, host=host,)
  40. # set data reader and data-io
  41. reader_0, reader_1 = Reader(name="reader_0"), Reader(name="reader_1")
  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.get_party_instance(role="guest", party_id=guest).component_param(table=guest_validate_data)
  45. reader_1.get_party_instance(role="host", party_id=host).component_param(table=host_validate_data)
  46. data_transform_0, data_transform_1 = DataTransform(name="data_transform_0"), DataTransform(name="data_transform_1")
  47. data_transform_0.get_party_instance(
  48. role="guest", party_id=guest).component_param(
  49. with_label=True, output_format="dense")
  50. data_transform_0.get_party_instance(role="host", party_id=host).component_param(with_label=False)
  51. data_transform_1.get_party_instance(
  52. role="guest", party_id=guest).component_param(
  53. with_label=True, output_format="dense")
  54. data_transform_1.get_party_instance(role="host", party_id=host).component_param(with_label=False)
  55. # data intersect component
  56. intersect_0 = Intersection(name="intersection_0")
  57. intersect_1 = Intersection(name="intersection_1")
  58. # secure boost component
  59. hetero_secure_boost_0 = HeteroSecureBoost(name="hetero_secure_boost_0",
  60. num_trees=3,
  61. task_type="classification",
  62. objective_param={"objective": "cross_entropy"},
  63. encrypt_param={"method": "Paillier"},
  64. tree_param={"max_depth": 3},
  65. validation_freqs=1,
  66. EINI_inference=True,
  67. EINI_random_mask=True
  68. )
  69. # evaluation component
  70. evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary")
  71. pipeline.add_component(reader_0)
  72. pipeline.add_component(reader_1)
  73. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  74. pipeline.add_component(
  75. data_transform_1, data=Data(
  76. data=reader_1.output.data), model=Model(
  77. data_transform_0.output.model))
  78. pipeline.add_component(intersect_0, data=Data(data=data_transform_0.output.data))
  79. pipeline.add_component(intersect_1, data=Data(data=data_transform_1.output.data))
  80. pipeline.add_component(hetero_secure_boost_0, data=Data(train_data=intersect_0.output.data,
  81. validate_data=intersect_1.output.data))
  82. pipeline.add_component(evaluation_0, data=Data(data=hetero_secure_boost_0.output.data))
  83. pipeline.compile()
  84. pipeline.fit()
  85. print("fitting hetero secureboost done, result:")
  86. print(pipeline.get_component("hetero_secure_boost_0").get_summary())
  87. print('start to predict')
  88. # predict
  89. # deploy required components
  90. pipeline.deploy_component([data_transform_0, intersect_0, hetero_secure_boost_0, evaluation_0])
  91. predict_pipeline = PipeLine()
  92. # add data reader onto predict pipeline
  93. predict_pipeline.add_component(reader_0)
  94. # add selected components from train pipeline onto predict pipeline
  95. # specify data source
  96. predict_pipeline.add_component(
  97. pipeline, data=Data(
  98. predict_input={
  99. pipeline.data_transform_0.input.data: reader_0.output.data}))
  100. # run predict model
  101. predict_pipeline.predict()
  102. predict_result = predict_pipeline.get_component("hetero_secure_boost_0").get_output_data()
  103. print("Showing 10 data of predict result")
  104. print(predict_result.head(10))
  105. if __name__ == "__main__":
  106. parser = argparse.ArgumentParser("PIPELINE DEMO")
  107. parser.add_argument("-config", type=str,
  108. help="config file")
  109. args = parser.parse_args()
  110. if args.config is not None:
  111. main(args.config)
  112. else:
  113. main()