fl4health.clients.fed_pca_client module

class FedPCAClient(data_path, device, model_save_path)[source]

Bases: NumPyClient

__init__(data_path, device, model_save_path)[source]

Client that facilitates the execution of federated PCA.

Parameters:
  • data_path (Path) – path to the data to be used to load the data for client-side training

  • device (torch.device) – Device indicator for where to send the model, batches, labels etc. Often “cpu” or “cuda”

  • model_save_path (Path) – Path to save the PCA components for use later, perhaps in dimensionality reduction

evaluate(parameters, config)[source]

Evaluate merged principal components on the local validation set.

Parameters:
  • parameters (NDArrays) – Server-merged principal components.

  • config (Config) – Configurations sent by the server to allow for customization of this functions behavior.

Returns:

A loss associated with the evaluation, the number of samples in the validation/test set and the metric_values associated with evaluation.

Return type:

tuple[float, int, dict[str, Scalar]]

fit(parameters, config)[source]

Function to perform the local side of FedPCA. We don’t use any parameters sent by the server. Hence parameters is ignored. We need only the train_data_tensor to do the work

Parameters:
  • parameters (NDArrays) – ignored

  • config (Config) – Configurations sent by the server to allow for customization of this functions behavior.

Returns:

The local principal components following the local training along with the number of samples in the local training dataset and the computed metrics throughout the fit.

Return type:

tuple[NDArrays, int, dict[str, Scalar]]

generate_hash(length=8)[source]

Generates unique hash used as id for client.

Parameters:

length (int, optional) – Length of the generated hash. Defaults to 8.

Returns:

Generated hash of length length

Return type:

str

get_data_loaders(config)[source]

User defined method that returns a PyTorch Train DataLoader and a PyTorch Validation DataLoader

Parameters:

config (Config) – Configurations sent by the server to allow for customization of this functions behavior.

Return type:

tuple[DataLoader, DataLoader]

Returns:

tuple[DataLoader, DataLoader] Tuple of length 2. The client train and validation loader.

Raises:

NotImplementedError – To be defined in child class.

get_data_tensor(data_loader)[source]

This function should be used to “collate” each of the dataloader batches into a single monolithic tensor representing all of the data in the loader.

Parameters:

data_loader (DataLoader) – The dataloader that can be used to iterate through a dataset

Raises:

NotImplementedError – Should be defined by the child class

Returns:

Single torch tensor representing all of the data stacked together.

Return type:

Tensor

get_model(config)[source]

Returns an instance of the PCAModule. This module is used to facilitate FedPCA training on the client side

Parameters:

config (Config) – Configurations sent by the server to allow for customization of this functions behavior.

Returns:

Module that determines how local FedPCA optimization will be performed.

Return type:

PcaModule

get_parameters(config)[source]

Sends all of the model components back to the server. The model in this case represents the principal components that have been computed

Parameters:

config (Config) – Configurations to allow for customization of this functions behavior

Returns:

Parameters representing the principal components computed by the client that need to be aggregated in some way

Return type:

NDArrays

save_model()[source]

Method to save the FedPCA computed principal components to disk. These can be reloaded to allow for dimensionality reduction in subsequent FL training.

Return type:

None

set_parameters(parameters, config)[source]

Sets the merged principal components transferred from the server. Since federated PCA only runs for one round, the principal components obtained here are in fact the final result, so they are saved locally by each client for downstream tasks.

Parameters:
  • parameters (NDArrays) – Aggregated principal components from the server. These are FINAL in the sense that FedPCA only runs for one round.

  • config (Config) – Configurations to allow for customization of this functions behavior

Return type:

None

setup_client(config)[source]

Used to setup all of the components necessary to run FedPCA

Parameters:

config (Config) – Configurations sent by the server to allow for customization of this functions behavior.

Return type:

None