pipeline-hetero-nn-train-binary-selective-bp.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 torch as t
  18. from torch import nn
  19. from pipeline import fate_torch_hook
  20. from pipeline.backend.pipeline import PipeLine
  21. from pipeline.component import DataTransform
  22. from pipeline.component import Evaluation
  23. from pipeline.component import HeteroNN
  24. from pipeline.component import Intersection
  25. from pipeline.component import Reader
  26. from pipeline.interface import Data
  27. from pipeline.utils.tools import load_job_config
  28. fate_torch_hook(t)
  29. def main(config="../../config.yaml", namespace=""):
  30. # obtain config
  31. if isinstance(config, str):
  32. config = load_job_config(config)
  33. parties = config.parties
  34. guest = parties.guest[0]
  35. host = parties.host[0]
  36. guest_train_data = {"name": "breast_hetero_guest", "namespace": "experiment"}
  37. host_train_data = {"name": "breast_hetero_host", "namespace": "experiment"}
  38. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host)
  39. reader_0 = Reader(name="reader_0")
  40. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  41. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  42. data_transform_0 = DataTransform(name="data_transform_0")
  43. data_transform_0.get_party_instance(role='guest', party_id=guest).component_param(with_label=True)
  44. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  45. intersection_0 = Intersection(name="intersection_0")
  46. hetero_nn_0 = HeteroNN(name="hetero_nn_0", epochs=5,
  47. interactive_layer_lr=0.01, batch_size=128, validation_freqs=1, task_type='classification',
  48. selector_param={"method": "relative"})
  49. guest_nn_0 = hetero_nn_0.get_party_instance(role='guest', party_id=guest)
  50. host_nn_0 = hetero_nn_0.get_party_instance(role='host', party_id=host)
  51. # define model
  52. guest_bottom = t.nn.Sequential(
  53. nn.Linear(10, 4),
  54. nn.ReLU(),
  55. nn.Dropout(p=0.2)
  56. )
  57. guest_top = t.nn.Sequential(
  58. nn.Linear(4, 1),
  59. nn.Sigmoid()
  60. )
  61. host_bottom = t.nn.Sequential(
  62. nn.Linear(20, 4),
  63. nn.ReLU(),
  64. nn.Dropout(p=0.2)
  65. )
  66. # use interactive layer after fate_torch_hook
  67. # add drop out in this layer
  68. interactive_layer = t.nn.InteractiveLayer(out_dim=4, guest_dim=4, host_dim=4, host_num=1, dropout=0.2)
  69. guest_nn_0.add_top_model(guest_top)
  70. guest_nn_0.add_bottom_model(guest_bottom)
  71. host_nn_0.add_bottom_model(host_bottom)
  72. optimizer = t.optim.Adam(lr=0.01) # you can initialize optimizer without parameters after fate_torch_hook
  73. loss = t.nn.BCELoss()
  74. hetero_nn_0.set_interactive_layer(interactive_layer)
  75. hetero_nn_0.compile(optimizer=optimizer, loss=loss)
  76. evaluation_0 = Evaluation(name='eval_0', eval_type='binary')
  77. # define components IO
  78. pipeline.add_component(reader_0)
  79. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  80. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  81. pipeline.add_component(hetero_nn_0, data=Data(train_data=intersection_0.output.data))
  82. pipeline.add_component(evaluation_0, data=Data(data=hetero_nn_0.output.data))
  83. pipeline.compile()
  84. pipeline.fit()
  85. print(pipeline.get_component("hetero_nn_0").get_summary())
  86. if __name__ == "__main__":
  87. parser = argparse.ArgumentParser("PIPELINE DEMO")
  88. parser.add_argument("-config", type=str,
  89. help="config file")
  90. args = parser.parse_args()
  91. if args.config is not None:
  92. main(args.config)
  93. else:
  94. main()