Meamed#
- class byzfl.Meamed(f=0)[source]#
Description#
Compute the mean around median along the first axis [1]:
\[\big[\mathrm{Meamed}_{f}(x_1, \ldots, x_n)\big]_k = \frac{1}{n-f} \sum_{j=1}^{n-f} \big[x_{\pi(j)}\big]_k\]where
\(x_1, \dots, x_n\) are the input vectors, which conceptually correspond to gradients submitted by honest and Byzantine participants during a training iteration.
\(f\) conceptually represents the expected number of Byzantine vectors.
\(\big[\cdot\big]_k\) refers to the \(k\)-th coordinate.
\(\mathrm{median}\) refers to the median of \(n\) scalars.
\(\pi\) denotes a permutation on \(\big[n\big]\) that sorts the input vectors based on their \(k\)-th coordinate in non-decreasing order of distance to the \(\mathrm{median}\) of the \(k\)-th coordinate across the input vectors. This sorting is expressed as:
\(\Big|\big[x_{\pi_k(1)}\big]_k - \mathrm{median}\big(\big[x_1\big]_k, \ldots, \big[x_n\big]_k\big)\Big| \leq \ldots \leq \Big|\big[x_{\pi_k(n)}\big]_k - \mathrm{median}\big(\big[x_1\big]_k, \ldots, \big[x_n\big]_k\big)\Big|\).
In other words, Meamed computes the average of the \(n-f\) closest elements to the \(\mathrm{median}\) for each dimension \(k\).
- Initialization parameters:
f (int, optional) – Number of faulty vectors. Set to 0 by default.
Calling the instance
- Input parameters:
vectors (numpy.ndarray, torch.Tensor, list of numpy.ndarray or list of torch.Tensor) – A set of vectors, matrix or tensors.
- Returns:
numpy.ndarray or torch.Tensor – The data type of the output will be the same as the input.
Examples
>>> import byzfl >>> agg = byzfl.Meamed(1)
Using numpy arrays
>>> import numpy as np >>> x = np.array([[1., 2., 3.], # np.ndarray >>> [4., 5., 6.], >>> [7., 8., 9.]]) >>> agg(x) array([2.5, 3.5, 4.5])
Using torch tensors
>>> import torch >>> x = torch.tensor([[1., 2., 3.], # torch.tensor >>> [4., 5., 6.], >>> [7., 8., 9.]]) >>> agg(x) tensor([2.5000, 3.5000, 4.5000])
Using list of numpy arrays
>>> import numpy as np >>> x = [np.array([1., 2., 3.]), # list of np.ndarray >>> np.array([4., 5., 6.]), >>> np.array([7., 8., 9.])] >>> agg(x) array([2.5, 3.5, 4.5])
Using list of torch tensors
>>> import torch >>> x = [torch.tensor([1., 2., 3.]), # list of torch.tensor >>> torch.tensor([4., 5., 6.]), >>> torch.tensor([7., 8., 9.])] >>> agg(x) tensor([2.5000, 3.5000, 4.5000])
References