pipeline-homo-lr-one-vs-all.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Evaluation
  21. from pipeline.component import HomoLR
  22. from pipeline.component import Reader
  23. from pipeline.interface import Data
  24. from pipeline.interface import Model
  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. arbiter = parties.arbiter[0]
  38. guest_train_data = {"name": "vehicle_scale_homo_guest", "namespace": f"experiment{namespace}"}
  39. host_train_data = {"name": "vehicle_scale_homo_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, arbiter=arbiter)
  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', with_label=True)
  53. pipeline.add_component(reader_0)
  54. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  55. lr_param = {
  56. "penalty": "L2",
  57. "optimizer": "sgd",
  58. "tol": 1e-05,
  59. "alpha": 0.01,
  60. "early_stop": "diff",
  61. "batch_size": -1,
  62. "learning_rate": 0.15,
  63. "decay": 1,
  64. "decay_sqrt": True,
  65. "init_param": {
  66. "init_method": "zeros"
  67. },
  68. "cv_param": {
  69. "n_splits": 4,
  70. "shuffle": True,
  71. "random_seed": 33,
  72. "need_cv": False
  73. },
  74. "callback_param": {
  75. "callbacks": ["ModelCheckpoint", "EarlyStopping"]
  76. }
  77. }
  78. homo_lr_0 = HomoLR(name="homo_lr_0", max_iter=1, **lr_param)
  79. homo_lr_1 = HomoLR(name="homo_lr_1")
  80. pipeline.add_component(homo_lr_0, data=Data(train_data=data_transform_0.output.data))
  81. pipeline.add_component(homo_lr_1, data=Data(test_data=data_transform_0.output.data),
  82. model=Model(model=homo_lr_0.output.model))
  83. evaluation_0 = Evaluation(name="evaluation_0", eval_type="multi")
  84. pipeline.add_component(evaluation_0, data=Data(data=[homo_lr_0.output.data,
  85. homo_lr_1.output.data]))
  86. pipeline.compile()
  87. # fit model
  88. pipeline.fit()
  89. # query component summary
  90. prettify(pipeline.get_component("evaluation_0").get_summary())
  91. return pipeline
  92. if __name__ == "__main__":
  93. parser = argparse.ArgumentParser("PIPELINE DEMO")
  94. parser.add_argument("-config", type=str,
  95. help="config file")
  96. args = parser.parse_args()
  97. if args.config is not None:
  98. main(args.config)
  99. else:
  100. main()