pipeline-sample-weight-name.py 4.8 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 SampleWeight, FederatedSample
  22. from pipeline.component import Intersection
  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_hetero_guest_sid", "namespace": f"experiment{namespace}"}
  36. host_train_data = {"name": "breast_hetero_host_sid", "namespace": f"experiment{namespace}"}
  37. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host, arbiter=arbiter)
  38. reader_0 = Reader(name="reader_0")
  39. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  40. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  41. data_transform_0 = DataTransform(name="data_transform_0", with_match_id=True)
  42. data_transform_0.get_party_instance(
  43. role='guest',
  44. party_id=guest).component_param(
  45. with_label=True,
  46. label_name="y",
  47. label_type="int",
  48. output_format="dense")
  49. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  50. intersection_0 = Intersection(name="intersection_0")
  51. scale_0 = FeatureScale(name="scale_0", method="min_max_scale", mode="normal", scale_names=["x0"])
  52. sample_weight_0 = SampleWeight(name="sample_weight_0")
  53. sample_weight_0.get_party_instance(role='guest', party_id=guest).component_param(need_run=True,
  54. sample_weight_name="x0")
  55. sample_weight_0.get_party_instance(role='host', party_id=host).component_param(need_run=False)
  56. federated_sampler_0 = FederatedSample(name="federated_sampler_0", mode="exact_by_weight")
  57. hetero_lr_0 = HeteroLR(name="hetero_lr_0", optimizer="sgd", tol=0.001,
  58. alpha=0.01, max_iter=20, early_stop="weight_diff", batch_size=-1,
  59. learning_rate=0.1,
  60. init_param={"init_method": "random_uniform"})
  61. evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary", pos_label=1)
  62. # evaluation_0.get_party_instance(role='host', party_id=host).component_param(need_run=False)
  63. pipeline.add_component(reader_0)
  64. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  65. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  66. pipeline.add_component(scale_0, data=Data(intersection_0.output.data))
  67. pipeline.add_component(sample_weight_0, data=Data(data=scale_0.output.data))
  68. pipeline.add_component(federated_sampler_0, data=Data(sample_weight_0.output.data))
  69. pipeline.add_component(hetero_lr_0, data=Data(train_data=federated_sampler_0.output.data))
  70. pipeline.add_component(evaluation_0, data=Data(data=hetero_lr_0.output.data))
  71. pipeline.compile()
  72. pipeline.fit()
  73. # predict
  74. # deploy required components
  75. pipeline.deploy_component([data_transform_0, intersection_0, hetero_lr_0])
  76. predict_pipeline = PipeLine()
  77. # add data reader onto predict pipeline
  78. predict_pipeline.add_component(reader_0)
  79. # add selected components from train pipeline onto predict pipeline
  80. # specify data source
  81. predict_pipeline.add_component(
  82. pipeline, data=Data(
  83. predict_input={
  84. pipeline.data_transform_0.input.data: reader_0.output.data}))
  85. # run predict model
  86. predict_pipeline.predict()
  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()