pipeline-hetero-lr-sample-weights.py 4.4 KB

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