fl4health.mixins.personalized.mr_mtl module¶
MR MTL Personalized Mixin.
- class MrMtlPersonalizedMixin(*args, **kwargs)[source]¶
Bases:
AdaptiveDriftConstrainedMixin
- __init__(*args, **kwargs)[source]¶
This client implements the MR-MTL algorithm from MR-MTL: On Privacy and Personalization in Cross-Silo Federated Learning. The idea is that we want to train personalized versions of the global model for each client. However, instead of using a separate solver for the global model, as in Ditto, we update the initial global model with aggregated local models on the server-side and use those weights to also constrain the training of a local model. The constraint for this local model is identical to the FedProx loss. The key difference is that the local model is never replaced with aggregated weights. It is always local.
NOTE: lambda, the drift loss weight, is initially set and potentially adapted by the server akin to the heuristic suggested in the original FedProx paper. Adaptation is optional and can be disabled in the corresponding strategy used by the server
- compute_training_loss(preds, features, target)[source]¶
Computes training losses given predictions of the modes and ground truth data. We add to vanilla loss function by including Mean Regularized (MR) penalty loss which is the \(\ell^2\) inner product between the initial global model weights and weights of the current model.
- Parameters:
preds (TorchPredType) – Prediction(s) of the model(s) indexed by name. All predictions included in dictionary will be used to compute metrics.
features (
dict
[str
,Tensor
]) – (TorchFeatureType): Feature(s) of the model(s) indexed by name.target (
Tensor
|dict
[str
,Tensor
]) – (TorchTargetType): Ground truth data to evaluate predictions against.
- Returns:
An instance of
TrainingLosses
containing backward loss and additional losses indexed by name. Additional losses includes each loss component of the total loss.- Return type:
- get_global_model(config)[source]¶
Returns the global model on client setup to be used as a constraint for the local model during training.
The global model should be the same architecture as the local model so we reuse the
get_model
call. We explicitly send the model to the desired device. This is idempotent.- Parameters:
config (Config) – The config from the server.
- Returns:
The PyTorch model serving as the global model for Ditto
- Return type:
nn.Module
- set_parameters(parameters, config, fitting_round)[source]¶
The parameters being passed are to be routed to the initial global model to be used in a penalty term in training the local model. Despite the usual FL setup, we actually never pass the aggregated model to the LOCAL model. Instead, we use the aggregated model to form the MR-MTL penalty term.
NOTE: In MR-MTL, unlike Ditto, the local model weights are not synced across clients to the initial global model, even in the FIRST ROUND.
- Parameters:
parameters (NDArrays) – Parameters have information about model state to be added to the relevant client model. It will also contain a penalty weight from the server at each round (possibly adapted)
config (Config) – The config is sent by the FL server to allow for customization in the function if desired.
fitting_round (bool) – Boolean that indicates whether the current federated learning round is a fitting round or an evaluation round. Not used here.
- Return type:
- setup_client(config)[source]¶
Set dataloaders, optimizers, parameter exchangers and other attributes derived from these. Then set initialized attribute to True. In this class, this function simply adds the additional step of setting up the global model.
- Parameters:
config (Config) – The config from the server.
- Return type:
- class MrMtlPersonalizedProtocol(*args, **kwargs)[source]¶
Bases:
AdaptiveDriftConstrainedProtocol
,Protocol
- criterion: _Loss¶
- device: torch.device¶
- drift_penalty_tensors: list[torch.Tensor] | None¶
- drift_penalty_weight: float | None¶
- initialized: bool¶
- loss_for_adaptation: float¶
- model: nn.Module¶
- optimizers: dict[str, torch.optim.Optimizer]¶
- parameter_exchanger: FullParameterExchangerWithPacking[float]¶
- penalty_loss_function: WeightDriftLoss¶
- test_loader: DataLoader | None¶
- train_loader: DataLoader¶
- val_loader: DataLoader¶