pipeline-quick-demo.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 json
  17. from pipeline.backend.pipeline import PipeLine
  18. from pipeline.component import Reader, DataTransform, Intersection, HeteroSecureBoost, Evaluation
  19. from pipeline.interface import Data
  20. # table name & namespace in data storage
  21. # data should be uploaded before running modeling task
  22. guest_train_data = {"name": "breast_hetero_guest", "namespace": "experiment"}
  23. host_train_data = {"name": "breast_hetero_host", "namespace": "experiment"}
  24. # initialize pipeline
  25. # Party ids are indicators of parties involved in federated learning. For standalone mode,
  26. # arbitrary integers can be used as party id.
  27. pipeline = PipeLine().set_initiator(role="guest", party_id=9999).set_roles(guest=9999, host=10000)
  28. # define components
  29. # Reader is a component to obtain the uploaded data. This component are very likely to be needed.
  30. reader_0 = Reader(name="reader_0")
  31. # By the following way, you can set different parameters for different party.
  32. reader_0.get_party_instance(role="guest", party_id=9999).component_param(table=guest_train_data)
  33. reader_0.get_party_instance(role="host", party_id=10000).component_param(table=host_train_data)
  34. # Data transform provided some preprocessing to the raw data, including extract label, convert data format,
  35. # filling missing value and so on. You may refer to the algorithm list doc for more details.
  36. data_transform_0 = DataTransform(name="data_transform_0", with_label=True)
  37. data_transform_0.get_party_instance(role="host", party_id=10000).component_param(with_label=False)
  38. # Perform PSI for hetero-scenario.
  39. intersect_0 = Intersection(name="intersection_0")
  40. # Define a hetero-secureboost component. The following parameters will be set for all parties involved.
  41. hetero_secureboost_0 = HeteroSecureBoost(name="hetero_secureboost_0",
  42. num_trees=5,
  43. bin_num=16,
  44. task_type="classification",
  45. objective_param={"objective": "cross_entropy"},
  46. encrypt_param={"method": "paillier"},
  47. tree_param={"max_depth": 3})
  48. # To show the evaluation result, an "Evaluation" component is needed.
  49. evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary")
  50. # add components to pipeline, in order of task execution
  51. # The components are connected by indicating upstream data output as their input.
  52. # Typically, a feature engineering component will indicate input data as "data" while
  53. # the modeling component will use "train_data". Please check out carefully of the difference
  54. # between hetero_secureboost_0 input and other components below.
  55. # Here we are just showing a simple example, for more details of other components, please check
  56. # out the examples in "example/pipeline/{component you are interested in}
  57. pipeline.add_component(reader_0)\
  58. .add_component(data_transform_0, data=Data(data=reader_0.output.data))\
  59. .add_component(intersect_0, data=Data(data=data_transform_0.output.data))\
  60. .add_component(hetero_secureboost_0, data=Data(train_data=intersect_0.output.data))\
  61. .add_component(evaluation_0, data=Data(data=hetero_secureboost_0.output.data))
  62. # compile & fit pipeline
  63. pipeline.compile().fit()
  64. # query component summary
  65. print(f"Evaluation summary:\n{json.dumps(pipeline.get_component('evaluation_0').get_summary(), indent=4)}")