pipeline-homo-lr-sample-weights.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. 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 Evaluation
  22. from pipeline.component import SampleWeight
  23. from pipeline.component import Reader
  24. from pipeline.component import FeatureScale
  25. from pipeline.interface import Data
  26. from pipeline.utils.tools import load_job_config
  27. def main(config="../../config.yaml", namespace=""):
  28. # obtain config
  29. if isinstance(config, str):
  30. config = load_job_config(config)
  31. parties = config.parties
  32. guest = parties.guest[0]
  33. host = parties.host[0]
  34. arbiter = parties.arbiter[0]
  35. guest_train_data = {"name": "breast_homo_guest", "namespace": f"experiment{namespace}"}
  36. host_train_data = {"name": "breast_homo_host", "namespace": f"experiment{namespace}"}
  37. # initialize pipeline
  38. pipeline = PipeLine()
  39. # set job initiator
  40. pipeline.set_initiator(role='guest', party_id=guest)
  41. # set participants information
  42. pipeline.set_roles(guest=guest, host=host, arbiter=arbiter)
  43. # define Reader components to read in data
  44. reader_0 = Reader(name="reader_0")
  45. # configure Reader for guest
  46. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  47. # configure Reader for host
  48. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  49. # define DataTransform components
  50. data_transform_0 = DataTransform(
  51. name="data_transform_0",
  52. with_label=True,
  53. output_format="dense") # start component numbering at 0
  54. scale_0 = FeatureScale(name='scale_0')
  55. sample_weight_0 = SampleWeight(name="sample_weight_0", class_weight={"0": 1, "1": 2})
  56. param = {
  57. "penalty": "L2",
  58. "optimizer": "sgd",
  59. "tol": 1e-05,
  60. "alpha": 0.01,
  61. "max_iter": 3,
  62. "early_stop": "diff",
  63. "batch_size": 320,
  64. "learning_rate": 0.15,
  65. "decay": 1.0,
  66. "decay_sqrt": True,
  67. "init_param": {
  68. "init_method": "zeros"
  69. },
  70. "cv_param": {
  71. "n_splits": 5,
  72. "shuffle": True,
  73. "random_seed": 33,
  74. "need_cv": False
  75. }
  76. }
  77. homo_lr_0 = HomoLR(name='homo_lr_0', **param)
  78. evaluation_0 = Evaluation(name='evaluation_0')
  79. # add components to pipeline, in order of task execution
  80. pipeline.add_component(reader_0)
  81. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  82. # set data input sources of intersection components
  83. pipeline.add_component(scale_0, data=Data(data=data_transform_0.output.data))
  84. pipeline.add_component(sample_weight_0, data=Data(data=scale_0.output.data))
  85. pipeline.add_component(homo_lr_0, data=Data(train_data=sample_weight_0.output.data))
  86. pipeline.add_component(evaluation_0, data=Data(data=homo_lr_0.output.data))
  87. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  88. pipeline.compile()
  89. # fit model
  90. pipeline.fit()
  91. # query component summary
  92. print(json.dumps(pipeline.get_component("evaluation_0").get_summary(), indent=4, ensure_ascii=False))
  93. if __name__ == "__main__":
  94. parser = argparse.ArgumentParser("PIPELINE DEMO")
  95. parser.add_argument("-config", type=str,
  96. help="config file")
  97. args = parser.parse_args()
  98. if args.config is not None:
  99. main(args.config)
  100. else:
  101. main()