fl4health.strategies.fedavg_dynamic_layer module

class FedAvgDynamicLayer(*, fraction_fit=1.0, fraction_evaluate=1.0, min_fit_clients=2, min_evaluate_clients=2, min_available_clients=2, evaluate_fn=None, on_fit_config_fn=None, on_evaluate_config_fn=None, accept_failures=True, initial_parameters=None, fit_metrics_aggregation_fn=None, evaluate_metrics_aggregation_fn=None, weighted_aggregation=True, weighted_eval_losses=True)[source]

Bases: BasicFedAvg

__init__(*, fraction_fit=1.0, fraction_evaluate=1.0, min_fit_clients=2, min_evaluate_clients=2, min_available_clients=2, evaluate_fn=None, on_fit_config_fn=None, on_evaluate_config_fn=None, accept_failures=True, initial_parameters=None, fit_metrics_aggregation_fn=None, evaluate_metrics_aggregation_fn=None, weighted_aggregation=True, weighted_eval_losses=True)[source]

A generalization of the FedAvg strategy where the server can receive any arbitrary subset of the layers from any arbitrary subset of the clients, and weighted average for each received layer is performed independently.

Parameters:
  • fraction_fit (float, optional) – Fraction of clients used during training. Defaults to 1.0. Defaults to 1.0.

  • fraction_evaluate (float, optional) – Fraction of clients used during validation. Defaults to 1.0.

  • min_fit_clients (int, optional) – Minimum number of clients used during fitting. Defaults to 2.

  • min_evaluate_clients (int, optional) – Minimum number of clients used during validation. Defaults to 2.

  • min_available_clients (int, optional) – Minimum number of clients used during validation. Defaults to 2.

  • evaluate_fn (Callable[[int, NDArrays, dict[str, Scalar]], tuple[float, dict[str, Scalar]] | None] | None) – Optional function used for central server-side evaluation. Defaults to None.

  • on_fit_config_fn (Callable[[int], dict[str, Scalar]] | None, optional) – Function used to configure training by providing a configuration dictionary. Defaults to None.

  • on_evaluate_config_fn (Callable[[int], dict[str, Scalar]] | None, optional) – Function used to configure client-side validation by providing a Config dictionary. Defaults to None.

  • accept_failures (bool, optional) – Whether or not accept rounds containing failures. Defaults to True.

  • initial_parameters (Parameters | None, optional) – Initial global model parameters. Defaults to None.

  • fit_metrics_aggregation_fn (MetricsAggregationFn | None, optional) – Metrics aggregation function. Defaults to None.

  • evaluate_metrics_aggregation_fn (MetricsAggregationFn | None, optional) – Metrics aggregation function. Defaults to None.

  • weighted_aggregation (bool, optional) – Determines whether parameter aggregation is a linearly weighted average or a uniform average. FedAvg default is weighted average by client dataset counts. Defaults to True.

  • weighted_eval_losses (bool, optional) – Determines whether losses during evaluation are linearly weighted averages or a uniform average. FedAvg default is weighted average of the losses by client dataset counts. Defaults to True.

aggregate(results)[source]

Aggregate the different layers across clients that have contributed to a layer. This aggregation may be weighted or unweighted. The called functions handle layer alignment.

Parameters:

results (list[tuple[NDArrays, int]]) – The weight results from each client’s local training that need to be aggregated on the server-side and the number of training samples held on each client. In this scheme, the clients pack the layer weights into the results object along with the weight values to allow for alignment during aggregation.

Returns:

A dictionary mapping the name of the layer that was aggregated to the aggregated weights.

Return type:

dict[str, NDArray]

aggregate_fit(server_round, results, failures)[source]

Aggregate the results from the federated fit round. The aggregation requires some special treatment, as the participating clients are allowed to exchange an arbitrary set of weights. So before aggregation takes place alignment must be done using the layer names packed in along with the weights in the client results.

Parameters:
  • server_round (int) – Indicates the server round we’re currently on.

  • results (list[tuple[ClientProxy, FitRes]]) – The client identifiers and the results of their local training that need to be aggregated on the server-side. In this scheme, the clients pack the layer weights into the results object along with the weight values to allow for alignment during aggregation.

  • failures (list[tuple[ClientProxy, FitRes] | BaseException]) – These are the results and exceptions from clients that experienced an issue during training, such as timeouts or exceptions.

Returns:

The aggregated model weights and the metrics dictionary. For dynamic layer exchange we also pack in the names of all of the layers that were aggregated in this phase to allow client’s to insert the values into the proper areas of their models.

Return type:

tuple[Parameters | None, dict[str, Scalar]]

unweighted_aggregate(results)[source]

Results consists of the layer weights (and their names) sent by clients who participated in this round of training. Since each client can send an arbitrary subset of layers, the aggregate performs uniform averaging for each layer separately.

Parameters:

results (list[tuple[NDArrays, int]]) – The weight results from each client’s local training that need to be aggregated on the server-side and the number of training samples held on each client. In this scheme, the clients pack the layer weights into the results object along with the weight values to allow for alignment during aggregation.

Returns:

A dictionary mapping the name of the layer that was aggregated to the aggregated weights.

Return type:

dict[str, NDArray]

weighted_aggregate(results)[source]

Results consists of the layer weights (and their names) sent by clients who participated in this round of training. Since each client can send an arbitrary subset of layers, the aggregate performs weighted averaging for each layer separately.

Parameters:

results (list[tuple[NDArrays, int]]) – The weight results from each client’s local training that need to be aggregated on the server-side and the number of training samples held on each client. In this scheme, the clients pack the layer weights into the results object along with the weight values to allow for alignment during aggregation.

Returns:

A dictionary mapping the name of the layer that was aggregated to the aggregated weights.

Return type:

dict[str, NDArray]