nvflare.app_common.np.recipes package

Subpackages

Submodules

Module contents

class NumpyFedAvgRecipe(*, name: str = 'fedavg', initial_model: Any | None = None, min_clients: int, num_rounds: int = 2, train_script: str, train_args: str = '', aggregator: Aggregator | None = None, aggregator_data_kind: DataKind | None = DataKind.WEIGHTS, launch_external_process: bool = False, command: str = 'python3 -u', server_expected_format: ExchangeFormat = ExchangeFormat.NUMPY, params_transfer_type: TransferType = TransferType.FULL)[source]

Bases: Recipe

A recipe for implementing Federated Averaging (FedAvg) with NumPy in NVFlare.

FedAvg is a fundamental federated learning algorithm that aggregates model updates from multiple clients by computing a weighted average based on the amount of local training data. This recipe sets up a complete federated learning workflow with scatter-and-gather communication pattern, specifically designed for NumPy-based models.

The recipe configures: - A federated job with initial model (optional) - 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 “fedavg”.

  • initial_model – Initial model to start federated training with. If None, clients will start with their own local models.

  • min_clients – Minimum number of clients required to start a training round.

  • num_rounds – Number of federated training rounds to execute. Defaults to 2.

  • 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.

  • aggregator – 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 (bool) – Whether to launch the script in external process. Defaults to False.

  • command (str) – If launch_external_process=True, command to run script (prepended to script). Defaults to “python3”.

  • server_expected_format (str) – What format to exchange the parameters between server and client.

  • params_transfer_type (str) – How to transfer the parameters. FULL means the whole model parameters are sent.

  • TransferType.FULL. (DIFF means that only the difference is sent. Defaults to)

Example

```python recipe = NumpyFedAvgRecipe(

name=”my_fedavg_job”, initial_model=numpy_model, min_clients=2, num_rounds=10, train_script=”client.py”, train_args=”–learning_rate 0.01”

)

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 or ModelAggregator class.

This is base class of a recipe. Recipes are implemented by jobs. A concrete recipe must provide the job for recipe implementation.

param job:

the job that implements the recipe.