pipeline-positive-unlabeled-sshe-lr.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. import json
  18. from pipeline.backend.pipeline import PipeLine
  19. from pipeline.component import Reader
  20. from pipeline.component import DataTransform
  21. from pipeline.component import Intersection
  22. from pipeline.component import HeteroSSHELR
  23. from pipeline.component import PositiveUnlabeled
  24. from pipeline.interface import Data
  25. from pipeline.utils.tools import load_job_config
  26. def prettify(response, verbose=True):
  27. if verbose:
  28. print(json.dumps(response, indent=4, ensure_ascii=False))
  29. print()
  30. return response
  31. def main(config="../../config.yaml", namespace=""):
  32. if isinstance(config, str):
  33. config = load_job_config(config)
  34. parties = config.parties
  35. guest = parties.guest[0]
  36. hosts = parties.host[0]
  37. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  38. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  39. # initialize pipeline
  40. pipeline = PipeLine()
  41. # set job initiator
  42. pipeline.set_initiator(role='guest', party_id=guest)
  43. # set participants information
  44. pipeline.set_roles(guest=guest, host=hosts)
  45. # define Reader components
  46. reader_0 = Reader(name="reader_0")
  47. # configure Reader for guest
  48. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  49. # configure Reader for host
  50. reader_0.get_party_instance(role='host', party_id=hosts).component_param(table=host_train_data)
  51. # define DataTransform components
  52. data_transform_0 = DataTransform(name="data_transform_0", output_format='dense')
  53. # configure DataTransform for guest
  54. data_transform_0.get_party_instance(role='guest', party_id=guest).component_param(with_label=True)
  55. # configure DataTransform for host
  56. data_transform_0.get_party_instance(role='host', party_id=hosts).component_param(with_label=False)
  57. # define Intersection components
  58. intersection_0 = Intersection(name="intersection_0")
  59. # define SSHE-LR and PositiveUnlabeled components
  60. sshe_lr_0_param = {
  61. "name": "hetero_sshe_lr_0",
  62. "max_iter": 2
  63. }
  64. pu_0_param = {
  65. "name": "positive_unlabeled_0",
  66. "strategy": "probability",
  67. "threshold": 0.9
  68. }
  69. sshe_lr_1_param = {
  70. "name": "hetero_sshe_lr_1",
  71. "max_iter": 1
  72. }
  73. hetero_sshe_lr_0 = HeteroSSHELR(**sshe_lr_0_param)
  74. positive_unlabeled_0 = PositiveUnlabeled(**pu_0_param)
  75. hetero_sshe_lr_1 = HeteroSSHELR(**sshe_lr_1_param)
  76. # configure pipeline components
  77. pipeline.add_component(reader_0)
  78. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  79. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  80. pipeline.add_component(hetero_sshe_lr_0, data=Data(train_data=intersection_0.output.data))
  81. pipeline.add_component(positive_unlabeled_0,
  82. data=Data(data=[intersection_0.output.data, hetero_sshe_lr_0.output.data]))
  83. pipeline.add_component(hetero_sshe_lr_1, data=Data(train_data=positive_unlabeled_0.output.data))
  84. pipeline.compile()
  85. # fit model
  86. pipeline.fit()
  87. # query component summary
  88. prettify(pipeline.get_component("positive_unlabeled_0").get_summary())
  89. if __name__ == "__main__":
  90. parser = argparse.ArgumentParser("PIPELINE DEMO")
  91. parser.add_argument("-config", type=str, help="config file")
  92. args = parser.parse_args()
  93. if args.config is not None:
  94. main(args.config)
  95. else:
  96. main()