pipeline-hetero-ftl-communication-efficient.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.hetero_ftl import HeteroFTL
  20. from pipeline.component.reader import Reader
  21. from pipeline.interface.data import Data
  22. from tensorflow.keras import optimizers
  23. from tensorflow.keras.layers import Dense
  24. from tensorflow.keras import initializers
  25. from pipeline.component.evaluation import Evaluation
  26. from pipeline.utils.tools import load_job_config
  27. def main(config="../../config.yaml", namespace=""):
  28. # obtain config
  29. if isinstance(config, str):
  30. config = load_job_config(config)
  31. parties = config.parties
  32. guest = parties.guest[0]
  33. host = parties.host[0]
  34. guest_train_data = {"name": "nus_wide_guest", "namespace": f"experiment{namespace}"}
  35. host_train_data = {"name": "nus_wide_host", "namespace": f"experiment{namespace}"}
  36. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host)
  37. reader_0 = Reader(name="reader_0")
  38. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  39. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  40. data_transform_0 = DataTransform(name="data_transform_0")
  41. data_transform_0.get_party_instance(
  42. role='guest', party_id=guest).component_param(
  43. with_label=True, output_format="dense")
  44. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  45. hetero_ftl_0 = HeteroFTL(name='hetero_ftl_0',
  46. epochs=10,
  47. alpha=1,
  48. batch_size=-1,
  49. mode='plain',
  50. communication_efficient=True,
  51. local_round=5)
  52. hetero_ftl_0.add_nn_layer(Dense(units=32, activation='sigmoid',
  53. kernel_initializer=initializers.RandomNormal(stddev=1.0),
  54. bias_initializer=initializers.Zeros()))
  55. hetero_ftl_0.compile(optimizer=optimizers.Adam(lr=0.01))
  56. evaluation_0 = Evaluation(name='evaluation_0', eval_type="binary")
  57. pipeline.add_component(reader_0)
  58. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  59. pipeline.add_component(hetero_ftl_0, data=Data(train_data=data_transform_0.output.data))
  60. pipeline.add_component(evaluation_0, data=Data(data=hetero_ftl_0.output.data))
  61. pipeline.compile()
  62. pipeline.fit()
  63. if __name__ == "__main__":
  64. parser = argparse.ArgumentParser("PIPELINE DEMO")
  65. parser.add_argument("-config", type=str,
  66. help="config file")
  67. args = parser.parse_args()
  68. if args.config is not None:
  69. main(args.config)
  70. else:
  71. main()