fedn.utils.plugins package

The plugins package is responsible for loading model helper functions supporting different ML frameworks. The fedn.utils.plugins.helperbase.HelperBase is an abstract class which user can implement their own helper functions to support different ML frameworks.

Submodules

fedn.utils.plugins.helperbase module

class fedn.utils.plugins.helperbase.HelperBase[source]

Bases: ABC

Abstract class defining helpers.

get_tmp_path()[source]

Return a temporary output path compatible with save_model, load_model.

Returns:

Path to file.

abstract increment_average(model, model_next, a, W)[source]

Compute one increment of incremental weighted averaging.

Parameters:
  • model – Current model weights in array-like format.

  • model_next – New model weights in array-like format.

  • a – Number of examples in new model.

  • W – Total number of examples.

Returns:

Incremental weighted average of model weights.

abstract load(fh)[source]

Load weights from file or filelike.

Parameters:

fh – file path, filehandle, filelike.

Returns:

Weights in array-like format.

abstract save(model, path)[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.

fedn.utils.plugins.kerashelper module

class fedn.utils.plugins.kerashelper.Helper[source]

Bases: HelperBase

FEDn helper class for keras.Sequential.

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 numpy format.

save(weights, path=None)[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.

Returns:

Path to file.

fedn.utils.plugins.numpyarrayhelper module

class fedn.utils.plugins.numpyarrayhelper.Helper[source]

Bases: HelperBase

FEDn helper class for numpy arrays.

increment_average(model, model_next, n)[source]

Update an incremental average.

Parameters:
  • model (numpy array.) – Current model weights.

  • model_next (numpy array.) – New model weights.

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

Returns:

Incremental weighted average of model weights.

Return type:

numpy.array

load(path)[source]

Load weights/parameters from file or filelike.

Parameters:

path (str) – Path to file.

Returns:

Weights/parameters in numpy array format.

Return type:

numpy.array

save(model, path=None)[source]

Serialize weights/parameters to file.

Parameters:
  • model (numpy array.) – Weights/parameters in numpy array format.

  • path (str) – Path to file.

Returns:

Path to file.

Return type:

str

fedn.utils.plugins.pytorchhelper module

class fedn.utils.plugins.pytorchhelper.Helper[source]

Bases: HelperBase

FEDn helper class for pytorch.

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

Update a weighted incremental average of model weights.

Parameters:
  • model (OrderedDict) – Current model weights with keys from torch state_dict.

  • model_next (OrderedDict) – New model weights with keys from torch state_dict.

  • 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:

OrderedDict

load(path)[source]

Load weights from file or filelike.

Parameters:

path (str) – file path, filehandle, filelike.

Returns:

Weights of model with keys from torch state_dict.

Return type:

OrderedDict

save(model, path=None)[source]

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

Parameters:
  • model (OrderedDict) – Weights of model with keys from torch state_dict.

  • path (str) – File path.

Returns:

Path to file (generated as tmp file unless path is set).

Return type:

str