pipeline-hetero-lr-cv.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 DataTransform
  20. from pipeline.component import HeteroSSHELR
  21. from pipeline.component import Intersection
  22. from pipeline.component import Reader
  23. from pipeline.interface import Data
  24. from pipeline.utils.tools import load_job_config
  25. def prettify(response, verbose=True):
  26. if verbose:
  27. print(json.dumps(response, indent=4, ensure_ascii=False))
  28. print()
  29. return response
  30. def main(config="../../config.yaml", namespace=""):
  31. if isinstance(config, str):
  32. config = load_job_config(config)
  33. parties = config.parties
  34. guest = parties.guest[0]
  35. hosts = parties.host[0]
  36. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  37. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  38. # guest_train_data = {"name": "default_credit_hetero_guest", "namespace": f"experiment{namespace}"}
  39. # host_train_data = {"name": "default_credit_hetero_host", "namespace": f"experiment{namespace}"}
  40. # initialize pipeline
  41. pipeline = PipeLine()
  42. # set job initiator
  43. pipeline.set_initiator(role='guest', party_id=guest)
  44. # set participants information
  45. pipeline.set_roles(guest=guest, host=hosts)
  46. # define Reader components to read in data
  47. reader_0 = Reader(name="reader_0")
  48. # configure Reader for guest
  49. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  50. # configure Reader for host
  51. reader_0.get_party_instance(role='host', party_id=hosts).component_param(table=host_train_data)
  52. data_transform_0 = DataTransform(name="data_transform_0", output_format='dense')
  53. # get DataTransform party instance of guest
  54. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  55. # configure DataTransform for guest
  56. data_transform_0_guest_party_instance.component_param(with_label=True)
  57. # get and configure DataTransform party instance of host
  58. data_transform_0.get_party_instance(role='host', party_id=hosts).component_param(with_label=False)
  59. # define Intersection components
  60. intersection_0 = Intersection(name="intersection_0")
  61. pipeline.add_component(reader_0)
  62. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  63. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  64. lr_param = {
  65. "name": "hetero_sshe_lr_0",
  66. "penalty": "L2",
  67. "optimizer": "rmsprop",
  68. "tol": 0.0001,
  69. "alpha": 0.01,
  70. "max_iter": 30,
  71. "early_stop": "diff",
  72. "batch_size": -1,
  73. "learning_rate": 0.15,
  74. "init_param": {
  75. "init_method": "zeros",
  76. "fit_intercept": True
  77. },
  78. "encrypt_param": {
  79. "key_length": 1024
  80. },
  81. "cv_param": {
  82. "n_splits": 3,
  83. "shuffle": False,
  84. "random_seed": 103,
  85. "need_cv": True
  86. },
  87. "reveal_every_iter": True,
  88. "reveal_strategy": "respectively"
  89. }
  90. hetero_sshe_lr_0 = HeteroSSHELR(**lr_param)
  91. pipeline.add_component(hetero_sshe_lr_0, data=Data(train_data=intersection_0.output.data))
  92. pipeline.compile()
  93. # fit model
  94. pipeline.fit()
  95. # query component summary
  96. prettify(pipeline.get_component("hetero_sshe_lr_0").get_summary())
  97. # pipeline.deploy_component([data_transform_0, intersection_0, hetero_sshe_lr_0])
  98. #
  99. # predict_pipeline = PipeLine()
  100. # # add data reader onto predict pipeline
  101. # predict_pipeline.add_component(reader_0)
  102. # # add selected components from train pipeline onto predict pipeline
  103. # # specify data source
  104. # predict_pipeline.add_component(pipeline,
  105. # data=Data(predict_input={pipeline.data_transform_0.input.data: reader_0.output.data}))
  106. # # run predict model
  107. # predict_pipeline.predict(job_parameters)
  108. return pipeline
  109. if __name__ == "__main__":
  110. parser = argparse.ArgumentParser("PIPELINE DEMO")
  111. parser.add_argument("-config", type=str,
  112. help="config file")
  113. args = parser.parse_args()
  114. if args.config is not None:
  115. main(args.config)
  116. else:
  117. main()