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]

abstract run()[source]#
score(dataloader, only_local=False)[source]#

Returns the performance on the given dataset.

Parameters
  • dataloader (torch.utils.data.DataLoader) – a dataloader

  • only_local (bool) – show only the local results

Returns

performance of global model and local models

Return type

Dict[str, int]

train_client(epoch=1, public=True)[source]#

Train local models with the local datasets or the public dataset.

Parameters

public (bool, optional) – Train with the public dataset or the local datasets. Defaults to True.

Returns

a list of average loss of each clients.

Return type

List[float]

class aijack.collaborative.core.api.BaseFedAPI[source]#

Bases: object

Abstract class for Federated Learning API

abstract local_train()[source]#
abstract run()[source]#

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.

abstract download()[source]#

Download the global model from the server.

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.

abstract local_train()[source]#
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

abstract upload()[source]#

Upload the locally learned informatino to the server.

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.

abstract action()[source]#

Execute thr routine of each communication.

abstract distribute()[source]#

Distribute the global model to each client.

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

abstract update()[source]#

Update the global model.

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.

abstract download()[source]#

Download the global model from the server.

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.

abstract local_train()[source]#
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

abstract upload()[source]#

Upload the locally learned informatino to the server.

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]

abstract run()[source]#
score(dataloader, only_local=False)[source]#

Returns the performance on the given dataset.

Parameters
  • dataloader (torch.utils.data.DataLoader) – a dataloader

  • only_local (bool) – show only the local results

Returns

performance of global model and local models

Return type

Dict[str, int]

train_client(epoch=1, public=True)[source]#

Train local models with the local datasets or the public dataset.

Parameters

public (bool, optional) – Train with the public dataset or the local datasets. Defaults to True.

Returns

a list of average loss of each clients.

Return type

List[float]

class aijack.collaborative.core.BaseFedAPI[source]#

Bases: object

Abstract class for Federated Learning API

abstract local_train()[source]#
abstract run()[source]#
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.

abstract action()[source]#

Execute thr routine of each communication.

abstract distribute()[source]#

Distribute the global model to each client.

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

abstract update()[source]#

Update the global model.