Client#
- class fl_sim.nodes.Client(client_id: int, device: device, model: Module, dataset: FedDataset, config: ClientConfig)[源代码]#
基类:
NodeThe class to simulate the client node.
The client node is responsible for training the local models, and communicating with the server node.
- 参数:
client_id (int) -- The id of the client.
device (torch.device) -- The device to train the model on.
model (torch.nn.Module) -- The model to train.
dataset (FedDataset) -- The dataset to train on.
config (ClientConfig) -- The config for the client.
- accept_parameters() None[源代码]#
Accepts parameters from received_messages.
The named
"model_state"mapping (the recommended exchange format, seeModelState) takes precedence over the flat positional"parameters"list.Need to be implemented in the child class to specify part in received_messages if the key is not "parameters"/"model_state".
Some algorithms (e.g.
IFCA,FedPD) do not put the parameters under the"parameters"key of the received messages (or communicate only with a subset of clients in a round); for those, the parameters are accepted in the client'supdate(), so a missing key is not an error.
- communicate(target: Server) None[源代码]#
Upload the local model parameters and metrics to the target server.
This is the default (typed-API) implementation; subclasses override it for algorithm-specific payloads (deltas, gradients, control variates, etc.). The typed-API way to send additional data is building a
ClientToServerand callingServer.receive_one()on the target; the legacy way (overriding communicate and appending totarget._received_messagesdirectly) keeps working.- 参数:
target (Server) -- The server to upload to.
- 返回类型:
None
- get_all_data() Tuple[Tensor, Tensor][源代码]#
Get all the data on the client.
This method is a helper function for fast access to the data on the client, including both training and validation data; both features and labels.
- receive(message: ServerToClient) None[源代码]#
Receive a typed message broadcast from the server.
The message is stored in
self._latest_message(for typed-API consumers) and merged into the legacyself._received_messagesdict (for legacy consumers, e.g.accept_parameters()).- 参数:
message (ServerToClient) -- The typed message broadcast from the server.
- 返回类型:
None
- solve_inner() None[源代码]#
Main part of inner loop solver.
Basic example:
self.model.train() epoch_losses = [] for epoch in range(self.config.num_epochs): batch_losses = [] for batch_idx, (data, target) in enumerate(self.train_loader): data, target = data.to(self.device), target.to(self.device) self.optimizer.zero_grad() output = self.model(data) loss = self.criterion(output, target) loss.backward() self.optimizer.step() batch_losses.append(loss.item()) epoch_losses.append(sum(batch_losses) / len(batch_losses)) self.lr_scheduler.step()
- abstract train() None[源代码]#
Main part of inner loop solver.
Basic example:
self.model.train() epoch_losses = [] for epoch in range(self.config.num_epochs): batch_losses = [] for batch_idx, (data, target) in enumerate(self.train_loader): data, target = data.to(self.device), target.to(self.device) self.optimizer.zero_grad() output = self.model(data) loss = self.criterion(output, target) loss.backward() self.optimizer.step() batch_losses.append(loss.item()) epoch_losses.append(sum(batch_losses) / len(batch_losses)) self.lr_scheduler.step()