Implementation:Online ml River Optim Initializers
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Optimization |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
A collection of weight initialization strategies for online machine learning models including constant, zeros, and normal distribution initializers.
Description
The initializers module provides strategies for initializing model weights before training begins. Proper initialization is crucial for effective training and convergence. The module includes three main initializers: Constant (returns a fixed value for all weights), Zeros (specialized constant initializer that returns zero), and Normal (samples from a normal distribution with specified mean and standard deviation). All initializers support both scalar initialization (shape=1) and vector initialization (shape>1), returning either a single float or a numpy array depending on the requested shape. The Normal initializer supports setting a random seed for reproducibility.
Usage
Import from river.optim.initializers to initialize weights in models or custom implementations. Commonly used in neural networks and linear models.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/optim/initializers.py
Signature
class Constant(Initializer):
def __init__(self, value: float):
...
def __call__(self, shape=1):
...
class Zeros(Constant):
def __init__(self):
...
class Normal(Initializer):
def __init__(self, mu=0.0, sigma=1.0, seed: int | None = None):
...
def __call__(self, shape=1):
...
Import
from river import optim
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| value | float | Yes (Constant) | Constant value to initialize all weights |
| mu | float | No (default=0.0) | Mean of normal distribution |
| sigma | float | No (default=1.0) | Standard deviation of normal distribution |
| seed | int or None | No (default=None) | Random seed for reproducibility |
| shape | int | No (default=1) | Number of weights to initialize (passed to __call__) |
Outputs
| Name | Type | Description |
|---|---|---|
| weights | float or ndarray | Single value if shape=1, otherwise numpy array of specified shape |
Usage Examples
from river import optim
# Constant initializer
init = optim.initializers.Constant(value=3.14)
print(init(shape=1)) # 3.14
print(init(shape=3)) # array([3.14, 3.14, 3.14])
# Zeros initializer (special case of Constant)
init = optim.initializers.Zeros()
print(init(shape=1)) # 0.0
print(init(shape=4)) # array([0., 0., 0., 0.])
# Normal distribution initializer
init = optim.initializers.Normal(mu=0, sigma=1, seed=42)
print(init(shape=1)) # Single random value
print(init(shape=5)) # Array of 5 random values
# Xavier/He-style initialization
init = optim.initializers.Normal(mu=0, sigma=0.01, seed=123)
weights = init(shape=100)
# Use in custom models
class SimpleLinear:
def __init__(self, n_features):
init = optim.initializers.Normal(mu=0, sigma=0.1, seed=42)
self.weights = init(shape=n_features)
# Common initialization strategies
zero_init = optim.initializers.Zeros()
small_random = optim.initializers.Normal(mu=0, sigma=0.01)
large_constant = optim.initializers.Constant(value=1.0)