3.1.1. aijack.collaborative.core package#
3.1.1.1. Submodules#
3.1.1.2. aijack.collaborative.core.api module#
- class aijack.collaborative.core.api.BaseFLKnowledgeDistillationAPI(server, clients, public_dataloader, local_dataloaders, validation_dataloader, criterion, num_communication, device)[source]#
Bases:
object
Abstract class for API of federated learning with knowledge distillation.
- Parameters
server (aijack.collaborative.core.BaseServer) – the server
clients (List[aijack.collaborative.core.BaseClient]) – a list of the clients
public_dataloader (torch.utils.data.DataLoader) – a dataloader for the public dataset
local_dataloaders (List[torch.utils.data.DataLoader]) – a list of local dataloaders
validation_dataloader (torch.utils.data.DataLoader) – a dataloader for the validation dataset
criterion (function) – a function to calculate the loss
num_communication (int) – the number of communication
device (str) – device type
- local_score()[source]#
Returns the local performance of each clients.
- Returns
performance of global model and local models
- Return type
Dict[str, int]
3.1.1.3. aijack.collaborative.core.client module#
- class aijack.collaborative.core.client.BaseClient(model, user_id=0)[source]#
Bases:
torch.nn.modules.module.Module
Abstract class foe the client of collaborative learning.
- Parameters
model (torch.nn.Module) – a local model
user_id (int, optional) – id of this client. Defaults to 0.
- backward(loss)[source]#
Execute backward mode automatic differentiation with the give loss.
- Parameters
loss (torch.Tensor) – the value of calculated loss.
- eval()[source]#
Sets the module in evaluation mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout
,BatchNorm
, etc.This is equivalent with
self.train(False)
.See locally-disable-grad-doc for a comparison between .eval() and several similar mechanisms that may be confused with it.
- Returns
self
- Return type
Module
- forward(x)[source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- train()[source]#
Sets the module in training mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout
,BatchNorm
, etc.- Parameters
mode (bool) – whether to set training mode (
True
) or evaluation mode (False
). Default:True
.- Returns
self
- Return type
Module
3.1.1.4. aijack.collaborative.core.server module#
- class aijack.collaborative.core.server.BaseServer(clients, server_model, server_id=0)[source]#
Bases:
torch.nn.modules.module.Module
Abstract class for the server of the collaborative learning.
- Parameters
clients (List[BaseClient]) – a list of clients
server_model (torch.nn.Module) – a global model
server_id (int, optional) – the id of this server. Defaults to 0.
- eval()[source]#
Sets the module in evaluation mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout
,BatchNorm
, etc.This is equivalent with
self.train(False)
.See locally-disable-grad-doc for a comparison between .eval() and several similar mechanisms that may be confused with it.
- Returns
self
- Return type
Module
- forward(x)[source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- train()[source]#
Sets the module in training mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout
,BatchNorm
, etc.- Parameters
mode (bool) – whether to set training mode (
True
) or evaluation mode (False
). Default:True
.- Returns
self
- Return type
Module
3.1.1.5. aijack.collaborative.core.utils module#
3.1.1.6. Module contents#
Subpackge implementing abstract classes for collaborative learning.
- class aijack.collaborative.core.BaseClient(model, user_id=0)[source]#
Bases:
torch.nn.modules.module.Module
Abstract class foe the client of collaborative learning.
- Parameters
model (torch.nn.Module) – a local model
user_id (int, optional) – id of this client. Defaults to 0.
- backward(loss)[source]#
Execute backward mode automatic differentiation with the give loss.
- Parameters
loss (torch.Tensor) – the value of calculated loss.
- eval()[source]#
Sets the module in evaluation mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout
,BatchNorm
, etc.This is equivalent with
self.train(False)
.See locally-disable-grad-doc for a comparison between .eval() and several similar mechanisms that may be confused with it.
- Returns
self
- Return type
Module
- forward(x)[source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- train()[source]#
Sets the module in training mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout
,BatchNorm
, etc.- Parameters
mode (bool) – whether to set training mode (
True
) or evaluation mode (False
). Default:True
.- Returns
self
- Return type
Module
- class aijack.collaborative.core.BaseFLKnowledgeDistillationAPI(server, clients, public_dataloader, local_dataloaders, validation_dataloader, criterion, num_communication, device)[source]#
Bases:
object
Abstract class for API of federated learning with knowledge distillation.
- Parameters
server (aijack.collaborative.core.BaseServer) – the server
clients (List[aijack.collaborative.core.BaseClient]) – a list of the clients
public_dataloader (torch.utils.data.DataLoader) – a dataloader for the public dataset
local_dataloaders (List[torch.utils.data.DataLoader]) – a list of local dataloaders
validation_dataloader (torch.utils.data.DataLoader) – a dataloader for the validation dataset
criterion (function) – a function to calculate the loss
num_communication (int) – the number of communication
device (str) – device type
- local_score()[source]#
Returns the local performance of each clients.
- Returns
performance of global model and local models
- Return type
Dict[str, int]
- class aijack.collaborative.core.BaseFedAPI[source]#
Bases:
object
Abstract class for Federated Learning API
- class aijack.collaborative.core.BaseServer(clients, server_model, server_id=0)[source]#
Bases:
torch.nn.modules.module.Module
Abstract class for the server of the collaborative learning.
- Parameters
clients (List[BaseClient]) – a list of clients
server_model (torch.nn.Module) – a global model
server_id (int, optional) – the id of this server. Defaults to 0.
- eval()[source]#
Sets the module in evaluation mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout
,BatchNorm
, etc.This is equivalent with
self.train(False)
.See locally-disable-grad-doc for a comparison between .eval() and several similar mechanisms that may be confused with it.
- Returns
self
- Return type
Module
- forward(x)[source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- train()[source]#
Sets the module in training mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout
,BatchNorm
, etc.- Parameters
mode (bool) – whether to set training mode (
True
) or evaluation mode (False
). Default:True
.- Returns
self
- Return type
Module