Implementation:Online ml River Linear Model SoftmaxRegression
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Classification, Multiclass_Learning |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Softmax regression (multinomial logistic regression) is a generalization of binary logistic regression that handles multiple classes with calibrated probabilities.
Description
This implementation maintains separate weight vectors for each class in a nested defaultdict structure. During training, it computes softmax probabilities across all classes and updates each class's weights using cross-entropy loss gradients. The key advantage over one-vs-all approaches is that probabilities are properly calibrated across classes through the softmax normalization. Each class has its own optimizer instance for flexible per-class learning dynamics. Optional L2 regularization helps prevent overfitting.
Usage
Use Softmax regression when you need well-calibrated probability estimates for multiclass problems. It's particularly effective when you want to avoid the independence assumptions of one-vs-all approaches and need probabilities that sum to 1.0. The model is more robust to outliers than alternatives and scales well to many classes.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/linear_model/softmax.py
Signature
class SoftmaxRegression(base.Classifier):
def __init__(
self,
optimizer: optim.base.Optimizer | None = None,
loss: optim.losses.MultiClassLoss | None = None,
l2=0,
):
if optimizer is None:
optimizer = optim.SGD(0.01)
self.optimizer = optimizer
self.optimizers = collections.defaultdict(new_optimizer)
self.loss = optim.losses.CrossEntropy() if loss is None else loss
self.l2 = l2
self.weights = collections.defaultdict(
functools.partial(collections.defaultdict, float)
)
Import
from river import linear_model
I/O Contract
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| optimizer | Optimizer or None | SGD(0.01) | Sequential optimizer for weight updates |
| loss | MultiClassLoss or None | CrossEntropy() | Loss function to optimize |
| l2 | float | 0 | L2 regularization amount |
Attributes
| Attribute | Type | Description |
|---|---|---|
| weights | defaultdict[defaultdict[float]] | Weight vectors per class |
| optimizers | defaultdict | Optimizer instances per class |
Input/Output
| Method | Input | Output |
|---|---|---|
| learn_one | x: dict, y: Any | None |
| predict_proba_one | x: dict | dict[Any, float] |
| predict_one | x: dict | Any |
Usage Examples
from river import datasets
from river import evaluate
from river import linear_model
from river import metrics
from river import optim
from river import preprocessing
dataset = datasets.ImageSegments()
model = preprocessing.StandardScaler()
model |= linear_model.SoftmaxRegression()
metric = metrics.MacroF1()
evaluate.progressive_val_score(dataset, model, metric)
# MacroF1: 81.88%