adap / flower

Flower: A Friendly Federated AI Framework
https://flower.ai
Apache License 2.0
5k stars 858 forks source link

fraction_fit 怎么动态变化 #2781

Open buaaYYC opened 9 months ago

buaaYYC commented 9 months ago

What is your question?

strategy = fl.server.strategy.FedAvg(
    fraction_fit=0.2,
    fraction_evaluate=0.2,
    min_fit_clients=2,
    min_evaluate_clients=2,
    min_available_clients=10,
    evaluate_fn=get_evaluate_fn(model, args.toy),
    on_fit_config_fn=fit_config,
    on_evaluate_config_fn=evaluate_config,
    initial_parameters=fl.common.ndarrays_to_parameters(model_parameters),
)

Hello, I have a requirement now. I want the number of clients to participate in training to be different in different epochs. How should I modify the strategy? Thank you.

helin0815 commented 9 months ago

在flwr/server/server.py的代码里好像有地方可以修改,但是也需要改代码

buaaYYC commented 9 months ago

在flwr/server/server.py的代码里好像有地方可以修改,但是也需要改代码

是的,但是不知道怎么修改这个策略 ,现在我看都是一个固定值,没法和训练轮数挂钩。 我现在能想到的方法就是写两个模拟函数fl.simulation.start_simulation,比如前10epochs,客户端选择10个;后10个epochs训练再启动一个模拟函数,选择20个客户端,他的模型参数用第一次训练的参数。

helin0815 commented 9 months ago

在flwr/server/server.py的代码里好像有地方可以修改,但是也需要改代码

是的,但是不知道怎么修改这个策略 ,现在我看都是一个固定值,没法和训练轮数挂钩。 我现在能想到的方法就是写两个模拟函数fl.simulation.start_simulation,比如前10epochs,客户端选择10个;后10个epochs训练再启动一个模拟函数,选择20个客户端,他的模型参数用第一次训练的参数。

感觉你这个方法也可以,或者你可以试试把fraction_fit这个值从某个配置文件(比如/opt/fraction_fit.txt)中读取,然后在训练10轮以后修改/opt/fraction_fit.txt的内容,然后server.py再去读取这个内容?我不太确定server的启动是不是每轮都会重新分配一下,你可以试试

buaaYYC commented 9 months ago

在flwr/server/server.py的代码里好像有地方可以修改,但是也需要改代码

是的,但是不知道怎么修改这个策略 ,现在我看都是一个固定值,没法和训练轮数挂钩。 我现在能想到的方法就是写两个模拟函数fl.simulation.start_simulation,比如前10epochs,客户端选择10个;后10个epochs训练再启动一个模拟函数,选择20个客户端,他的模型参数用第一次训练的参数。

感觉你这个方法也可以,或者你可以试试把fraction_fit这个值从某个配置文件(比如/opt/fraction_fit.txt)中读取,然后在训练10轮以后修改/opt/fraction_fit.txt的内容,然后server.py再去读取这个内容?我不太确定server的启动是不是每轮都会重新分配一下,你可以试试

好的我试一试,我刚刚看到一个参数on_fit_config_fn ,感觉可能解决这个问题 image image

helin0815 commented 9 months ago

在flwr/server/server.py的代码里好像有地方可以修改,但是也需要改代码

是的,但是不知道怎么修改这个策略 ,现在我看都是一个固定值,没法和训练轮数挂钩。 我现在能想到的方法就是写两个模拟函数fl.simulation.start_simulation,比如前10epochs,客户端选择10个;后10个epochs训练再启动一个模拟函数,选择20个客户端,他的模型参数用第一次训练的参数。

感觉你这个方法也可以,或者你可以试试把fraction_fit这个值从某个配置文件(比如/opt/fraction_fit.txt)中读取,然后在训练10轮以后修改/opt/fraction_fit.txt的内容,然后server.py再去读取这个内容?我不太确定server的启动是不是每轮都会重新分配一下,你可以试试

好的我试一试,我刚刚看到一个参数on_fit_config_fn ,感觉可能解决这个问题 image image

你使用flower框架是在做什么呢,我现在的研究方向是联邦学习的差分隐私

buaaYYC commented 9 months ago

我研究联邦学习的客户端选择策略

yan-gao-GY commented 9 months ago

Hi, this can be done by overriding the configure_fit function of the strategy. E.g.,

def configure_fit(
    self, server_round: int, parameters: Parameters, client_manager: ClientManager
) -> List[Tuple[ClientProxy, FitIns]]:
    """Configure the next round of training."""
    config = {}
    if self.on_fit_config_fn is not None:
        # Custom fit config function provided
        config = self.on_fit_config_fn(server_round)
    fit_ins = FitIns(parameters, config)

    # Sample clients
    sample_size, min_num_clients = self.num_fit_clients(
        client_manager.num_available()
    )
    clients = client_manager.sample(
        num_clients=sample_size, min_num_clients=min_num_clients
    )

    # Sample the clients sequentially given server_round
    sampled_idx = (server_round - 1) % len(clients)
    sampled_clients = [clients[sampled_idx]]

    # Return client/config pairs
    return [(client, fit_ins) for client in sampled_clients]

The above code allows to sequentially sample the clients given server_round. You could do whatever clients sampling based on the given server_round.

buaaYYC commented 9 months ago

Hi, this can be done by overriding the configure_fit function of the strategy. E.g.,

def configure_fit(
    self, server_round: int, parameters: Parameters, client_manager: ClientManager
) -> List[Tuple[ClientProxy, FitIns]]:
    """Configure the next round of training."""
    config = {}
    if self.on_fit_config_fn is not None:
        # Custom fit config function provided
        config = self.on_fit_config_fn(server_round)
    fit_ins = FitIns(parameters, config)

    # Sample clients
    sample_size, min_num_clients = self.num_fit_clients(
        client_manager.num_available()
    )
    clients = client_manager.sample(
        num_clients=sample_size, min_num_clients=min_num_clients
    )

    # Sample the clients sequentially given server_round
    sampled_idx = (server_round - 1) % len(clients)
    sampled_clients = [clients[sampled_idx]]

    # Return client/config pairs
    return [(client, fit_ins) for client in sampled_clients]

The above code allows to sequentially sample the clients given server_round. You could do whatever clients sampling based on the given server_round.

"Thank you very much. Your answers have been very helpful to me."

Klein920116 commented 3 months ago

如果启动了flower server后,客户端一直没有连接,可以手动停止server吗?

helin0815 commented 3 months ago

如果启动了flower server后,客户端一直没有连接,可以手动停止server吗?

你具体是指什么?启动服务端后再启动客户端,客户端连不上服务端的话,当然是可以手动停止server的,停止了以后你再去排查一下你的代码哪里不对导致客户端连不上