pipeline-hetero-lr-no-intercept.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 Evaluation
  20. from pipeline.component import HeteroLR
  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 main(config="../../config.yaml", namespace=""):
  26. # obtain config
  27. if isinstance(config, str):
  28. config = load_job_config(config)
  29. parties = config.parties
  30. guest = parties.guest[0]
  31. host = parties.host[0]
  32. arbiter = parties.arbiter[0]
  33. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  34. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  35. # initialize pipeline
  36. pipeline = PipeLine()
  37. # set job initiator
  38. pipeline.set_initiator(role='guest', party_id=guest)
  39. # set participants information
  40. pipeline.set_roles(guest=guest, host=host, arbiter=arbiter)
  41. # define Reader components to read in data
  42. reader_0 = Reader(name="reader_0")
  43. # configure Reader for guest
  44. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  45. # configure Reader for host
  46. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  47. data_transform_0 = DataTransform(name="data_transform_0", output_format='dense')
  48. # get DataTransform party instance of guest
  49. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  50. # configure DataTransform for guest
  51. data_transform_0_guest_party_instance.component_param(with_label=True)
  52. # get and configure DataTransform party instance of host
  53. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  54. # define Intersection components
  55. intersection_0 = Intersection(name="intersection_0")
  56. pipeline.add_component(reader_0)
  57. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  58. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  59. lr_param = {
  60. "penalty": "L2",
  61. "optimizer": "rmsprop",
  62. "tol": 0.0001,
  63. "alpha": 0.01,
  64. "max_iter": 30,
  65. "early_stop": "diff",
  66. "batch_size": 320,
  67. "learning_rate": 0.15,
  68. "init_param": {
  69. "init_method": "zeros",
  70. "fit_intercept": False
  71. },
  72. "sqn_param": {
  73. "update_interval_L": 3,
  74. "memory_M": 5,
  75. "sample_size": 5000,
  76. "random_seed": None
  77. },
  78. "cv_param": {
  79. "n_splits": 5,
  80. "shuffle": False,
  81. "random_seed": 103,
  82. "need_cv": False
  83. },
  84. "callback_param": {
  85. "callbacks": ["ModelCheckpoint"],
  86. "save_freq": 1
  87. }
  88. }
  89. hetero_lr_0 = HeteroLR(name="hetero_lr_0", **lr_param)
  90. pipeline.add_component(hetero_lr_0, data=Data(train_data=intersection_0.output.data))
  91. evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary")
  92. pipeline.add_component(evaluation_0, data=Data(data=hetero_lr_0.output.data))
  93. pipeline.compile()
  94. pipeline.fit()
  95. if __name__ == "__main__":
  96. parser = argparse.ArgumentParser("PIPELINE DEMO")
  97. parser.add_argument("-config", type=str,
  98. help="config file")
  99. args = parser.parse_args()
  100. if args.config is not None:
  101. main(args.config)
  102. else:
  103. main()