nvflare.app_opt.sklearn.recipes.fedavg module¶
- class SklearnFedAvgRecipe(*, name: str = 'sklearn_fedavg', min_clients: int, num_rounds: int = 2, model_params: dict | None = None, train_script: str, train_args: str | Dict[str, str] = '', aggregator: Aggregator | None = None, aggregator_data_kind: DataKind = DataKind.WEIGHTS, launch_external_process: bool = False, command: str = 'python3 -u')[source]¶
Bases:
RecipeA recipe for implementing Federated Averaging (FedAvg) with Scikit-learn.
This recipe sets up a complete federated learning workflow with scatter-and-gather communication pattern specifically designed for scikit-learn models.
The recipe configures: - A federated job with initial parameters - Scatter-and-gather controller for coordinating training rounds - Weighted aggregator for combining client model updates (or custom aggregator) - Script runners for client-side training execution
- Parameters:
name – Name of the federated learning job. Defaults to “sklearn_fedavg”.
min_clients – Minimum number of clients required to start a training round.
num_rounds – Number of federated training rounds to execute. Defaults to 2.
model_params – Model hyperparameters as a dictionary. For SGDClassifier, can include: n_classes, learning_rate, eta0, loss, penalty, fit_intercept, etc. Can also include initial weights if needed.
train_script – Path to the training script that will be executed on each client.
train_args – Command line arguments to pass to the training script. Can be: - str: Same arguments for all clients (uses job.to_clients) - dict[str, str]: Per-client arguments mapping site names to args (uses job.to per site)
aggregator – Custom aggregator for combining client updates. If None, uses InTimeAccumulateWeightedAggregator with aggregator_data_kind.
aggregator_data_kind – Data kind to use for the aggregator. Defaults to DataKind.WEIGHTS.
launch_external_process – Whether to launch the script in external process. Defaults to False.
command – If launch_external_process=True, command to run script (prepended to script). Defaults to “python3 -u”.
Example
```python recipe = SklearnFedAvgRecipe(
name=”sklearn_linear”, min_clients=5, num_rounds=50, model_params={
“n_classes”: 2, “learning_rate”: “constant”, “eta0”: 1e-4, “loss”: “log_loss”, “penalty”: “l2”, “fit_intercept”: 1,
}, train_script=”client.py”, train_args=”–data_path /tmp/data/HIGGS.csv”,
)
from nvflare.recipe import SimEnv env = SimEnv(num_clients=5) run = recipe.execute(env) print(“Result:”, run.get_result()) ```
Note
By default, this recipe implements the standard FedAvg algorithm where model updates are aggregated using weighted averaging based on the number of training samples provided by each client.
If you want to use a custom aggregator, you can pass it in the aggregator parameter. The custom aggregator must be a subclass of the Aggregator class.
This is base class of a recipe. Recipes are implemented by jobs. A concrete recipe must provide the job for recipe implementation.
- Parameters:
job – the job that implements the recipe.