Finetune#

class capymoa.classifier.Finetune[source]#

Bases: BatchClassifier

Finetune a PyTorch neural network using stochastic gradient descent.

>>> from capymoa.datasets import ElectricityTiny
>>> from capymoa.evaluation import prequential_evaluation
>>> from capymoa.classifier import Finetune
>>> from capymoa.ann import Perceptron
>>> from torch import nn
>>> from torch.optim import Adam
>>> from functools import partial
>>>
>>> stream = ElectricityTiny()
>>> learner = Finetune(
...     stream.get_schema(),
...     model=Perceptron,
...     optimizer=partial(Adam, lr=0.01)
... )
>>> results = prequential_evaluation(stream, learner, batch_size=32)
>>> print(f"{results['cumulative'].accuracy():.1f}")
62.4

Alternatively, you can use a custom model and optimizer:

>>> model = nn.Sequential(nn.Linear(6, 10), nn.ReLU(), nn.Linear(10, 2))
>>> optimizer = Adam(model.parameters(), lr=0.001)
>>> learner = Finetune(
...     schema=stream.get_schema(),
...     model=model,
...     optimizer=optimizer,
... )
>>> results = prequential_evaluation(stream, learner, batch_size=32)
>>> print(f"{results['cumulative'].accuracy():.1f}")
60.4
__init__(
schema: Schema,
model: Module | Callable[[Schema], Module],
optimizer: Optimizer | Callable[[Iterator[Tensor]], Optimizer] = optim.Adam,
criterion: Module = nn.CrossEntropyLoss(),
device: device | str = 'cpu',
random_seed: int = 0,
) None[source]#

Construct a learner to finetune a neural network.

Parameters:
  • schema – Describes streaming data types and shapes.

  • model – A classifier model that takes a (bs, input_dim) matrix and returns a (bs, num_classes) matrix. Alternatively, a constructor function that takes a schema and returns a model.

  • optimizer – A PyTorch gradient descent optimizer or a constructor function that takes the model parameters and returns an optimizer.

  • criterion – Loss function to use for training.

  • device – Hardware for training.

  • random_seed – Seeds torch torch.manual_seed().

batch_predict(x: Tensor) Tensor[source]#

Predict the labels for a batch of instances.

Parameters:

x – Batch of x_dtype valued feature vectors (batch_size, num_features)

Returns:

Predicted batch of y_dtype valued labels (batch_size,).

batch_predict_proba(x: Tensor) Tensor[source]#

Predict the probabilities of the classes for the given batch of data.

Parameters:
  • x – Input data of shape (batch_size, num_features).

  • y – Target labels of shape (batch_size,).

Returns:

Predicted probabilities of shape (batch_size, num_classes).

batch_train(x: Tensor, y: Tensor) None[source]#

Train with a batch of instances.

Parameters:
  • x – Batch of x_dtype valued feature vectors (batch_size, num_features)

  • y – Batch of y_dtype valued labels (batch_size,).

predict(instance: Instance) int | None[source]#

Predict the label of an instance.

The base implementation calls predict_proba() and returns the label with the highest probability.

Parameters:

instance – The instance to predict the label for.

Returns:

The predicted label or None if the classifier is unable to make a prediction.

predict_proba(
instance: Instance,
) ndarray[Any, dtype[float64]] | None[source]#

Calls batch_predict_proba() with a batch of size 1.

train(instance: LabeledInstance) None[source]#

Calls batch_train() with a batch of size 1.

criterion: Module#

The loss function to be used for training.

device: device = device(type='cpu')#

The device to be used for training.

dtype: dtype#

The data type to convert the input data to.

model: Module#

The model to be trained.

optimizer: Optimizer#

The optimizer to be used for training.

random_seed: int#

The random seed for reproducibility.

When implementing a classifier ensure random number generators are seeded.

schema: Schema#

The schema representing the instances.

x_dtype: dtype = torch.float32[source]#

Data type for the input features.

y_dtype: dtype = torch.int64[source]#

Data type for the target value/labels.