pipeline-label-transform.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 LabelTransform
  19. from pipeline.component import Evaluation
  20. from pipeline.component import HeteroLR
  21. from pipeline.component import DataTransform
  22. from pipeline.component import Intersection
  23. from pipeline.component import Reader
  24. from pipeline.interface import Data, 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. arbiter = parties.arbiter[0]
  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. # initialize pipeline
  37. pipeline = PipeLine()
  38. # set job initiator
  39. pipeline.set_initiator(role="guest", party_id=guest).set_roles(guest=guest, host=host, arbiter=arbiter)
  40. # define Reader components to read in data
  41. reader_0 = Reader(name="reader_0")
  42. # configure Reader for guest
  43. reader_0.get_party_instance(role="guest", party_id=guest).component_param(table=guest_train_data)
  44. reader_0.get_party_instance(role="host", party_id=host).component_param(table=host_train_data)
  45. data_transform_0 = DataTransform(name="data_transform_0") # start component numbering at 0
  46. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role="guest", party_id=guest)
  47. data_transform_0_guest_party_instance.component_param(with_label=True, output_format="dense")
  48. data_transform_0.get_party_instance(role="host", party_id=host).component_param(with_label=False,
  49. output_format="dense")
  50. intersection_0 = Intersection(name="intersection_0")
  51. label_transform_0 = LabelTransform(name="label_transform_0")
  52. label_transform_0.get_party_instance(role="host", party_id=host).component_param(need_run=False)
  53. hetero_lr_0 = HeteroLR(name="hetero_lr_0", penalty="L2", optimizer="sgd", tol=0.001,
  54. alpha=0.01, max_iter=20, early_stop="weight_diff", batch_size=-1,
  55. learning_rate=0.15, decay=0.0, decay_sqrt=False,
  56. init_param={"init_method": "zeros"},
  57. floating_point_precision=23)
  58. label_transform_1 = LabelTransform(name="label_transform_1")
  59. label_transform_1.get_party_instance(role="host", party_id=host).component_param(need_run=False)
  60. evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary", pos_label=1)
  61. # add components to pipeline, in order of task execution
  62. pipeline.add_component(reader_0)
  63. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  64. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  65. pipeline.add_component(label_transform_0, data=Data(data=intersection_0.output.data))
  66. pipeline.add_component(hetero_lr_0, data=Data(train_data=label_transform_0.output.data))
  67. pipeline.add_component(
  68. label_transform_1, data=Data(
  69. data=hetero_lr_0.output.data), model=Model(
  70. label_transform_0.output.model))
  71. pipeline.add_component(evaluation_0, data=Data(data=label_transform_1.output.data))
  72. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  73. pipeline.compile()
  74. # fit model
  75. pipeline.fit()
  76. deploy_components = [data_transform_0, intersection_0, label_transform_0, hetero_lr_0, label_transform_1]
  77. pipeline.deploy_component(components=deploy_components)
  78. predict_pipeline = PipeLine()
  79. # # add data reader onto predict pipeline
  80. predict_pipeline.add_component(reader_0)
  81. # # add selected components from train pipeline onto predict pipeline
  82. # # specify data source
  83. predict_pipeline.add_component(
  84. pipeline, data=Data(
  85. predict_input={
  86. pipeline.data_transform_0.input.data: reader_0.output.data}))
  87. predict_pipeline.compile()
  88. predict_pipeline.predict()
  89. if __name__ == "__main__":
  90. parser = argparse.ArgumentParser("PIPELINE DEMO")
  91. parser.add_argument("-config", type=str,
  92. help="config file")
  93. args = parser.parse_args()
  94. if args.config is not None:
  95. main(args.config)
  96. else:
  97. main()