classifier
¶
Classes:
Name | Description |
---|---|
Classifier |
Incremental PyTorch classifier with optional dynamic feature & class growth. |
Classifier
¶
Classifier(
module: Module,
loss_fn: Union[str, Callable],
optimizer_fn: Union[str, type],
lr: float = 0.001,
output_is_logit: bool = True,
is_class_incremental: bool = False,
is_feature_incremental: bool = False,
device: str = "cpu",
seed: int = 42,
gradient_clip_value: float | None = None,
**kwargs
)
Bases: DeepEstimator
, MiniBatchClassifier
Incremental PyTorch classifier with optional dynamic feature & class growth.
This wrapper turns an arbitrary torch.nn.Module
into an incremental
classifier that follows the :mod:river
API. It can optionally expand its
input dimensionality when previously unseen feature names occur
(is_feature_incremental=True
) and expand the output layer when new class
labels appear (is_class_incremental=True
).
When loss_fn='cross_entropy'
targets are handled as integer class indices;
otherwise they are converted to one-hot vectors to match the output dimension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module
|
Module
|
The underlying PyTorch model producing (logit) outputs. |
required |
loss_fn
|
str | Callable
|
Loss identifier (e.g. |
required |
optimizer_fn
|
str | type
|
Optimizer identifier ( |
required |
lr
|
float
|
Learning rate passed to the optimizer. |
1e-3
|
output_is_logit
|
bool
|
If True, |
True
|
is_class_incremental
|
bool
|
Whether to expand the output layer when new class labels appear. |
False
|
is_feature_incremental
|
bool
|
Whether to expand the input layer when new feature names are observed. |
False
|
device
|
str
|
Runtime device. |
'cpu'
|
seed
|
int
|
Random seed. |
42
|
gradient_clip_value
|
float | None
|
Norm to clip gradients to (disabled if |
None
|
**kwargs
|
Extra parameters retained for reconstruction. |
{}
|
Examples:
Online binary classification on the Phishing dataset from :mod:`river`.
We build a tiny MLP and maintain an online Accuracy metric. The exact value
may vary depending on library version and hardware::
>>> import random, numpy as np
>>> import torch
>>> from torch import nn, manual_seed
>>> from river import datasets, metrics
>>> from deep_river.classification import Classifier
>>> _ = manual_seed(42); random.seed(42); np.random.seed(42)
>>> first_x, _ = next(iter(datasets.Phishing()))
>>> n_features = len(first_x)
>>> class SmallMLP(nn.Module):
... def __init__(self, n_features):
... super().__init__()
... self.net = nn.Sequential(
... nn.Linear(n_features, 16),
... nn.ReLU(),
... nn.Linear(16, 2)
... )
... def forward(self, x):
... return self.net(x) # raw logits
>>> clf = Classifier(
... module=SmallMLP(n_features),
... loss_fn='cross_entropy',
... optimizer_fn='sgd',
... lr=1e-2,
... is_class_incremental=True
... )
>>> acc = metrics.Accuracy()
>>> for i, (x, y) in enumerate(datasets.Phishing().take(200)):
... if i > 0: # only predict after first sample is seen
... y_pred = clf.predict_one(x)
... acc.update(y, y_pred)
... clf.learn_one(x, y)
>>> print(f"Accuracy: {acc.get():.4f}") # doctest: +ELLIPSIS
Accuracy: ...
Methods:
Name | Description |
---|---|
clone |
Return a fresh estimator instance with (optionally) copied state. |
draw |
Render a (partial) computational graph of the wrapped model. |
learn_many |
Learn from a batch of instances. |
learn_one |
Learn from a single instance. |
load |
Load a previously saved estimator. |
predict_proba_many |
Predict probabilities for a batch of instances. |
predict_proba_one |
Predict class membership probabilities for one instance. |
save |
Persist the estimator (architecture, weights, optimiser & runtime state). |
Source code in deep_river/classification/classifier.py
clone
¶
Return a fresh estimator instance with (optionally) copied state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_params
|
dict | None
|
Parameter overrides for the cloned instance. |
None
|
include_attributes
|
bool
|
If True, runtime state (observed features, buffers) is also copied. |
False
|
copy_weights
|
bool
|
If True, model weights are copied (otherwise the module is re‑initialised). |
False
|
Source code in deep_river/base.py
draw
¶
Render a (partial) computational graph of the wrapped model.
Imports graphviz
and torchviz
lazily. Raises an informative
ImportError if the optional dependencies are not installed.
Source code in deep_river/base.py
learn_many
¶
Learn from a batch of instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
Batch of feature rows. |
required |
y
|
Series
|
Corresponding labels. |
required |
Source code in deep_river/classification/classifier.py
learn_one
¶
Learn from a single instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
dict
|
Feature dictionary. |
required |
y
|
hashable
|
Class label. |
required |
Source code in deep_river/classification/classifier.py
load
classmethod
¶
Load a previously saved estimator.
The method reconstructs the estimator class, its wrapped module, optimiser state and runtime information (feature names, buffers, etc.).
Source code in deep_river/base.py
predict_proba_many
¶
Predict probabilities for a batch of instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
Feature matrix. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
Each row sums to 1 (multi-class) or has two columns for binary. |
Source code in deep_river/classification/classifier.py
predict_proba_one
¶
Predict class membership probabilities for one instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
dict
|
Feature dictionary. |
required |
Returns:
Type | Description |
---|---|
dict
|
Mapping from label -> probability. |
Source code in deep_river/classification/classifier.py
save
¶
Persist the estimator (architecture, weights, optimiser & runtime state).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath
|
str | Path
|
Destination file. Parent directories are created automatically. |
required |