pipeline-mini-demo.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. from pipeline.backend.pipeline import PipeLine
  17. from pipeline.component import DataTransform
  18. from pipeline.component import HeteroLR
  19. from pipeline.component import Intersection
  20. from pipeline.component import Reader
  21. from pipeline.interface import Data
  22. from pipeline.interface import Model
  23. def main():
  24. # parties config
  25. guest = 9999
  26. host = 10000
  27. arbiter = 10000
  28. # specify input data name & namespace in database
  29. guest_train_data = {"name": "breast_hetero_guest", "namespace": "experiment"}
  30. host_train_data = {"name": "breast_hetero_host", "namespace": "experiment"}
  31. guest_eval_data = {"name": "breast_hetero_guest", "namespace": "experiment"}
  32. host_eval_data = {"name": "breast_hetero_host", "namespace": "experiment"}
  33. # initialize pipeline
  34. pipeline = PipeLine()
  35. # set job initiator
  36. pipeline.set_initiator(role="guest", party_id=guest)
  37. # set participants information
  38. pipeline.set_roles(guest=guest, host=host, arbiter=arbiter)
  39. # define Reader components to read in data
  40. reader_0 = Reader(name="reader_0")
  41. # configure Reader for guest
  42. reader_0.get_party_instance(role="guest", party_id=guest).component_param(table=guest_train_data)
  43. # configure Reader for host
  44. reader_0.get_party_instance(role="host", party_id=host).component_param(table=host_train_data)
  45. reader_1 = Reader(name="reader_1")
  46. reader_1.get_party_instance(role="guest", party_id=guest).component_param(table=guest_eval_data)
  47. reader_1.get_party_instance(role="host", party_id=host).component_param(table=host_eval_data)
  48. # define DataTransform components
  49. data_transform_0 = DataTransform(name="data_transform_0")
  50. data_transform_1 = DataTransform(name="data_transform_1")
  51. # get DataTransform party instance of guest
  52. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role="guest", party_id=guest)
  53. # configure DataTransform for guest
  54. data_transform_0_guest_party_instance.component_param(with_label=True, output_format="dense")
  55. # get and configure DataTransform party instance of host
  56. data_transform_0.get_party_instance(role="host", party_id=host).component_param(with_label=False)
  57. # define Intersection components
  58. intersection_0 = Intersection(name="intersection_0")
  59. intersection_1 = Intersection(name="intersection_1")
  60. # define HeteroLR component
  61. hetero_lr_0 = HeteroLR(name="hetero_lr_0", early_stop="weight_diff", learning_rate=0.15, optimizer="rmsprop",
  62. max_iter=10, early_stopping_rounds=2, validation_freqs=1)
  63. # add components to pipeline, in order of task execution
  64. pipeline.add_component(reader_0)
  65. pipeline.add_component(reader_1)
  66. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  67. # set data_transform_1 to replicate model from data_transform_0
  68. pipeline.add_component(data_transform_1,
  69. data=Data(data=reader_1.output.data), model=Model(data_transform_0.output.model))
  70. # set data input sources of intersection components
  71. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  72. pipeline.add_component(intersection_1, data=Data(data=data_transform_1.output.data))
  73. # set train & validate data of hetero_lr_0 component
  74. pipeline.add_component(
  75. hetero_lr_0,
  76. data=Data(
  77. train_data=intersection_0.output.data,
  78. validate_data=intersection_1.output.data))
  79. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  80. pipeline.compile()
  81. # fit model
  82. pipeline.fit()
  83. # query component summary
  84. import json
  85. print(json.dumps(pipeline.get_component("hetero_lr_0").get_summary(), indent=4))
  86. # predict
  87. # deploy required components
  88. pipeline.deploy_component([data_transform_0, intersection_0, hetero_lr_0])
  89. # initiate predict pipeline
  90. predict_pipeline = PipeLine()
  91. reader_2 = Reader(name="reader_2")
  92. reader_2.get_party_instance(role="guest", party_id=guest).component_param(table=guest_eval_data)
  93. reader_2.get_party_instance(role="host", party_id=host).component_param(table=host_eval_data)
  94. # add data reader onto predict pipeline
  95. predict_pipeline.add_component(reader_2)
  96. # add selected components from train pipeline onto predict pipeline
  97. # specify data source
  98. predict_pipeline.add_component(pipeline, data=Data(
  99. predict_input={pipeline.data_transform_0.input.data: reader_2.output.data}))
  100. # run predict model
  101. predict_pipeline.predict()
  102. if __name__ == "__main__":
  103. main()