5.1.1.1. FedEval.aggregator

5.1.1.1.1. Submodules

5.1.1.1.2. Package Contents

5.1.1.1.2.1. Classes

ParamAggregator

A class to aggregate the parameters from different clients.

5.1.1.1.2.2. Functions

_layerwise_aggregate(...)

Aggregate the given client-side params layer-wise.

weighted_average(...)

return the weighted average of the given client-side params according to the given weights.

trimmed_mean(→ FedEval.aggregator.ModelWeight.ModelWeights)

Return the coordinate-wise mean of the given client-side params after trimming a certain ratio

coordinate_wise_median(...)

return the coordinate-wise median of the given client-side params.

trimmed_coordinate_wise_median(...)

Return the coordinate-wise median of the given client-side params after trimming a certain ratio

krum

Krum Implementations

norm_clip(→ FedEval.aggregator.ModelWeight.ModelWeights)

Aggregate the given client-side params by norm clipping.

bulyan

5.1.1.1.2.3. Attributes

ModelWeights

FedEval.aggregator.ModelWeights
FedEval.aggregator._layerwise_aggregate(params: Iterable[FedEval.aggregator.ModelWeight.ModelWeights], func: Callable[[numpy.ndarray], numpy.ndarray]) FedEval.aggregator.ModelWeight.ModelWeights

Aggregate the given client-side params layer-wise.

Parameters:
  • params (Iterable[ModelWeights]) – the weights form different clients, ordered like [params1, params2, …]

  • func (Callable[[ndarray], ndarray]) – the aggregation function to apply to stacked layers from each client. And they will be called with axis=0.

Returns:

the aggregated parameters which have the same format with any instance from the params

Return type:

ModelWeights

Example

>>> params = [[np.array([[1, 2], [3, 4]]), np.array([[5, 6], [7, 8]])], [np.array([[9, 10], [11, 12]]), np.array([[13, 14], [15, 16]])]]
>>> _layerwise_aggregate(params, np.mean)
[array([[5., 6.],
       [7., 8.]]),
array([[9., 10.],
       [11., 12.]])]
>>> _layerwise_aggregate(params, np.median)
[array([[1., 2.],
       [3., 4.]]),
array([[5., 6.],
       [7., 8.]])]
FedEval.aggregator.weighted_average(client_params: Iterable[FedEval.aggregator.ModelWeight.ModelWeights], weights: Iterable[float | int]) FedEval.aggregator.ModelWeight.ModelWeights

return the weighted average of the given client-side params according to the given weights.

Parameters:
  • client_params (Iterable[ModelWeights]) – the weights form different clients, ordered like [params1, params2, …]

  • weights (Iterable[Union[float, int]]) – aggregate weights of different clients, usually set according to the clients’ training sample size. E.g., A, B, and C have 10, 20, and 30 images, then the aggregate_weights can be [1/6, 1/3, 1/2] or [10, 20, 30].

Raises:

ValueError – if the number of client params and weights are not the same

Returns:

the aggregated parameters which have the same format with any instance from the client_params

Return type:

ModelWeights

FedEval.aggregator.trimmed_mean(client_params: Iterable[FedEval.aggregator.ModelWeight.ModelWeights], ratio: float = 0.05) FedEval.aggregator.ModelWeight.ModelWeights

Return the coordinate-wise mean of the given client-side params after trimming a certain ratio of the extreme parameter values.

Parameters:
  • client_params (Iterable[ModelWeights]) – The weights from different clients, ordered like [params1, params2, …].

  • ratio (float, optional) – The ratio of extreme parameter values to trim. Should be between 0 and 0.5. Defaults to 0.05.

Raises:

ValueError – If trim_ratio is not in [0, 0.5).

Returns:

The aggregated parameters which have the same format with any instance from the client_params.

Return type:

ModelWeights

FedEval.aggregator.coordinate_wise_median(client_params: Iterable[FedEval.aggregator.ModelWeight.ModelWeights]) FedEval.aggregator.ModelWeight.ModelWeights

return the coordinate-wise median of the given client-side params.

Parameters:

client_params (Iterable[ModelWeights]) – the weights form different clients, ordered like [params1, params2, …]

Returns:

the aggregated parameters which have the same format with any instance from the client_params

Return type:

ModelWeights

FedEval.aggregator.trimmed_coordinate_wise_median(client_params: Iterable[FedEval.aggregator.ModelWeight.ModelWeights], ratio: float = 0.05) FedEval.aggregator.ModelWeight.ModelWeights

Return the coordinate-wise median of the given client-side params after trimming a certain ratio of the extreme parameter values.

Parameters:
  • client_params (Iterable[ModelWeights]) – The weights from different clients, ordered like [params1, params2, …].

  • ratio (float, optional) – The ratio of extreme parameter values to trim. Should be between 0 and 0.5. Defaults to 0.05.

Raises:

ValueError – If trim_ratio is in [0, 0.5).

Returns:

The aggregated parameters which have the same format with any instance from the client_params.

Return type:

ModelWeights

FedEval.aggregator.krum(params: Iterable[FedEval.aggregator.ModelWeight.ModelWeights], select: int | None = 1, dist_metric: str = 'euclidean') FedEval.aggregator.ModelWeight.ModelWeights
FedEval.aggregator.norm_clip(client_params: Iterable[FedEval.aggregator.ModelWeight.ModelWeights], server_param: FedEval.aggregator.ModelWeight.ModelWeights, threshold: float | None = 0.5) FedEval.aggregator.ModelWeight.ModelWeights

Aggregate the given client-side params by norm clipping.

Parameters:
  • client_params (Iterable[ModelWeights]) – the weights form different clients, ordered like [params1, params2, …]

  • server_param (ModelWeights) – the weights at the server-side

  • threshold (Optional[float], optional) – the threshold of the norm. Defaults to 0.5.

Raises:

ValueError – If threshold is invalid.

Returns:

the aggregated model weights.

Return type:

ModelWeights

FedEval.aggregator.bulyan(params: Iterable[FedEval.aggregator.ModelWeight.ModelWeights], ratio: float = 0.05, select: int | None = 1, dist_metric: str = 'euclidean') FedEval.aggregator.ModelWeight.ModelWeights

Bulyan aggregation method.

Parameters:
  • params (Iterable[ModelWeights]) – List of model weights.

  • ratio (float) – Ratio of params to be trimmed. Defaults to 0.05.

  • select (Optional[int], optional) – Number of clients to be selected. Defaults to 1.

  • dist_metric (str, optional) – Distance metric. Defaults to ‘euclidean’.

Raises:

ValueError – Invalid number of selected params, or invalid trim ratio.

Returns:

Aggregated model weights.

Return type:

ModelWeights

class FedEval.aggregator.ParamAggregator(params: Iterable[ModelWeight.ModelWeights])

A class to aggregate the parameters from different clients.

average() ModelWeight.ModelWeights

return the average of the given client-side params.

Returns:

the aggregated parameters which have the same format with any instance from the client_params

Return type:

ModelWeights

weighted_average(weights: Iterable[float | int]) ModelWeight.ModelWeights

return the weighted average of the given client-side params according to the given weights.

Parameters:

weights (Iterable[Union[float, int]]) – aggregate weights of different clients, usually set according to the clients’ training sample size. E.g., A, B, and C have 10, 20, and 30 images, then the aggregate_weights can be [1/6, 1/3, 1/2] or [10, 20, 30].

Returns:

the aggregated parameters which have the same format with any instance from the client_params

Return type:

ModelWeights

median() ModelWeight.ModelWeights

return the coordinate-wise median of the given client-side params.

Returns:

the aggregated parameters which have the same format with any instance from the client_params

Return type:

ModelWeights

trimmed_median(ratio: float = 0.05) ModelWeight.ModelWeights

Return the coordinate-wise median of the given client-side params after trimming a certain ratio of the extreme parameter values.

Parameters:

ratio (float, optional) – The ratio of extreme parameter values to trim. Should be between 0 and 1. Defaults to 0.05.

Raises:

ValueError – If trim_ratio is not in [0, 0.5).

Returns:

The aggregated parameters which have the same format with any instance from the client_params.

Return type:

ModelWeights

trimmed_mean(ratio: float = 0.05) ModelWeight.ModelWeights

Return the coordinate-wise mean of the given client-side params after trimming a certain ratio of the extreme parameter values.

Parameters:

ratio (float, optional) – The ratio of extreme parameter values to trim. Should be between 0 and 1. Defaults to 0.05.

Raises:

ValueError – If trim_ratio is not in [0, 0.5).

Returns:

The aggregated parameters which have the same format with any instance from the client_params.

Return type:

ModelWeights

krum(select: int | None = 1) ModelWeight.ModelWeights

Return the krum aggregate of the given client-side params.

Parameters:

select (int, optional) – The number of clients to select to support multi-krum. If set to None, it will return an averaged one of all the params. Defaults to 1.

Raises:

ValueError – If select is invalid.

Returns:

The aggregated parameters which have the same format with any instance from the client_params.

Return type:

ModelWeights

bulyan(ratio: float = 0.05, select: int | None = 1, dist_metric: str = 'euclidean') ModelWeight.ModelWeights

Return the bulyan aggregate of the given client-side params.

Parameters:
  • ratio (float, optional) – The ratio of extreme parameter values to trim. Should be in [0, 0.5). Defaults to 0.05.

  • select (int, optional) – The number of clients to select to support multi-krum. Defaults to 1.

  • dist_metric (str, optional) – The distance metric to use. Defaults to ‘euclidean’.

Raises:

ValueError – Invalid number of selected params or trim ratio.

Returns:

The aggregated parameters which have the same format with any instance from the client_params.

Return type:

ModelWeights

norm_clip(server_param: ModelWeight.ModelWeights, threshold: float | None = 0.5) ModelWeight.ModelWeights

Aggregate the given client-side params by norm clipping.

Parameters:
  • server_param (ModelWeights) – the weights at the server-side

  • threshold (Optional[float], optional) – the threshold of the norm. Defaults to 0.5.

Raises:

ValueError – If threshold is invalid.

Returns:

the aggregated model weights.

Return type:

ModelWeights