5.1.1.1.1.8. FedEval.aggregator.utils
5.1.1.1.1.8.1. Module Contents
5.1.1.1.1.8.1.1. Functions
|
stack the given layers into a single array |
|
Aggregate the given client-side params layer-wise. |
- FedEval.aggregator.utils.stack_layers(layers: Iterable[numpy.ndarray]) numpy.ndarray
stack the given layers into a single array
- Parameters:
layers (Iterable[ndarray]) – the layers to stack, ordered like [layer1, layer2, …]
- Returns:
the stacked layers, with shape (len(layers), *layers[0].shape)
- Return type:
ndarray
Example
>>> _stack_layers([np.array([[1, 2], [3, 4]]), np.array([[5, 6], [7, 8]])]) array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
- FedEval.aggregator.utils.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.]])]