pipeline-homo-sbt-binary-multi-host.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.homo_secureboost import HomoSecureBoost
  20. from pipeline.component.reader import Reader
  21. from pipeline.interface.data import Data
  22. from pipeline.component.evaluation import Evaluation
  23. from pipeline.interface.model import Model
  24. from pipeline.utils.tools import load_job_config
  25. def main(config="../../config.yaml", namespace=""):
  26. # obtain config
  27. if isinstance(config, str):
  28. config = load_job_config(config)
  29. parties = config.parties
  30. guest = parties.guest[0]
  31. host_0 = parties.host[0]
  32. host_1 = parties.host[1]
  33. arbiter = parties.arbiter[0]
  34. guest_train_data = {"name": "default_credit_guest", "namespace": f"experiment{namespace}"}
  35. host_train_data_0 = {"name": "default_credit_host1", "namespace": f"experiment{namespace}"}
  36. host_train_data_1 = {"name": "default_credit_host2", "namespace": f"experiment{namespace}"}
  37. guest_validate_data = {"name": "default_credit_test", "namespace": f"experiment{namespace}"}
  38. host_validate_data_0 = {"name": "default_credit_test", "namespace": f"experiment{namespace}"}
  39. host_validate_data_1 = {"name": "default_credit_test", "namespace": f"experiment{namespace}"}
  40. pipeline = PipeLine().set_initiator(
  41. role='guest', party_id=guest).set_roles(
  42. guest=guest, host=[
  43. host_0, host_1], arbiter=arbiter)
  44. data_transform_0, data_transform_1 = DataTransform(name="data_transform_0"), DataTransform(name='data_transform_1')
  45. reader_0, reader_1 = Reader(name="reader_0"), Reader(name='reader_1')
  46. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  47. reader_0.get_party_instance(role='host', party_id=host_0).component_param(table=host_train_data_0)
  48. reader_0.get_party_instance(role='host', party_id=host_1).component_param(table=host_train_data_1)
  49. data_transform_0.get_party_instance(
  50. role='guest', party_id=guest).component_param(
  51. with_label=True, output_format="dense")
  52. data_transform_0.get_party_instance(
  53. role='host', party_id=host_0).component_param(
  54. with_label=True, output_format="dense")
  55. data_transform_0.get_party_instance(
  56. role='host', party_id=host_1).component_param(
  57. with_label=True, output_format="dense")
  58. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_validate_data)
  59. reader_1.get_party_instance(role='host', party_id=host_0).component_param(table=host_validate_data_0)
  60. reader_1.get_party_instance(role='host', party_id=host_1).component_param(table=host_validate_data_1)
  61. data_transform_1.get_party_instance(
  62. role='guest', party_id=guest).component_param(
  63. with_label=True, output_format="dense")
  64. data_transform_1.get_party_instance(
  65. role='host', party_id=host_0).component_param(
  66. with_label=True, output_format="dense")
  67. data_transform_1.get_party_instance(
  68. role='host', party_id=host_1).component_param(
  69. with_label=True, output_format="dense")
  70. homo_secureboost_0 = HomoSecureBoost(name="homo_secureboost_0",
  71. num_trees=3,
  72. task_type='classification',
  73. objective_param={"objective": "cross_entropy"},
  74. tree_param={
  75. "max_depth": 3
  76. },
  77. validation_freqs=1
  78. )
  79. evaluation_0 = Evaluation(name='evaluation_0', eval_type='binary')
  80. pipeline.add_component(reader_0)
  81. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  82. pipeline.add_component(reader_1)
  83. pipeline.add_component(
  84. data_transform_1, data=Data(
  85. data=reader_1.output.data), model=Model(
  86. data_transform_0.output.model))
  87. pipeline.add_component(homo_secureboost_0, data=Data(train_data=data_transform_0.output.data,
  88. validate_data=data_transform_1.output.data
  89. ))
  90. pipeline.add_component(evaluation_0, data=Data(homo_secureboost_0.output.data))
  91. pipeline.compile()
  92. pipeline.fit()
  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()