pipeline-hetero-lr-cv.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 import HeteroLR
  20. from pipeline.component import Intersection
  21. from pipeline.component import Reader
  22. from pipeline.interface import Data
  23. from pipeline.utils.tools import load_job_config
  24. def main(config="../../config.yaml", namespace=""):
  25. if isinstance(config, str):
  26. config = load_job_config(config)
  27. parties = config.parties
  28. guest = parties.guest[0]
  29. host = parties.host[0]
  30. arbiter = parties.arbiter[0]
  31. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  32. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  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. data_transform_0 = DataTransform(name="data_transform_0", output_format='dense')
  46. # get DataTransform party instance of guest
  47. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  48. # configure DataTransform for guest
  49. data_transform_0_guest_party_instance.component_param(with_label=True)
  50. # get and configure DataTransform party instance of host
  51. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  52. # define Intersection components
  53. intersection_0 = Intersection(name="intersection_0")
  54. pipeline.add_component(reader_0)
  55. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  56. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  57. lr_param = {
  58. "penalty": "L2",
  59. "optimizer": "rmsprop",
  60. "tol": 0.0001,
  61. "alpha": 0.01,
  62. "max_iter": 10,
  63. "early_stop": "diff",
  64. "batch_size": -1,
  65. "learning_rate": 0.15,
  66. "init_param": {
  67. "init_method": "zeros"
  68. },
  69. "cv_param": {
  70. "n_splits": 3,
  71. "shuffle": False,
  72. "random_seed": 103,
  73. "need_cv": True
  74. }
  75. }
  76. hetero_lr_0 = HeteroLR(name="hetero_lr_0", **lr_param)
  77. pipeline.add_component(hetero_lr_0, data=Data(train_data=intersection_0.output.data))
  78. pipeline.compile()
  79. pipeline.fit()
  80. if __name__ == "__main__":
  81. parser = argparse.ArgumentParser("PIPELINE DEMO")
  82. parser.add_argument("-config", type=str,
  83. help="config file")
  84. args = parser.parse_args()
  85. if args.config is not None:
  86. main(args.config)
  87. else:
  88. main()