Skip to content

rolling_classifier

Classes:

Name Description
RollingClassifier

Rolling window variant of :class:Classifier.

RollingClassifier

RollingClassifier(
    module: Module,
    loss_fn: Union[
        str, Callable
    ] = "binary_cross_entropy_with_logits",
    optimizer_fn: Union[str, Type[Optimizer]] = "sgd",
    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,
    window_size: int = 10,
    append_predict: bool = False,
    gradient_clip_value: float | None = None,
    **kwargs
)

Bases: Classifier, RollingDeepEstimator

Rolling window variant of :class:Classifier.

Maintains a fixed-size deque of the most recent observations (window_size) and feeds them as a temporal slice to the underlying module. This enables simple short-term sequence conditioning without explicit recurrent state handling on the user side.

Parameters:

Name Type Description Default
module Module

Classification module consuming a rolling tensor shaped roughly as (seq_len, batch=1, n_features) depending on internal conversion.

required
loss_fn str | Callable

Loss identifier or callable.

'binary_cross_entropy_with_logits'
optimizer_fn str | type

Optimizer specification.

'sgd'
lr float

Learning rate.

1e-3
output_is_logit bool

Whether raw logits are produced (enables post-softmax via output2proba).

True
is_class_incremental bool

Expand output layer when new class labels appear.

False
is_feature_incremental bool

Expand input layer when new feature names appear.

False
device str

Torch device.

'cpu'
seed int

Random seed.

42
window_size int

Number of past samples kept.

10
append_predict bool

If True, predictions are appended to internal window during inference (useful for autoregressive generation).

False
gradient_clip_value float | None

Optional gradient clipping threshold.

None
**kwargs

Forwarded to parent constructors.

{}

Examples:

Streaming binary classification on the Phishing dataset with a tiny RNN.
We only assert the final Accuracy lies in ``[0, 1]`` for doctest stability.

>>> import random, numpy as np, torch
>>> from torch import nn, manual_seed
>>> from river import datasets, metrics
>>> from deep_river.classification import RollingClassifier
>>> _ = manual_seed(42); random.seed(42); np.random.seed(42)
>>> first_x, _ = next(iter(datasets.Phishing()))
>>> n_features = len(first_x)
>>> class TinyRNN(nn.Module):
...     def __init__(self, n_features):
...         super().__init__()
...         self.rnn = nn.RNN(n_features, 8)
...         self.head = nn.Linear(8, 2)
...     def forward(self, x):
...         out, _ = self.rnn(x)
...         return self.head(out[-1])  # logits
>>> rclf = RollingClassifier(
...     module=TinyRNN(n_features),
...     loss_fn='cross_entropy',
...     optimizer_fn='sgd',
...     lr=5e-3,
...     window_size=8,
...     is_class_incremental=True
... )
>>> acc = metrics.Accuracy()
>>> for i, (x, y) in enumerate(datasets.Phishing().take(200)):
...     if i > 0:
...         y_pred = rclf.predict_one(x)
...         acc.update(y, y_pred)
...     rclf.learn_one(x, y)
>>> print(f"Accuracy: {acc.get():.4f}")
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

Batch update: extend window with rows of X and perform a step.

learn_one

Learn from a single (x, y) updating the rolling window.

load

Load a previously saved estimator.

predict_proba_many

Return probability DataFrame for multiple samples with rolling context.

predict_proba_one

Return class probability mapping for one sample using rolling context.

save

Persist the estimator (architecture, weights, optimiser & runtime state).

Source code in deep_river/classification/rolling_classifier.py
def __init__(
    self,
    module: torch.nn.Module,
    loss_fn: Union[str, Callable] = "binary_cross_entropy_with_logits",
    optimizer_fn: Union[str, Type[optim.Optimizer]] = "sgd",
    lr: float = 1e-3,
    output_is_logit: bool = True,
    is_class_incremental: bool = False,
    is_feature_incremental: bool = False,
    device: str = "cpu",
    seed: int = 42,
    window_size: int = 10,
    append_predict: bool = False,
    gradient_clip_value: float | None = None,
    **kwargs,
):
    # Use RollingDeepEstimator init to build window + base functionality
    RollingDeepEstimator.__init__(
        self,
        module=module,
        loss_fn=loss_fn,
        optimizer_fn=optimizer_fn,
        lr=lr,
        device=device,
        seed=seed,
        window_size=window_size,
        append_predict=append_predict,
        is_feature_incremental=is_feature_incremental,
        gradient_clip_value=gradient_clip_value,
        **kwargs,
    )
    # Classification specific attributes (mirror Classifier.__init__)
    self.output_is_logit = output_is_logit
    self.is_class_incremental = is_class_incremental
    self.observed_classes: SortedSet = SortedSet()

clone

clone(
    new_params=None,
    include_attributes: bool = False,
    copy_weights: bool = False,
)

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
def clone(
    self,
    new_params=None,
    include_attributes: bool = False,
    copy_weights: bool = False,
):
    """Return a fresh estimator instance with (optionally) copied state.

    Parameters
    ----------
    new_params : dict | None
        Parameter overrides for the cloned instance.
    include_attributes : bool, default=False
        If True, runtime state (observed features, buffers) is also copied.
    copy_weights : bool, default=False
        If True, model weights are copied (otherwise the module is re‑initialised).
    """
    new_params = new_params or {}
    copy_weights = new_params.pop("copy_weights", copy_weights)

    params = {**self._get_all_init_params(), **new_params}

    if "module" not in new_params:
        params["module"] = self._rebuild_module()

    new_est = self.__class__(**self._filter_kwargs(self.__class__.__init__, params))

    if copy_weights and hasattr(self.module, "state_dict"):
        new_est.module.load_state_dict(self.module.state_dict())

    if include_attributes:
        new_est._restore_runtime_state(self._get_runtime_state())

    return new_est

draw

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
def draw(self):  # type: ignore[override]
    """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.
    """
    try:  # pragma: no cover
        from torchviz import make_dot  # type: ignore
    except Exception as err:  # noqa: BLE001
        raise ImportError(
            "graphviz and torchviz must be installed to draw the model."
        ) from err

    first_parameter = next(self.module.parameters())
    input_shape = first_parameter.size()
    y_pred = self.module(torch.rand(input_shape))
    return make_dot(y_pred.mean(), params=dict(self.module.named_parameters()))

learn_many

learn_many(X: DataFrame, y: Series) -> None

Batch update: extend window with rows of X and perform a step.

Source code in deep_river/classification/rolling_classifier.py
def learn_many(self, X: pd.DataFrame, y: pd.Series) -> None:
    """Batch update: extend window with rows of X and perform a step."""
    self._update_observed_targets(y)
    self._update_observed_features(X)
    X = X[list(self.observed_features)]
    self._x_window.extend(X.values.tolist())
    X_t = self._deque2rolling_tensor(self._x_window)
    self._learn(x=X_t, y=y)

learn_one

learn_one(x: dict, y: ClfTarget, **kwargs) -> None

Learn from a single (x, y) updating the rolling window.

Source code in deep_river/classification/rolling_classifier.py
def learn_one(self, x: dict, y: ClfTarget, **kwargs) -> None:
    """Learn from a single (x, y) updating the rolling window."""
    self._update_observed_features(x)
    self._update_observed_targets(y)
    self._x_window.append([x.get(feature, 0) for feature in self.observed_features])
    x_t = self._deque2rolling_tensor(self._x_window)
    self._learn(x=x_t, y=y)

load classmethod

load(filepath: Union[str, Path])

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
@classmethod
def load(cls, filepath: Union[str, Path]):
    """Load a previously saved estimator.

    The method reconstructs the estimator class, its wrapped module, optimiser
    state and runtime information (feature names, buffers, etc.).
    """
    with open(filepath, "rb") as f:
        state = pickle.load(f)

    estimator_cls = cls._import_from_path(state["estimator_class"])
    init_params = state["init_params"]

    # Rebuild module if needed
    if "module" in init_params and isinstance(init_params["module"], dict):
        module_info = init_params.pop("module")
        module_cls = cls._import_from_path(module_info["class"])
        module = module_cls(
            **cls._filter_kwargs(module_cls.__init__, module_info["kwargs"])
        )
        if state.get("model_state_dict"):
            module.load_state_dict(state["model_state_dict"])
        init_params["module"] = module

    estimator = estimator_cls(
        **cls._filter_kwargs(estimator_cls.__init__, init_params)
    )

    if state.get("optimizer_state_dict") and hasattr(estimator, "optimizer"):
        try:
            estimator.optimizer.load_state_dict(
                state["optimizer_state_dict"]  # type: ignore[arg-type]
            )
        except Exception:  # noqa: E722
            pass

    estimator._restore_runtime_state(state.get("runtime_state", {}))
    return estimator

predict_proba_many

predict_proba_many(X: DataFrame) -> DataFrame

Return probability DataFrame for multiple samples with rolling context.

Source code in deep_river/classification/rolling_classifier.py
def predict_proba_many(self, X: pd.DataFrame) -> pd.DataFrame:
    """Return probability DataFrame for multiple samples with rolling context."""
    self._update_observed_features(X)
    X = X[list(self.observed_features)]
    x_win = self._x_window.copy()
    x_win.extend(X.values.tolist())
    if self.append_predict:
        self._x_window = x_win
    self.module.eval()
    with torch.inference_mode():
        x_t = self._deque2rolling_tensor(x_win)
        probas = self.module(x_t).detach().tolist()
    return pd.DataFrame(probas)

predict_proba_one

predict_proba_one(x: dict) -> Dict[ClfTarget, float]

Return class probability mapping for one sample using rolling context.

Source code in deep_river/classification/rolling_classifier.py
def predict_proba_one(self, x: dict) -> Dict[ClfTarget, float]:
    """Return class probability mapping for one sample using rolling context."""
    self._update_observed_features(x)
    x_win = self._x_window.copy()
    x_win.append([x.get(feature, 0) for feature in self.observed_features])
    if self.append_predict:
        self._x_window = x_win
    self.module.eval()
    with torch.inference_mode():
        x_t = self._deque2rolling_tensor(x_win)
        y_pred = self.module(x_t)
        proba = output2proba(y_pred, self.observed_classes, self.output_is_logit)
    return cast(Dict[ClfTarget, float], proba[0])

save

save(filepath: Union[str, Path]) -> None

Persist the estimator (architecture, weights, optimiser & runtime state).

Parameters:

Name Type Description Default
filepath str | Path

Destination file. Parent directories are created automatically.

required
Source code in deep_river/base.py
def save(self, filepath: Union[str, Path]) -> None:
    """Persist the estimator (architecture, weights, optimiser & runtime state).

    Parameters
    ----------
    filepath : str | Path
        Destination file. Parent directories are created automatically.
    """
    filepath = Path(filepath)
    filepath.parent.mkdir(parents=True, exist_ok=True)

    state = {
        "estimator_class": f"{type(self).__module__}.{type(self).__name__}",
        "init_params": self._get_all_init_params(),
        "model_state_dict": getattr(self.module, "state_dict", lambda: {})(),
        "optimizer_state_dict": getattr(self.optimizer, "state_dict", lambda: {})(),
        "runtime_state": self._get_runtime_state(),
    }

    with open(filepath, "wb") as f:
        pickle.dump(state, f)