Sequence Models (RNN / Transformer) for Recommendation Service widely used at OTT and e-Commerce | ||
| ||
| Sequence models treat a user’s history as an ordered sequence of interactions and predict the next item the user will engage with. Unlike matrix factorization or NCF — which treat history as an unordered bag — sequence models exploit order, recency, and short-term intent. The shift from “shopping for a tent last month” to “currently researching laptops” is exactly the kind of signal they capture. Two architectural lineages dominate: recurrent neural network (RNN) models (GRU4Rec) and self-attention / Transformer models (SASRec, BERT4Rec). Transformers have largely become the default for their parallelism and ability to model long-range dependencies.
How It Works (The Math)Let a user’s interaction sequence be Recurrent (GRU4Rec)Each item embedding is fed step by step into a GRU (Gated Recurrent Unit), which maintains a hidden state summarizing history:
Trained session-by-session, originally with a pairwise ranking loss (BPR — Bayesian Personalized Ranking — / TOP1). Self-Attention (SASRec)Items are embedded and given positional encodings, then passed through stacked self-attention blocks. Attention weights how much each past item matters for the current prediction:
SASRec is causal (left-to-right masking): position Bidirectional (BERT4Rec)Uses a masked-item objective like BERT: random items in the sequence are masked and predicted from both left and right context, giving richer representations at the cost of a different (non-autoregressive) training scheme. Typical lossNext-item prediction as classification over the catalog (often with sampled softmax for large catalogs) or a pairwise ranking loss.
Requirements
Simplified Architecture
Reference Architecture
Reference Architecture (with Technology Stack)The same flow on concrete tooling — Flink for sessionization, Redis for recent-history state, a Transformer (SASRec) trained on GPU, FAISS for retrieval, and near-real-time retraining off the Kafka stream.
Signals & FeaturesThe essential input is the ordered item sequence. Models are routinely enriched with per-step features: action type (click vs. purchase vs. skip), dwell time, time gaps between events (to encode recency and session boundaries), item category, and contextual features (device, time of day). Encoding the time delta explicitly helps the model distinguish a rapid in-session burst from interactions weeks apart.
Training & Serving ConsiderationsCausal models (SASRec) train efficiently because every position contributes a next-item prediction in parallel. The output layer over a huge catalog is the cost center, mitigated by sampled softmax. At serving time the user’s latest events must be incorporated — this is why sequence recommenders pair naturally with near-real-time feature pipelines and online/continuous training (the design philosophy behind systems like TikTok’s Monolith). The sequence is encoded into a query vector that hits an ANN (Approximate Nearest Neighbor) index over item embeddings.
EvaluationNext-item prediction is evaluated with HR@k (Hit Ratio at k), NDCG@k (Normalized Discounted Cumulative Gain at k), and MRR (Mean Reciprocal Rank) under a leave-one-out (predict the last item) or temporal-split protocol. Because user intent shifts, temporal evaluation is mandatory — random splits leak future information and badly overstate performance. Online, watch session depth and immediate next-action engagement.
Strengths & WeaknessesStrengths: captures order, recency, and short-term intent that static models ignore; excels at session-based and “next-item” recommendation; Transformers model long-range dependencies and parallelize well; naturally suited to fast-moving feeds. Weaknesses: compute- and data-hungry; sensitive to sequence length and noise; cold-start for brand-new items/users persists; bidirectional variants add training complexity; real-time serving of fresh sequences raises infrastructure demands.
When to Use ItSequence models shine wherever order and immediacy matter: short-video and music feeds, e-commerce sessions, news, and any context with strong intra-session intent. They are the backbone of modern “next item” and feed-ranking systems and are typically combined with two-tower retrieval and feature-rich ranking layers in a full production stack. | ||
| | ||
| |