pipeline-homo-lr-cv.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 HomoLR
  21. from pipeline.component import Reader
  22. from pipeline.component import FeatureScale
  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_homo_guest", "namespace": f"experiment{namespace}"}
  34. host_train_data = {"name": "breast_homo_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. # define DataTransform components
  48. data_transform_0 = DataTransform(
  49. name="data_transform_0",
  50. with_label=True,
  51. output_format="dense") # start component numbering at 0
  52. scale_0 = FeatureScale(name='scale_0')
  53. param = {
  54. "penalty": "L2",
  55. "optimizer": "sgd",
  56. "tol": 1e-05,
  57. "alpha": 0.01,
  58. "max_iter": 3,
  59. "early_stop": "diff",
  60. "batch_size": 320,
  61. "learning_rate": 0.15,
  62. "decay": 1.0,
  63. "decay_sqrt": True,
  64. "init_param": {
  65. "init_method": "zeros"
  66. },
  67. "cv_param": {
  68. "n_splits": 5,
  69. "shuffle": True,
  70. "random_seed": 33,
  71. "need_cv": True
  72. }
  73. }
  74. homo_lr_0 = HomoLR(name='homo_lr_0', **param)
  75. # add components to pipeline, in order of task execution
  76. pipeline.add_component(reader_0)
  77. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  78. # set data input sources of intersection components
  79. pipeline.add_component(scale_0, data=Data(data=data_transform_0.output.data))
  80. pipeline.add_component(homo_lr_0, data=Data(train_data=scale_0.output.data))
  81. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  82. pipeline.compile()
  83. # fit model
  84. pipeline.fit()
  85. # query component summary
  86. print(json.dumps(pipeline.get_component("homo_lr_0").get_summary(), indent=4, ensure_ascii=False))
  87. if __name__ == "__main__":
  88. parser = argparse.ArgumentParser("PIPELINE DEMO")
  89. parser.add_argument("-config", type=str,
  90. help="config file")
  91. args = parser.parse_args()
  92. if args.config is not None:
  93. main(args.config)
  94. else:
  95. main()