def job_generate(): #generate a fl job which is the same as fl_master inputs = [fluid.layers.data( \ name=str(slot_id), shape=[5], dtype="float32") for slot_id in range(3)] label = fluid.layers.data( \ name="label", shape=[1], dtype='int64') model = Model() model.mlp(inputs, label) job_generator = JobGenerator() optimizer = fluid.optimizer.SGD(learning_rate=0.1) job_generator.set_optimizer(optimizer) job_generator.set_losses([model.loss]) job_generator.set_startup_program(model.startup_program) job_generator.set_infer_feed_and_target_names([x.name for x in inputs], [model.predict.name]) build_strategy = FLStrategyFactory() build_strategy.fed_avg = True build_strategy.inner_step = 10 strategy = build_strategy.create_fl_strategy() # endpoints will be collected through the cluster # in this example, we suppose endpoints have been collected server_ip = ["{}".format(ip_list[0])] output = "job_config" job_generator.generate_fl_job( strategy, server_endpoints=server_ip, worker_num=int(default_dict["worker_nodes"]), output=output) file_list = os.listdir(output) for file in file_list: tar = tarfile.open('{}/{}.tar.gz'.format(output, file), 'w:gz') for root, dir, files in os.walk("{}/{}".format(output, file)): for f in files: fullpath = os.path.join(root, f) tar.add(fullpath) tar.close()
label = fluid.layers.data( \ name="label", shape=[1], dtype='int64') model = Model() model.mlp(inputs, label) job_generator = JobGenerator() optimizer = fluid.optimizer.SGD(learning_rate=0.1) job_generator.set_optimizer(optimizer) job_generator.set_losses([model.loss]) job_generator.set_startup_program(model.startup_program) job_generator.set_infer_feed_and_target_names([x.name for x in inputs], [model.predict.name]) build_strategy = FLStrategyFactory() build_strategy.fed_avg = True build_strategy.inner_step = 10 strategy = build_strategy.create_fl_strategy() # endpoints will be collected through the cluster # in this example, we suppose endpoints have been collected endpoints = ["127.0.0.1:8181"] output = "fl_job_config" job_generator.generate_fl_job(strategy, server_endpoints=endpoints, worker_num=2, output=output) # fl_job_config will be dispatched to workers
STEP_EPSILON = 0.1 DELTA = 0.00001 SIGMA = math.sqrt(2.0 * math.log(1.25 / DELTA)) / STEP_EPSILON CLIP = 4.0 batch_size = 64 job_generator = JobGenerator() optimizer = fluid.optimizer.SGD(learning_rate=0.1) job_generator.set_optimizer(optimizer) job_generator.set_losses([model.avg_cost]) job_generator.set_startup_program(model.startup_program) job_generator.set_infer_feed_and_target_names( [model.inputs.name, model.label.name], [model.avg_cost.name]) build_strategy = FLStrategyFactory() build_strategy.dpsgd = True build_strategy.inner_step = 1 strategy = build_strategy.create_fl_strategy() strategy.learning_rate = 0.1 strategy.clip = CLIP strategy.batch_size = float(batch_size) strategy.sigma = CLIP * SIGMA # endpoints will be collected through the cluster # in this example, we suppose endpoints have been collected endpoints = ["127.0.0.1:8181"] output = "fl_job_config" job_generator.generate_fl_job(strategy, server_endpoints=endpoints, worker_num=2,
inputs = fluid.layers.data(name='x', shape=[1, 28, 28], dtype='float32') label = fluid.layers.data(name='y', shape=[1], dtype='int64') model = Model() model.linear_regression(inputs, label) job_generator = JobGenerator() optimizer = fluid.optimizer.SGD(learning_rate=0.01) job_generator.set_optimizer(optimizer) job_generator.set_losses([model.loss]) job_generator.set_startup_program(model.startup_program) job_generator.set_infer_feed_and_target_names([inputs.name, label.name], [model.loss.name]) build_strategy = FLStrategyFactory() #build_strategy.fed_avg = True build_strategy.sec_agg = True param_name_list = [] param_name_list.append( "fc_0.w_0.opti.trainer_") # need trainer_id when running param_name_list.append("fc_0.b_0.opti.trainer_") build_strategy.param_name_list = param_name_list build_strategy.inner_step = 10 strategy = build_strategy.create_fl_strategy() # endpoints will be collected through the cluster # in this example, we suppose endpoints have been collected endpoints = ["127.0.0.1:8181"]