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
- Parameters
data (Tuple[torch.Tensor, torch.Tensor]) –
- 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) –
See also
- evaluate(data)#
Evaluate the model.
- 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) –
See also
- 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.
See also
- 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). Thecriterion
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) –
- 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). Thecriterion
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). Thecriterion
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
Notes
Currently, only metrics for classification tasks are implemented. Specifically, the evaluation metrics are:
accuracy
,precision
,recall
,f1
, and, when possible,roc_auc
.