pipeline-hetero-linr-compute-loss-not-reveal.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # you may not use this file except in compliance with the License.
  2. # You may obtain a copy of the License at
  3. #
  4. # http://www.apache.org/licenses/LICENSE-2.0
  5. #
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. #
  12. import argparse
  13. from pipeline.backend.pipeline import PipeLine
  14. from pipeline.component import DataTransform
  15. from pipeline.component import Evaluation
  16. from pipeline.component import HeteroSSHELinR
  17. from pipeline.component import Intersection
  18. from pipeline.component import Reader
  19. from pipeline.interface import Data
  20. from pipeline.utils.tools import load_job_config
  21. def main(config="../../config.yaml", namespace=""):
  22. # obtain config
  23. if isinstance(config, str):
  24. config = load_job_config(config)
  25. parties = config.parties
  26. guest = parties.guest[0]
  27. host = parties.host[0]
  28. guest_train_data = {"name": "motor_hetero_guest", "namespace": f"experiment{namespace}"}
  29. host_train_data = {"name": "motor_hetero_host", "namespace": f"experiment{namespace}"}
  30. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host)
  31. reader_0 = Reader(name="reader_0")
  32. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  33. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  34. data_transform_0 = DataTransform(name="data_transform_0")
  35. data_transform_0.get_party_instance(role='guest', party_id=guest).component_param(with_label=True,
  36. label_name="motor_speed",
  37. label_type="float",
  38. output_format="dense")
  39. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  40. intersection_0 = Intersection(name="intersection_0")
  41. hetero_linr_0 = HeteroSSHELinR(name="hetero_linr_0", penalty=None, optimizer="sgd", tol=0.001,
  42. alpha=0.01, max_iter=20, early_stop="weight_diff", batch_size=-1,
  43. learning_rate=0.15, decay=0.0, decay_sqrt=False,
  44. init_param={"init_method": "zeros", "fit_intercept": True},
  45. reveal_every_iter=False, reveal_strategy="respectively"
  46. )
  47. evaluation_0 = Evaluation(name="evaluation_0", eval_type="regression", pos_label=1)
  48. pipeline.add_component(reader_0)
  49. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  50. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  51. pipeline.add_component(hetero_linr_0, data=Data(train_data=intersection_0.output.data))
  52. pipeline.add_component(evaluation_0, data=Data(data=hetero_linr_0.output.data))
  53. pipeline.compile()
  54. pipeline.fit()
  55. # predict
  56. # deploy required components
  57. pipeline.deploy_component([data_transform_0, intersection_0, hetero_linr_0])
  58. predict_pipeline = PipeLine()
  59. # add data reader onto predict pipeline
  60. predict_pipeline.add_component(reader_0)
  61. # add selected components from train pipeline onto predict pipeline
  62. # specify data source
  63. predict_pipeline.add_component(pipeline,
  64. data=Data(
  65. predict_input={pipeline.data_transform_0.input.data: reader_0.output.data}))
  66. # run predict model
  67. predict_pipeline.predict()
  68. if __name__ == "__main__":
  69. parser = argparse.ArgumentParser("PIPELINE DEMO")
  70. parser.add_argument("-config", type=str,
  71. help="config file")
  72. args = parser.parse_args()
  73. if args.config is not None:
  74. main(args.config)
  75. else:
  76. main()