pipeline-homo-recursive-binning-predict.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 HomoFeatureBinning
  21. from pipeline.component import Reader
  22. from pipeline.interface import Data
  23. from pipeline.interface import 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_homo_guest", "namespace": f"experiment{namespace}"}
  34. host_train_data = {"name": "breast_homo_host", "namespace": f"experiment{namespace}"}
  35. # initialize pipeline
  36. pipeline = PipeLine()
  37. # set job initiator
  38. pipeline.set_initiator(role='guest', party_id=guest)
  39. # set participants information
  40. pipeline.set_roles(guest=guest, host=host, arbiter=arbiter)
  41. # define Reader components to read in data
  42. reader_0 = Reader(name="reader_0")
  43. # configure Reader for guest
  44. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  45. # configure Reader for host
  46. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  47. # define DataTransform components
  48. data_transform_0 = DataTransform(
  49. name="data_transform_0",
  50. with_label=True,
  51. output_format="dense") # start component numbering at 0
  52. homo_binning_0 = HomoFeatureBinning(name='homo_binning_0', sample_bins=1000, method="recursive_query")
  53. homo_binning_1 = HomoFeatureBinning(name='homo_binning_1', sample_bins=1000)
  54. # add components to pipeline, in order of task execution
  55. pipeline.add_component(reader_0)
  56. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  57. # set data input sources of intersection components
  58. pipeline.add_component(homo_binning_0, data=Data(data=data_transform_0.output.data))
  59. pipeline.add_component(homo_binning_1, data=Data(data=data_transform_0.output.data),
  60. model=Model(model=homo_binning_0.output.model))
  61. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  62. pipeline.compile()
  63. # fit model
  64. pipeline.fit()
  65. # query component summary
  66. # print(json.dumps(pipeline.get_component("homo_binning_0").get_summary(), indent=4, ensure_ascii=False))
  67. if __name__ == "__main__":
  68. parser = argparse.ArgumentParser("PIPELINE DEMO")
  69. parser.add_argument("-config", type=str,
  70. help="config file")
  71. args = parser.parse_args()
  72. if args.config is not None:
  73. main(args.config)
  74. else:
  75. main()