Implementation:Online ml River Facto FwFM
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Factorization_Machines, Click_Through_Rate_Prediction |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Field-weighted Factorization Machines (FwFM) extend standard FM by learning interaction strength weights for each pair of feature fields, allowing the model to emphasize or de-emphasize specific field interactions.
Description
FwFM augments Factorization Machines with learnable field-pair weights that modulate the strength of interactions between different fields. Each field pair has an associated weight that is learned alongside the latent factors and feature weights. The model computes interactions as the dot product of latent vectors multiplied by the learned field-pair weight. This allows FwFM to automatically discover which field interactions are most important for the prediction task. Field names are inferred from feature names by splitting at the first underscore. The model uses three optimizers: one for feature weights, one for latent factors, and one for interaction weights.
Usage
Use FwFM for CTR prediction and display advertising where different field combinations have varying importance. It is particularly valuable when you want the model to automatically learn which field interactions matter most rather than manually engineering field combinations. FwFM works best with datasets that have clear field structure (user features, item features, context features, etc.) where some field pairs are naturally more predictive than others.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/facto/fwfm.py
Signature
class FwFMRegressor(
n_factors=10,
weight_optimizer: optim.base.Optimizer | None = None,
latent_optimizer: optim.base.Optimizer | None = None,
int_weight_optimizer: optim.base.Optimizer | None = None,
loss: optim.losses.RegressionLoss | None = None,
sample_normalization=False,
l1_weight=0.0,
l2_weight=0.0,
l1_latent=0.0,
l2_latent=0.0,
intercept=0.0,
intercept_lr: optim.base.Scheduler | float = 0.01,
weight_initializer: optim.initializers.Initializer | None = None,
latent_initializer: optim.initializers.Initializer | None = None,
clip_gradient=1e12,
seed: int | None = None,
)
class FwFMClassifier(
n_factors=10,
weight_optimizer: optim.base.Optimizer | None = None,
latent_optimizer: optim.base.Optimizer | None = None,
int_weight_optimizer: optim.base.Optimizer | None = None,
loss: optim.losses.BinaryLoss | None = None,
sample_normalization=False,
l1_weight=0.0,
l2_weight=0.0,
l1_latent=0.0,
l2_latent=0.0,
intercept=0.0,
intercept_lr: optim.base.Scheduler | float = 0.01,
weight_initializer: optim.initializers.Initializer | None = None,
latent_initializer: optim.initializers.Initializer | None = None,
clip_gradient=1e12,
seed: int | None = None,
)
Import
from river import facto
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| x | dict | Feature dictionary with field prefixes in feature names |
| y | float/bool | Target value for regression or classification |
| Method | Return Type | Description |
|---|---|---|
| predict_one(x) | float/bool | Predicted value or class label |
| predict_proba_one(x) | dict | Class probabilities (classifier only) |
| learn_one(x, y) | None | Updates model with sample |
Usage Examples
from river import facto
dataset = (
({'user': 'Alice', 'item': 'Superman'}, 8),
({'user': 'Alice', 'item': 'Terminator'}, 9),
({'user': 'Alice', 'item': 'Star Wars'}, 8),
({'user': 'Alice', 'item': 'Notting Hill'}, 2),
({'user': 'Bob', 'item': 'Superman'}, 8),
)
model = facto.FwFMRegressor(
n_factors=10,
intercept=5,
seed=42,
)
for x, y in dataset:
model.learn_one(x, y)
prediction = model.predict_one({'Bob': 1, 'Harry Potter': 1})
print(f"Prediction: {prediction}") # 5.236501