fedn.utils.helpers.plugins package

Submodules

fedn.utils.helpers.plugins.androidhelper module

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

Bases: HelperBase

FEDn helper class for android json model weights.

__init__()[source]

Initialize helper.

get_tmp_path()[source]

Return a temporary output path compatible with save_model, load_model.

Returns:

Path to file.

increment_average(model, model_next, num_examples, total_examples)[source]

Incremental weighted average of model weights.

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

  • model_next (list of numpy arrays.) – New model weights.

  • num_examples (int) – Number of examples in new model.

  • total_examples (int) – Total number of examples.

Returns:

Incremental weighted average of model weights.

Return type:

list of numpy arrays.

increment_average_add(model, model_next, num_examples, total_examples)[source]

Incremental weighted average of model weights.

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

  • model_next (list of numpy arrays.) – New model weights.

  • num_examples (int) – Number of examples in new model.

  • total_examples (int) – Total number of examples.

Returns:

Incremental weighted average of model weights.

Return type:

list of numpy arrays.

load(fh)[source]

Load weights from file or filelike.

Parameters:

fh – file path, filehandle, filelike.

Returns:

List of weights in json format.

save(weights, path=None)[source]

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

Parameters:
  • weights – weights in json format.

  • path – Path to file.

Returns:

Path to file.

fedn.utils.helpers.plugins.binaryhelper module

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

Bases: Helper

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

__init__()[source]

Initialize helper.

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

Load weights from file or filelike.

Parameters:

path – file path, filehandle, filelike.

Returns:

List of weights in numpy format.

save(model, path=None, file_type='raw_binary')[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.

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.

__init__()[source]

Initialize helper.

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

fedn.utils.helpers.plugins.splitlearninghelper module

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

Bases: HelperBase

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

__init__()[source]

Initialize helper.

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

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(embedding1, embedding2)[source]

Concatenates two embeddings of format {client_id: embedding} into a new dictionary

Parameters:
  • embedding1 – First embedding dictionary

  • embedding2 – Second embedding dictionary

Returns:

Concatenated embedding dictionary

load(path)[source]

Load embeddings/gradients.

Parameters:

path – Path to file

Returns:

Dict mapping client IDs to numpy arrays (either embeddings or gradients)

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

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

Parameters:
  • model – Weights in array-like format.

  • path – Path to file.