fedn.utils.helpers.plugins package

Submodules

fedn.utils.helpers.plugins.numpyhelper module

class fedn.utils.helpers.plugins.numpyhelper.Helper[source]

Bases: HelperBase

FEDn helper class for models weights/parameters that can be transformed to numpy ndarrays.

add(m1, m2, a=1.0, b=1.0)[source]

m1*a + m2*b

Parameters:
  • model (list of ndarrays) – Current model weights.

  • model_next (list of ndarrays) – New model weights.

Returns:

Incremental weighted average of model weights.

Return type:

list of ndarrays

check_supported_file_type(file_type)[source]

Check if the file type is supported.

Parameters:

file_type (str) – File type to check.

Returns:

True if supported, False otherwise.

Return type:

bool

divide(m1, m2)[source]

Subtract weights.

Parameters:
  • m1 (list of ndarrays) – Current model weights.

  • m2 (list of ndarrays) – New model weights.

Returns:

m1/m2.

Return type:

list of ndarrays

get_tmp_path(suffix='.npz')[source]

Return a temporary output path compatible with save_model, load_model.

Parameters:

suffix – File suffix.

Returns:

Path to file.

increment_average(m1, m2, n, N)[source]

Update a weighted incremental average of model weights.

Parameters:
  • m1 – Current parameters.

  • m2 – next parameters.

  • n (int) – Number of examples used for updating m2.

  • N (int) – Total number of examples (accumulated).

Returns:

Updated incremental weighted average.

Return type:

list of numpy ndarray

load(path, file_type='npz')[source]

Load weights from file or filelike.

Parameters:

path – file path, filehandle, filelike.

Returns:

List of weights in numpy format.

multiply(m1, m2)[source]

Multiply m1 by m2.

Parameters:
  • m1 (list of ndarrays) – Current model weights.

  • m2 (list of ndarrays) – New model weights.

Returns:

m1.*m2

Return type:

list of ndarrays

norm(m)[source]

Return the norm (L1) of model weights.

Parameters:

m (list of ndarrays) – Current model weights.

Returns:

norm of m

Return type:

float

ones(m1, a)[source]

Return a list of numpy arrays of the same shape as m1, filled with ones.

Parameters:
  • m1 (list of ndarrays) – Current model weights.

  • a (float) – Scalar value.

Returns:

list of numpy arrays of the same shape as m1, filled with ones.

Return type:

list of ndarrays

power(m1, a)[source]

m1 raised to the power of m2.

Parameters:
  • m1 (list of ndarrays) – Current model weights.

  • m2 – New model weights.

Returns:

m1.^m2

Return type:

list of ndarrays

save(weights, path=None, file_type='npz')[source]

Serialize weights to file. The serialized model must be a single binary object.

Parameters:
  • weights – List of weights in numpy format.

  • path – Path to file.

  • file_type – File type to save to. Can be ‘npz’ or ‘raw_binary’. Default is ‘npz’.

Returns:

Path to file.

sign(m)[source]

Sign of m.

Parameters:

m (list of ndarrays) – Model parameters.

Returns:

sign(m)

Return type:

list of ndarrays

sqrt(m1)[source]

Sqrt of m1, element-wise.

Parameters:
  • m1 – Current model weights.

  • model_next (list of ndarrays) – New model weights.

Returns:

sqrt(m1)

Return type:

list of ndarrays

subtract(m1, m2, a=1.0, b=1.0)[source]

m1*a - m2*b.

Parameters:
  • m1 (list of ndarrays) – Current model weights.

  • m2 (list of ndarrays) – New model weights.

Returns:

m1*a-m2*b

Return type:

list of ndarrays