pipeline-homo-onehot-string-test.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 HomoLR
  20. from pipeline.component import HomoOneHotEncoder
  21. from pipeline.component import Reader
  22. from pipeline.component import Evaluation
  23. from pipeline.component import FeatureScale
  24. from pipeline.interface import Data
  25. from pipeline.interface import Model
  26. from pipeline.utils.tools import load_job_config
  27. import json
  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": "mock_string", "namespace": f"experiment{namespace}"}
  37. host_train_data = {"name": "mock_string", "namespace": f"experiment{namespace}"}
  38. guest_eval_data = {"name": "mock_string", "namespace": f"experiment{namespace}"}
  39. host_eval_data = {"name": "mock_string", "namespace": f"experiment{namespace}"}
  40. # initialize pipeline
  41. pipeline = PipeLine()
  42. # set job initiator
  43. pipeline.set_initiator(role='guest', party_id=guest)
  44. # set participants information
  45. pipeline.set_roles(guest=guest, host=host, arbiter=arbiter)
  46. # define Reader components to read in data
  47. reader_0 = Reader(name="reader_0")
  48. # configure Reader for guest
  49. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  50. # configure Reader for host
  51. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  52. reader_1 = Reader(name="reader_1")
  53. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_eval_data)
  54. reader_1.get_party_instance(role='host', party_id=host).component_param(table=host_eval_data)
  55. # define DataTransform components
  56. data_transform_0 = DataTransform(name="data_transform_0", with_label=True, output_format="dense", label_name='y',
  57. data_type="str") # start component numbering at 0
  58. data_transform_1 = DataTransform(name="data_transform_1")
  59. homo_onehot_param = {
  60. "transform_col_indexes": -1,
  61. "transform_col_names": [],
  62. "need_alignment": True
  63. }
  64. homo_onehot_0 = HomoOneHotEncoder(name='homo_onehot_0', **homo_onehot_param)
  65. homo_onehot_1 = HomoOneHotEncoder(name='homo_onehot_1')
  66. scale_0 = FeatureScale(name='scale_0', method="standard_scale")
  67. scale_1 = FeatureScale(name='scale_1')
  68. homo_lr_param = {
  69. "penalty": "L2",
  70. "optimizer": "sgd",
  71. "tol": 1e-05,
  72. "alpha": 0.01,
  73. "max_iter": 3,
  74. "early_stop": "diff",
  75. "batch_size": 500,
  76. "learning_rate": 0.15,
  77. "decay": 1,
  78. "decay_sqrt": True,
  79. "init_param": {
  80. "init_method": "zeros"
  81. },
  82. "cv_param": {
  83. "n_splits": 4,
  84. "shuffle": True,
  85. "random_seed": 33,
  86. "need_cv": False
  87. }
  88. }
  89. homo_lr_0 = HomoLR(name='homo_lr_0', **homo_lr_param)
  90. homo_lr_1 = HomoLR(name='homo_lr_1')
  91. # add components to pipeline, in order of task execution
  92. pipeline.add_component(reader_0)
  93. pipeline.add_component(reader_1)
  94. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  95. # set data_transform_1 to replicate model from data_transform_0
  96. pipeline.add_component(
  97. data_transform_1, data=Data(
  98. data=reader_1.output.data), model=Model(
  99. data_transform_0.output.model))
  100. pipeline.add_component(homo_onehot_0, data=Data(data=data_transform_0.output.data))
  101. pipeline.add_component(homo_onehot_1, data=Data(data=data_transform_1.output.data),
  102. model=Model(homo_onehot_0.output.model))
  103. pipeline.add_component(scale_0, data=Data(data=homo_onehot_0.output.data))
  104. pipeline.add_component(scale_1, data=Data(data=homo_onehot_1.output.data),
  105. model=Model(scale_0.output.model))
  106. pipeline.add_component(homo_lr_0, data=Data(train_data=scale_0.output.data))
  107. pipeline.add_component(homo_lr_1, data=Data(test_data=scale_1.output.data),
  108. model=Model(homo_lr_0.output.model))
  109. evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary")
  110. evaluation_0.get_party_instance(role='host', party_id=host).component_param(need_run=False)
  111. pipeline.add_component(evaluation_0, data=Data(data=[homo_lr_0.output.data,
  112. homo_lr_1.output.data]))
  113. pipeline.compile()
  114. # fit model
  115. pipeline.fit()
  116. # query component summary
  117. print(json.dumps(pipeline.get_component("homo_lr_0").get_summary(), indent=4, ensure_ascii=False))
  118. print(json.dumps(pipeline.get_component("evaluation_0").get_summary(), indent=4, ensure_ascii=False))
  119. if __name__ == "__main__":
  120. parser = argparse.ArgumentParser("PIPELINE DEMO")
  121. parser.add_argument("-config", type=str,
  122. help="config file")
  123. args = parser.parse_args()
  124. if args.config is not None:
  125. main(args.config)
  126. else:
  127. main()