pipeline-kmeans-validate.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 HeteroKmeans
  20. from pipeline.component import Intersection
  21. from pipeline.component import Evaluation
  22. from pipeline.component import Reader
  23. from pipeline.interface import Data
  24. from pipeline.interface import Model
  25. from pipeline.utils.tools import load_job_config
  26. def main(config="../../config.yaml", namespace=""):
  27. # obtain config
  28. if isinstance(config, str):
  29. config = load_job_config(config)
  30. parties = config.parties
  31. guest = parties.guest[0]
  32. host = parties.host[0]
  33. arbiter = parties.arbiter[0]
  34. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  35. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  36. guest_eval_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  37. host_eval_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. reader_1 = Reader(name="reader_1")
  51. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_eval_data)
  52. reader_1.get_party_instance(role='host', party_id=host).component_param(table=host_eval_data)
  53. # define DataTransform components
  54. data_transform_0 = DataTransform(name="data_transform_0") # start component numbering at 0
  55. data_transform_1 = DataTransform(name="data_transform_1")
  56. # get DataTransform party instance of guest
  57. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  58. # configure DataTransform for guest
  59. data_transform_0_guest_party_instance.component_param(with_label=True, output_format="dense")
  60. # get and configure DataTransform party instance of host
  61. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  62. # define Intersection components
  63. intersection_0 = Intersection(name="intersection_0")
  64. intersection_1 = Intersection(name="intersection_1")
  65. param = {
  66. "k": 3,
  67. "max_iter": 10
  68. }
  69. hetero_kmeans_0 = HeteroKmeans(name='hetero_kmeans_0', **param)
  70. hetero_kmeans_1 = HeteroKmeans(name='hetero_kmeans_1')
  71. evaluation_0 = Evaluation(name='evaluation_0', eval_type='clustering')
  72. evaluation_1 = Evaluation(name='evaluation_1', eval_type='clustering')
  73. # add components to pipeline, in order of task execution
  74. pipeline.add_component(reader_0)
  75. pipeline.add_component(reader_1)
  76. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  77. pipeline.add_component(
  78. data_transform_1, data=Data(
  79. data=reader_1.output.data), model=Model(
  80. data_transform_0.output.model))
  81. # set data input sources of intersection components
  82. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  83. pipeline.add_component(intersection_1, data=Data(data=data_transform_1.output.data))
  84. # set train & validate data of hetero_lr_0 component
  85. pipeline.add_component(hetero_kmeans_0, data=Data(train_data=intersection_0.output.data))
  86. pipeline.add_component(hetero_kmeans_1, data=Data(train_data=intersection_1.output.data))
  87. # print(f"data: {hetero_kmeans_0.output.data.data[0]}")
  88. pipeline.add_component(evaluation_0, data=Data(data=hetero_kmeans_0.output.data.data[0]))
  89. pipeline.add_component(evaluation_1, data=Data(data=hetero_kmeans_1.output.data.data[0]))
  90. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  91. pipeline.compile()
  92. # fit model
  93. pipeline.fit()
  94. # query component summary
  95. print(pipeline.get_component("hetero_kmeans_0").get_summary())
  96. if __name__ == "__main__":
  97. parser = argparse.ArgumentParser("PIPELINE DEMO")
  98. parser.add_argument("-config", type=str,
  99. help="config file")
  100. args = parser.parse_args()
  101. if args.config is not None:
  102. main(args.config)
  103. else:
  104. main()