Implementation:Online ml River Optim Adam
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Optimization |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Adam (Adaptive Moment Estimation) is a popular adaptive learning rate optimizer that combines momentum and RMSProp, maintaining both first and second moment estimates.
Description
Adam computes adaptive learning rates for each parameter by maintaining exponentially decaying averages of both past gradients (first moment) and past squared gradients (second moment). These moment estimates are bias-corrected to account for their initialization at zero. The algorithm updates parameters using the bias-corrected first moment divided by the square root of the bias-corrected second moment, plus a small constant for numerical stability. Adam combines the benefits of AdaGrad (adapting learning rates per parameter) and RMSProp (using exponential moving averages), resulting in an optimizer that works well across a wide range of problems with minimal hyperparameter tuning. The implementation supports both dictionary-based weights and numpy vectors.
Usage
Import from river.optim and use as an optimizer in any River model. Adam is a versatile default choice for most online learning problems.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/optim/adam.py
Signature
class Adam(optim.base.Optimizer):
def __init__(self, lr=0.1, beta_1=0.9, beta_2=0.999, eps=1e-8) -> None:
...
Import
from river import optim
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| lr | float | No (default=0.1) | Learning rate |
| beta_1 | float | No (default=0.9) | Exponential decay rate for first moment estimates |
| beta_2 | float | No (default=0.999) | Exponential decay rate for second moment estimates |
| eps | float | No (default=1e-8) | Small constant for numerical stability |
Outputs
| Name | Type | Description |
|---|---|---|
| optimizer | Adam | Configured optimizer instance ready for model training |
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
# Create Adam optimizer with default parameters
optimizer = optim.Adam()
# Use with a linear model
dataset = datasets.Phishing()
model = (
preprocessing.StandardScaler() |
linear_model.LogisticRegression(optimizer)
)
metric = metrics.F1()
# Evaluate
score = evaluate.progressive_val_score(dataset, model, metric)
print(score) # F1: 86.52%
# Custom parameters
optimizer = optim.Adam(
lr=0.01,
beta_1=0.95,
beta_2=0.999,
eps=1e-7
)
model = linear_model.LogisticRegression(optimizer)
# Works well as a general-purpose optimizer
from river import linear_model
# For regression
regressor = linear_model.LinearRegression(optimizer=optim.Adam())
# For classification
classifier = linear_model.LogisticRegression(optimizer=optim.Adam())