在开始建模任务之前,应上载要使用的数据。请参阅指南。
Guest表示数据应用方,Host是数据提供方,在纵向算法中,Guest往往是有标签y的一方。
arbiter是用来辅助多方完成联合建模的,主要的作用是用来聚合梯度或者模型,
比如纵向lr里面,各方将自己一半的梯度发送给arbiter,然后arbiter再联合优化等等,
arbiter还参与以及分发公私钥,进行加解密服务等等。一般是由数据应用方Guest发起建模流程。
准备
pipeline包提供组成FATE管道的组件。
from pipeline.backend.pipeline import PipeLine
from pipeline.component import Reader, DataTransform, Intersection, HeteroSecureBoost, Evaluation
from pipeline.interface import Data
创建pipeline实例:
-发起人:
*角色:host
*参与方:9999
-角色:
*Guest:9999
*Host:10000
pipeline = PipeLine() \
.set_initiator(role='guest', party_id=9999) \
.set_roles(guest=9999, host=10000)
定义读取器以加载数据
reader_0 = Reader(name="reader_0")
# 设置guest参数
reader_0.get_party_instance(role='guest', party_id=9999).component_param(
table={"name": "breast_hetero_guest", "namespace": "experiment"})
# 设置host参数
reader_0.get_party_instance(role='host', party_id=10000).component_param(
table={"name": "breast_hetero_host", "namespace": "experiment"})
添加DataTransform组件以将原始数据解析到数据实例中
data_transform_0 = DataTransform(name="data_transform_0")
# 设置guest参数
data_transform_0.get_party_instance(role='guest', party_id=9999).component_param(
with_label=True)
data_transform_0.get_party_instance(role='host', party_id=[10000]).component_param(
with_label=False)
添加Intersection 组件以执行异场景的PSI
intersect_0 = Intersection(name="intersect_0")
现在,我们定义了HeteroSecureBoost组件。将为所有相关方设置以下参数。
hetero_secureboost_0 = HeteroSecureBoost(name="hetero_secureboost_0",
num_trees=5,
bin_num=16,
task_type="classification",
objective_param={"objective": "cross_entropy"},
encrypt_param={"method": "paillier"},
tree_param={"max_depth": 3})
要显示评估结果,需要“Evaluation”组件。
evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary")
训练
按执行顺序向管道添加组件:
-data_transform_0 消耗 reader_0的输出数据
-intersect_0 消耗 data_transform_0的输出数据
-heterosecurebost_0消耗intersect_0的输出数据
-evaluation_0消耗训练数据上的heterosecurebost_0的预测结果
然后编译我们的pipeline ,以便提交。
pipeline.add_component(reader_0)
pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
pipeline.add_component(intersect_0, data=Data(data=data_transform_0.output.data))
pipeline.add_component(hetero_secureboost_0, data=Data(train_data=intersect_0.output.data))
pipeline.add_component(evaluation_0, data=Data(data=hetero_secureboost_0.output.data))
pipeline.compile();
现在,提交(安装)我们的pipeline:
pipeline.fit()
![](https://img-blog.csdnimg.cn/img_convert/32e43f8a64330df19c7c8eef54a7714b.png)
一旦训练完成,训练后的模型可用于预测。(可选)保存经过训练的pipeline以供将来使用。
pipeline.dump("pipeline_saved.pkl");
预测
首先,从训练的pipeline部署所需组件
pipeline = PipeLine.load_model_from_file('pipeline_saved.pkl')
pipeline.deploy_component([pipeline.data_transform_0, pipeline.intersect_0, pipeline.hetero_secureboost_0]);
定义用于读取预测数据的新Reader组件
reader_1 = Reader(name="reader_1")
reader_1.get_party_instance(role="guest", party_id=9999).component_param(table={"name": "breast_hetero_guest", "namespace": "experiment"})
reader_1.get_party_instance(role="host", party_id=10000).component_param(table={"name": "breast_hetero_host", "namespace": "experiment"})
(可选)定义新的评估组件。
evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary")
按执行顺序添加组件以预测pipeline:
predict_pipeline = PipeLine()
predict_pipeline.add_component(reader_1)\
.add_component(pipeline,
data=Data(predict_input={pipeline.data_transform_0.input.data: reader_1.output.data}))\
.add_component(evaluation_0, data=Data(data=pipeline.hetero_secureboost_0.output.data));
然后,运行预测作业
predict_pipeline.predict()
![](https://img-blog.csdnimg.cn/img_convert/f1f04d10e41b9e863da22fe76a26eb8f.png)
有关使用pipeline提交作业的更多演示,请参阅pipeline演示