multioutput
¶
Classes:
Name | Description |
---|---|
MultiTargetRegressor |
Incremental multi-target regression wrapper for PyTorch modules. |
MultiTargetRegressor
¶
MultiTargetRegressor(
module: Module,
loss_fn: Union[str, Callable] = "mse",
optimizer_fn: Union[str, Callable] = "sgd",
is_feature_incremental: bool = False,
is_target_incremental: bool = False,
lr: float = 0.001,
device: str = "cpu",
seed: int = 42,
**kwargs
)
Bases: MultiTargetRegressor
, DeepEstimator
Incremental multi-target regression wrapper for PyTorch modules.
This estimator adapts a torch.nn.Module
to the :mod:river
streaming API
for multi‑target (a.k.a. multi‑output) regression. It optionally supports
feature‑incremental learning (dynamic growth of the input layer when new
feature names appear) as provided by :class:deep_river.base.DeepEstimator
and
additionally (optionally) target‑incremental learning: if new target names
appear during the stream, the output layer can be expanded on‑the‑fly so the
model natively handles the enlarged target vector.
Targets are tracked via an ordered :class:~sortedcontainers.SortedSet
to
guarantee deterministic ordering between training and prediction. Incoming
target dictionaries / frames are converted into dense tensors with columns
arranged according to the observed target name order. Missing targets (when
the model has been expanded but a prior sample omits some target) are imputed
with 0.0
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module
|
Module
|
PyTorch module producing an output tensor of shape |
required |
loss_fn
|
str | Callable
|
Loss identifier or custom callable passed through :func: |
'mse'
|
optimizer_fn
|
str | Callable
|
Optimizer identifier (e.g. |
'sgd'
|
is_feature_incremental
|
bool
|
If True, unseen feature names trigger expansion of the first trainable
layer (see :class: |
False
|
is_target_incremental
|
bool
|
If True, unseen target names trigger expansion of the last trainable layer. Expansion preserves existing weights and initialises new units with small random values. |
False
|
lr
|
float
|
Learning rate. |
1e-3
|
device
|
str
|
Torch device (e.g. |
'cpu'
|
seed
|
int
|
Random seed for reproducibility. |
42
|
**kwargs
|
Extra arguments stored for persistence / cloning. |
{}
|
Examples:
>>> import torch
>>> from torch import nn
>>> from deep_river.regression.multioutput import MultiTargetRegressor
>>> class TinyMultiNet(nn.Module):
... def __init__(self, n_features, n_outputs):
... super().__init__()
... self.net = nn.Sequential(
... nn.Linear(n_features, 8),
... nn.ReLU(),
... nn.Linear(8, n_outputs)
... )
... def forward(self, x):
... return self.net(x)
>>> model = MultiTargetRegressor(
... module=TinyMultiNet(3, 2),
... loss_fn='mse',
... optimizer_fn='sgd',
... is_feature_incremental=True,
... is_target_incremental=True,
... )
>>> x = {'a': 1.0, 'b': 2.0, 'c': 3.0}
>>> y = {'y1': 10.0, 'y2': 20.0}
>>> _ = model.learn_one(x, y)
>>> model.predict_one(x)
{'y1': ..., 'y2': ...}
Notes
- The module's last trainable leaf layer is treated as output layer for
- If
is_target_incremental
is disabled, the number of outputs is fixed and encountering a new target name will only register it internally (the tensor conversion will still allocate a slot, but the model's output layer size will not change, possibly causing a mismatch). Therefore, enabling target incrementality is recommended for truly open‑world streams.
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 multi-target instances. |
learn_one |
Learn from a single multi-target instance. |
load |
Load a previously saved estimator. |
predict_many |
Predict target values for multiple instances. |
predict_one |
Predict a dictionary of target values for a single instance. |
save |
Persist the estimator (architecture, weights, optimiser & runtime state). |
Source code in deep_river/regression/multioutput.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_many(
X: DataFrame,
y: Union[
DataFrame, Series, Mapping[str, Sequence[RegTarget]]
],
) -> None
Learn from a batch of multi-target instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
Feature matrix (rows are samples, columns are feature names). |
required |
y
|
DataFrame | Series | mapping
|
Target matrix. Preferred is a DataFrame with one column per target.
A Series is interpreted as one target. A mapping of |
required |
Source code in deep_river/regression/multioutput.py
learn_one
¶
Learn from a single multi-target instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
dict[str, float]
|
Feature mapping. |
required |
y
|
dict[str, float]
|
Mapping of target name -> target value. |
required |
**kwargs
|
Ignored (kept for signature compatibility / future hooks). |
{}
|
Source code in deep_river/regression/multioutput.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_many
¶
Predict target values for multiple instances.
Returns:
Type | Description |
---|---|
DataFrame
|
DataFrame whose columns follow the ordering of |
Source code in deep_river/regression/multioutput.py
predict_one
¶
Predict a dictionary of target values for a single instance.
Source code in deep_river/regression/multioutput.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 |