首先根据前面的几个例子总结一下直接使用FLGo运行其中算法的流程
- 定义联邦任务:指定任务路径、使用的数据集、数据集的划分方式
- 创建联邦i任务:使用flgo.gen_task函数根据config创建不同的联邦任务
- 选择算法,进行联邦训练:使用flgo.init函数和run函数进行训练
- 提取训练结果进行分析展示
之前创建不同的联邦任务时学习的主要是熟悉第一、二部分,本篇将简单介绍第三部分选择算法,进行联邦训练;在这一节中只介绍了在调用flgo.init和run函数iaangz时的一些参数设置,至于这两个函数具体的实现流程,在之后的教程中也会进行介绍。
flgo.init函数介绍
def init(task: str, algorithm, option = {
}, model=None, Logger: flgo.experiment.logger.BasicLogger = flgo.experiment.logger.simple_logger.SimpleLogger, Simulator: BasicSimulator=flgo.system_simulator.DefaultSimulator, scene='horizontal'):
r"""
Initialize a runner in FLGo, which is to optimize a model on a specific task (i.e. IID-mnist-of-100-clients) by the selected federated algorithm.
:param
task (str): the dictionary of the federated task
algorithm (module || class): the algorithm will be used to optimize the model in federated manner, which must contain pre-defined attributions (e.g. algorithm.Server and algorithm.Client for horizontal federated learning)
option (dict || str): the configurations of training, environment, algorithm, logger and simulator
model (module || class): the model module that contains two methods: model.init_local_module(object) and model.init_global_module(object)
Logger (class): the class of the logger inherited from flgo.experiment.logger.BasicLogger
Simulator (class): the class of the simulator inherited from flgo.system_simulator.BasicSimulator
scene (str): 'horizontal' or 'vertical' in current version of FLGo
:return
runner: the object instance that has the method runner.run()
"""
...
调用示例
runner = flgo.init(task, fedavg, {
'log_file':True, 'num_epochs':1})
runner.run()
可以看到flgo.init返回一个具有run方法的对象,通过调用run方法来开启迭代训练,具体的init函数的输入包括:
- task: 联邦任务(路径、处理数据集等)由前面的gen_task生成
- algorithm: ,要求algorithm的类型是class或module,横向联邦中需要其具备algorithm.Server和algorithm.Client两个可访问的属性;
- option(可选):运行选项,类型为字典,包含运行时的各类参数;
- model(可选):待优化的模型模块,要求model的类型为类或module,横向联邦中需要其具备model.init_global_module和model.init_local_module两个可访问的方法,为实体初始化module;
- Logger(可选):日志记录器类,要求父类为flgo.experiment.logger.BasicLogger
- Simulator(可选):系统模拟器类,要求父类为flgo.system_simulator.BasicSimulator
- scene(可选):类型为字符串,联邦场景(e.g. 横向 or 纵向)
每个参数应该如何设置,来达到自己的实验目的在之后的教程中也将会依次介绍。
本节要介绍一下其中option参数的设置
option参数的设置
在flgo中,每个runner的运行时参数由传入的字典option来指定,这些参数主要分为4类(训练参数、用户选项、日志选项、模拟器选项),为了能够在写代码时能够查看option包含哪些关键字,可以使用flgo.option_helper( )
函数来查看。该函数输出结果如下
+---------------------+-----------+------------------------------------------------------------------------------------------------+---------------+