gossipy.model.handler module#

Module contents#

class gossipy.model.handler.AdaLineHandler(net, learning_rate, create_model_mode=CreateModelMode.UPDATE, copy_model=True)#

Bases: gossipy.model.handler.ModelHandler

This class implements the AdaLine model handler.

Parameters
  • net (AdaLine) – The AdaLine model.

  • learning_rate (float) – The learning rate.

  • create_model_mode (CreateModelMode, default=CreateModelMode.UPDATE) – The mode in which the model is created/updated.

  • copy_model (bool, default=True) – Whether to use a copy of the model (i.e., net) or not.

evaluate(data)#

Evaluate the model.

Return type

Dict[str, int]

Parameters

data (Tuple[torch.Tensor, torch.Tensor]) –

init()#

Initialize the model.

Return type

None

class gossipy.model.handler.KMeansHandler(k, dim, alpha=0.1, matching='naive', create_model_mode=CreateModelMode.UPDATE)#

Bases: gossipy.model.handler.ModelHandler

The ModelHandler class is the base class for all model handlers.

A ModelHandler class manages the training (i.e., update) and evaluation of a model. It is also responsible for the merging of two (or more) models according to the mode.

A ModelHandler is a callable object. Calling a ModelHandler instance will preform the update according to the mode.

Parameters
  • create_model_mode (CreateModelMode, default=CreateModelMode.MERGE_UPDATE) – The mode in which the model is created/updated.

  • k (int) –

  • dim (int) –

  • alpha (float) –

  • matching (str) –

evaluate(data)#

Evaluate the model.

Return type

Dict[str, float]

Parameters

data (Tuple[torch.FloatTensor, torch.LongTensor]) –

get_size()#

Return the size of the model.

Returns

The size of the model.

Return type

int

init()#

Initialize the model.

Return type

None

class gossipy.model.handler.MFModelHandler(dim, n_items, lam_reg=0.1, learning_rate=0.001, create_model_mode=CreateModelMode.UPDATE)#

Bases: gossipy.model.handler.ModelHandler

The ModelHandler class is the base class for all model handlers.

A ModelHandler class manages the training (i.e., update) and evaluation of a model. It is also responsible for the merging of two (or more) models according to the mode.

A ModelHandler is a callable object. Calling a ModelHandler instance will preform the update according to the mode.

Parameters
  • create_model_mode (CreateModelMode, default=CreateModelMode.MERGE_UPDATE) – The mode in which the model is created/updated.

  • dim (int) –

  • n_items (int) –

  • lam_reg (float) –

  • learning_rate (float) –

evaluate(ratings)#

Evaluate the model.

Return type

Dict[str, float]

get_size()#

Return the size of the model.

Returns

The size of the model.

Return type

int

init(r_min=1, r_max=5)#

Initialize the model.

Return type

None

Parameters
  • r_min (int) –

  • r_max (int) –

class gossipy.model.handler.ModelHandler(create_model_mode=CreateModelMode.MERGE_UPDATE, *args, **kwargs)#

Bases: gossipy.Sizeable, gossipy.model.handler.ModelEqualityMixin, abc.ABC

The ModelHandler class is the base class for all model handlers.

A ModelHandler class manages the training (i.e., update) and evaluation of a model. It is also responsible for the merging of two (or more) models according to the mode.

A ModelHandler is a callable object. Calling a ModelHandler instance will preform the update according to the mode.

Parameters

create_model_mode (CreateModelMode, default=CreateModelMode.MERGE_UPDATE) – The mode in which the model is created/updated.

caching(owner)#

Cache the model handler and return the cache key.

Parameters

owner (int) – The ID of the client that own of this particular model handler.

Returns

The cache key corresponding to this model handler in the cache.

Return type

CacheKey

copy()#

Return a deep copy of the model handler.

Return type

Any

abstract evaluate(*args, **kwargs)#

Evaluate the model.

Return type

Any

get_size()#

Return the size of the model.

Returns

The size of the model.

Return type

int

abstract init(*args, **kwargs)#

Initialize the model.

Return type

None

class gossipy.model.handler.PartitionedTMH(net, tm_partition, optimizer, optimizer_params, criterion, local_epochs=1, batch_size=32, create_model_mode=CreateModelMode.MERGE_UPDATE, copy_model=True)#

Bases: gossipy.model.handler.TorchModelHandler

Handler for torch models.

This handler is responsible for the training and evaluation of a pytorch model. Thus it requires a TorchModel instance that represents the model to be trained, and an optimizer (e.g., torch.optim.SGD) with its parameters (a dict). The criterion is the loss function to be used for the training.

Parameters
  • net (TorchModel) – The model to be trained.

  • optimizer (torch.optim.Optimizer) – The optimizer to be used for the training.

  • optimizer_params (Dict[str, Any]) – The parameters of the optimizer.

  • criterion (Callable[[torch.Tensor, torch.Tensor], torch.Tensor]) – The loss function to be used for the training.

  • local_epochs (int, default=1) – The number of local epochs.

  • batch_size (int, default=32) – The batch size.

  • create_model_mode (CreateModelMode, default=CreateModelMode.MERGE_UPDATE) – The mode in which the model is created/updated

  • copy_model (bool, default=True) – Whether to use a copy of the model (i.e., net) or not.

  • tm_partition (TorchModelPartition) –

caching(owner)#

Cache the model handler and return the cache key.

Parameters

owner (int) – The ID of the client that own of this particular model handler.

Returns

The cache key corresponding to this model handler in the cache.

Return type

CacheKey

class gossipy.model.handler.PegasosHandler(net, learning_rate, create_model_mode=CreateModelMode.UPDATE, copy_model=True)#

Bases: gossipy.model.handler.AdaLineHandler

This class implements the Pegasos model handler.

Parameters
  • net (AdaLine) – The Pegasos (same as AdaLine) model.

  • learning_rate (float) – The learning rate.

  • create_model_mode (CreateModelMode, default=CreateModelMode.UPDATE) – The mode in which the model is created/updated.

  • copy_model (bool, default=True) – Whether to use a copy of the model (i.e., net) or not.

class gossipy.model.handler.SamplingTMH(sample_size, *args, **kwargs)#

Bases: gossipy.model.handler.TorchModelHandler

Handler for torch models.

This handler is responsible for the training and evaluation of a pytorch model. Thus it requires a TorchModel instance that represents the model to be trained, and an optimizer (e.g., torch.optim.SGD) with its parameters (a dict). The criterion is the loss function to be used for the training.

Parameters
  • net (TorchModel) – The model to be trained.

  • optimizer (torch.optim.Optimizer) – The optimizer to be used for the training.

  • optimizer_params (Dict[str, Any]) – The parameters of the optimizer.

  • criterion (Callable[[torch.Tensor, torch.Tensor], torch.Tensor]) – The loss function to be used for the training.

  • local_epochs (int, default=1) – The number of local epochs.

  • batch_size (int, default=32) – The batch size.

  • create_model_mode (CreateModelMode, default=CreateModelMode.MERGE_UPDATE) – The mode in which the model is created/updated

  • copy_model (bool, default=True) – Whether to use a copy of the model (i.e., net) or not.

  • sample_size (float) –

class gossipy.model.handler.TorchModelHandler(net, optimizer, optimizer_params, criterion, local_epochs=1, batch_size=32, create_model_mode=CreateModelMode.MERGE_UPDATE, copy_model=True)#

Bases: gossipy.model.handler.ModelHandler

Handler for torch models.

This handler is responsible for the training and evaluation of a pytorch model. Thus it requires a TorchModel instance that represents the model to be trained, and an optimizer (e.g., torch.optim.SGD) with its parameters (a dict). The criterion is the loss function to be used for the training.

Parameters
  • net (TorchModel) – The model to be trained.

  • optimizer (torch.optim.Optimizer) – The optimizer to be used for the training.

  • optimizer_params (Dict[str, Any]) – The parameters of the optimizer.

  • criterion (Callable[[torch.Tensor, torch.Tensor], torch.Tensor]) – The loss function to be used for the training.

  • local_epochs (int, default=1) – The number of local epochs.

  • batch_size (int, default=32) – The batch size.

  • create_model_mode (CreateModelMode, default=CreateModelMode.MERGE_UPDATE) – The mode in which the model is created/updated

  • copy_model (bool, default=True) – Whether to use a copy of the model (i.e., net) or not.

evaluate(data)#

Evaluates the model on the given data.

Parameters

data (Tuple[torch.Tensor, torch.Tensor]) – The data to use for the evaluation.

Returns

The evaluation results. The dictionary keys are the metrics names, and the values are the corresponding scores.

Return type

Dict[str, int]

Notes

Currently, only metrics for classification tasks are implemented. Specifically, the evaluation metrics are: accuracy, precision, recall, f1, and, when possible, roc_auc.

init()#

Initialize the model.

Return type

None