pipeline-label-transform-encoder.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 LabelTransform
  19. from pipeline.component import HeteroLR
  20. from pipeline.component import DataTransform
  21. from pipeline.component import Intersection
  22. from pipeline.component import Reader
  23. from pipeline.interface import Data, Model
  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_hetero_guest", "namespace": f"experiment{namespace}"}
  34. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  35. # initialize pipeline
  36. pipeline = PipeLine()
  37. # set job initiator
  38. pipeline.set_initiator(role="guest", party_id=guest).set_roles(guest=guest, host=host, arbiter=arbiter)
  39. # define Reader components to read in data
  40. reader_0 = Reader(name="reader_0")
  41. # configure Reader
  42. reader_0.get_party_instance(role="guest", party_id=guest).component_param(table=guest_train_data)
  43. reader_0.get_party_instance(role="host", party_id=host).component_param(table=host_train_data)
  44. data_transform_0 = DataTransform(name="data_transform_0") # start component numbering at 0
  45. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role="guest", party_id=guest)
  46. data_transform_0_guest_party_instance.component_param(with_label=True, output_format="dense")
  47. data_transform_0.get_party_instance(role="host", party_id=host).component_param(with_label=False,
  48. output_format="dense")
  49. intersection_0 = Intersection(name="intersection_0")
  50. label_transform_0 = LabelTransform(name="label_transform_0", label_encoder={"0": 1, "1": 0}, label_list=[0, 1])
  51. label_transform_0.get_party_instance(role="host", party_id=host).component_param(need_run=False)
  52. hetero_lr_0 = HeteroLR(name="hetero_lr_0", penalty="L2", optimizer="sgd", tol=0.001,
  53. alpha=0.01, max_iter=20, early_stop="weight_diff", batch_size=-1,
  54. learning_rate=0.15, decay=0.0, decay_sqrt=False,
  55. init_param={"init_method": "zeros"},
  56. floating_point_precision=23)
  57. label_transform_1 = LabelTransform(name="label_transform_1")
  58. # add components to pipeline, in order of task execution
  59. pipeline.add_component(reader_0)
  60. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  61. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  62. pipeline.add_component(label_transform_0, data=Data(data=intersection_0.output.data))
  63. pipeline.add_component(hetero_lr_0, data=Data(train_data=label_transform_0.output.data))
  64. pipeline.add_component(
  65. label_transform_1, data=Data(
  66. data=hetero_lr_0.output.data), model=Model(
  67. label_transform_0.output.model))
  68. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  69. pipeline.compile()
  70. # fit model
  71. pipeline.fit()
  72. if __name__ == "__main__":
  73. parser = argparse.ArgumentParser("PIPELINE DEMO")
  74. parser.add_argument("-config", type=str,
  75. help="config file")
  76. args = parser.parse_args()
  77. if args.config is not None:
  78. main(args.config)
  79. else:
  80. main()