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,
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_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).
- 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,
Calls
batch_predict_proba()
with a batch of size 1.
- train(instance: LabeledInstance) None [source]#
Calls
batch_train()
with a batch of size 1.
- random_seed: int#
The random seed for reproducibility.
When implementing a classifier ensure random number generators are seeded.