FedProx#
- class FedProx(data_manager, metric_logger, num_clients, sample_scheme, sample_rate, model_def, epochs, criterion_def, optimizer_def=functools.partial(<class 'torch.optim.sgd.SGD'>, lr=1.0), local_optimizer_def=functools.partial(<class 'torch.optim.sgd.SGD'>, lr=0.1), lr_scheduler_def=None, local_lr_scheduler_def=None, r2r_local_lr_scheduler_def=None, batch_size=32, test_batch_size=64, device='cpu', *args, **kwargs)[source]#
Implements FedProx algorithm for centralized FL.
For further details regarding the algorithm we refer to Federated Optimization in Heterogeneous Networks.
- Parameters
data_manager (
distributed.data_management.DataManager
) -- data managermetric_logger (
logall.Logger
) -- metric logger for tracking.num_clients (int) -- number of clients
sample_scheme (
str
) -- mode of sampling clients. Options are'uniform'
and'sequential'
sample_rate (
float
) -- rate of sampling clientsmodel_def (
torch.Module
) -- definition of for constructing the modelepochs (
int
) -- number of local epochscriterion_def (
Callable
) -- loss function defining local objectiveoptimizer_def (
Callable
) -- derfintion of server optimizerlocal_optimizer_def (
Callable
) -- defintoin of local optimizerlr_scheduler_def (
Callable
) -- definition of lr scheduler of server optimizer.local_lr_scheduler_def (
Callable
) -- definition of lr scheduler of local optimizerr2r_local_lr_scheduler_def (
Callable
) -- definition to schedule lr that is delivered to the clients at each round (deterimined init lr of the client optimizer)batch_size (int) -- batch size of the local trianing
test_batch_size (int) -- inference time batch size
device (str) -- cpu, cuda, or gpu number
mu (float) -- FedProx's \(\mu\) hyper-parameter for local regularization
Note
- definition of
- learning rate schedulers, could be any of the ones defined at
torch.optim.lr_scheduler
or any other that implements step and get_last_lr methods._schedulers``.
optimizers, could be any
torch.optim.Optimizer
.model, could be any
torch.Module
.criterion, could be any
fedsim.scores.Score
.
- init(*args, **kwrag)[source]#
this method is executed only once at the time of instantiating the algorithm object. Here you define your model and whatever needed during the training. Remember to write the outcome of your processing to server_storage for access in other methods.
Note
*args
and**kwargs
are directly passed through from algorithm constructor.- Parameters
server_storage (Storage) -- server storage object
- send_to_client(client_id)[source]#
returns context to send to the client corresponding to client_id.
Warning
Do not send shared objects like server model if you made any before you deepcopy it.
- Parameters
server_storage (Storage) -- server storage object.
client_id (int) -- id of the receiving client
- Raises
NotImplementedError -- abstract class to be implemented by child
- Returns
Mapping[Hashable, Any] -- the context to be sent in form of a Mapping
- send_to_server(rounds, storage, datasets, train_split_name, scores, epochs, criterion, train_batch_size, inference_batch_size, optimizer_def, lr_scheduler_def=None, device='cuda', ctx=None, step_closure=None)[source]#
client operation on the recieved information.
- Parameters
id (int) -- id of the client
rounds (int) -- global round number
storage (Storage) -- storage object of the client
datasets (Dict[str, Iterable]) -- this comes from Data Manager
train_split_name (str) -- string containing name of the training split
scores -- Dict[str, Dict[str, Score]]: dictionary of form {'split_name':{'score_name': Score}} for global scores to evaluate at the current round.
epochs (int) -- number of epochs to train
criterion (Score) -- citerion, should be a differentiable fedsim.scores.score
train_batch_size (int) -- training batch_size
inference_batch_size (int) -- inference batch_size
optimizer_def (float) -- class for constructing the local optimizer
lr_scheduler_def (float) -- class for constructing the local lr scheduler
device (Union[int, str], optional) -- Defaults to 'cuda'.
ctx (Optional[Dict[Hashable, Any]], optional) -- context reveived.
- Returns
Mapping[str, Any] -- client context to be sent to the server