Gaussian#

class byzfl.Gaussian(mu=0.0, sigma=1.0)[source]#

Bases: object

Description#

Generate a random vector where each coordinate is independently sampled from a Gaussian distribution.

\[\begin{split}\mathrm{Gaussian}_{\mu, \sigma}(x_1, \dots, x_n) = \begin{bmatrix} g_1 \\ g_2 \\ \vdots \\ g_d \end{bmatrix} \in \mathbb{R}^d\end{split}\]

where:

  • \(x_1, \dots, x_n\) are the input vectors, which conceptually correspond to correct gradients submitted by honest participants during a training iteration.

  • \(d\) is the dimensionality of the input space, i.e., \(d\) is the number of coordinates of vectors \(x_1, \dots, x_n\).

  • \(\mathit{N}(\mu, \sigma^2)\) is the Gaussian distribution of mean \(\mu \in \mathbb{R}\) and standard deviation \(\sigma \geq 0\).

  • \(g_i \sim \mathit{N}(\mu, \sigma^2)\) for all \(i \in \{1, \dots, d\}\).

Initialization parameters:
  • mu (float, optional (default=0.0)) – Mean of the Gaussian distribution.

  • sigma (float, optional (default=1.0)) – Standard deviation of the Gaussian distribution.

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 is the same as the input.

Examples

>>> import byzfl
>>> attack = byzfl.Gaussian(mu=0.0, sigma=1.0)

Using numpy arrays

>>> import numpy as np
>>> x = np.array([[1., 2., 3.],       # np.ndarray
>>>               [4., 5., 6.],
>>>               [7., 8., 9.]])
>>> attack(x)
array([-0.08982162  0.07237574  0.55886579])

Using torch tensors

>>> import torch
>>> x = torch.tensor([[1., 2., 3.],   # torch.tensor
>>>                   [4., 5., 6.],
>>>                   [7., 8., 9.]])
>>> attack(x)
tensor([ 0.9791,  0.0266, -1.0112])

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.])]
>>> attack(x)
array([-0.08982162  0.07237574  0.55886579])

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.])]
>>> attack(x)
tensor([ 0.9791,  0.0266, -1.0112])