TL;DR
A sequential recommender looks at the order of a user's recent actions and predicts what they will pick next. A plain recommender treats a user as a bag of past items; this one treats the history as a sequence, so a song played after a run of rock tracks is read differently from the same song played after a run of lullabies. The model scores every item in the catalogue for "is this the next one?" and shows the top few.
Builds on: Recommender System, Embedding Layer, Recurrent Neural Network, Attention Mechanism, Cross-Entropy Loss · Leads to: BERT4Rec: Sequential Recommendation with Bidirectional Encoder Representations
Where it's used
Next track and next video queues. A listener who has just played three ambient tracks is probably not about to hear a death-metal track next, and a viewer who has finished a cooking series may want the next episode, not the most popular video of the year. The history that matters is the last few plays or views, and the order is part of that signal.
Shopping sessions. On a shopping site, a visitor who has clicked on running shoes and then a heart-rate watch is in a different state from one who clicked on the same two items in the opposite order. Session-based Recommendations with Recurrent Neural Networks was built for this case: it models each anonymous browsing session as a sequence of clicks, with no login history. On its RSC15 click data, the strongest item-to-item baseline reached Recall@20 of 0.5065, and the best GRU variant reached 0.6322, about 25% higher. Recall@20 is the share of test cases in which the true next item appears in the top 20 suggestions.
Long movie histories. Self-Attentive Sequential Recommendation models a user's whole rating history with self-attention. On MovieLens-1M it reports NDCG@10 of 0.5905 and Hit@10 of 0.8245 for the default setting. NDCG@10 gives more credit the higher the true item sits in the top 10, and Hit@10 is the share of cases where it appears there at all. Its attention weights show that on sparse data it attends to the latest items, and on denser data it looks further back.
Industrial feeds. Actions Speak Louder than Words: Generative Recommenders (HSTU) treats a user's full stream of actions as one sequence and reports a 12.4% online lift on a large platform. The point of the example is scale: the same idea of ordering actions is applied to histories far longer than a shopping session.
How it works
Here is one tiny case, followed from start to finish. A shop has five items: shoes (A), socks (B), sweater (C), scarf (D) and hat (E). Each item gets a two-number vector, its embedding, chosen so that similar items point the same way. The vectors are A = (1, 0), B = (0, 1), C = (1, 1), D = (0.6, 2) and E = (0, −1). A shopper's session so far is shoes, then sweater, and we want the next item.
flowchart LR H[Recent items in order] --> E[Look up each item's embedding] E --> S[Combine into one state vector] S --> SC[Score every item against the state] SC --> P[Softmax: scores become probabilities] P --> T[Show the top few items]
-
Write the session as item IDs in order. The session is A then C. The order is the point: the same two items in the reverse order would be a different session. Check: the list has two entries and A comes first.
-
Look up the embeddings. A is (1, 0) and C is (1, 1). Check: each item is one row from the embedding table, so the table has five rows, one per item.
-
Combine them into a state. The simplest state is the average of the recent embeddings: ((1, 0) + (1, 1)) / 2 = (1, 0.5). A real model learns a better combination, such as a recurrent network or attention, but the job is the same: turn a sequence into one vector that summarises it. Check: the state has the same size as one embedding, two numbers.
-
Score every item. Take the dot product of the state with each item's embedding. A: 1 × 1 + 0.5 × 0 = 1.0. B: 1 × 0 + 0.5 × 1 = 0.5. C: 1 × 1 + 0.5 × 1 = 1.5. D: 1 × 0.6 + 0.5 × 2 = 1.6. E: 1 × 0 + 0.5 × (−1) = −0.5. Check: a higher score means the item points more in the state's direction.
-
Turn scores into probabilities. The Softmax takes exponentials and divides by their sum, so the five numbers become positive and add up to 1. Rounded to two decimals: A 0.19, B 0.11, C 0.31, D 0.34, E 0.04. Check: the probabilities sum to 1 (0.19 + 0.11 + 0.31 + 0.34 + 0.04 = 0.99, the difference is rounding).
-
Rank and show the top few. The highest is the scarf (D), then the sweater (C), then the shoes (A). Those three go on the shelf. Check: the ranking follows the probabilities, not the order of the catalogue.
-
Learn from the real next item. Suppose the shopper actually clicked the scarf next. The model's probability for that item was 0.34 (0.344 before rounding), and the training loss is minus its natural log, about 1.07. Training nudges the embeddings and the state-building so that this probability goes up. Check: a probability of 0.99 would give a loss of about 0.01, so the loss goes down as the model gets more confident in the right item.
The math
Let be the embedding of item , and let be the state after the first items of a session . The state is produced by a sequence encoder :
In Step 3 the encoder was an average. An RNN, a GRU or a self-attention block is a different choice of that also respects the order.
Step 4 in symbols: scores
The score of item is the dot product of the state with that item's embedding:
Here is a real number, not a probability. Larger means more likely to be next.
Step 5 in symbols: softmax
The probability that item comes next is the softmax over all items in the catalogue:
Each probability is between 0 and 1, and they sum to 1. The sum runs over the whole catalogue, which is why a large catalogue makes the exact softmax expensive and motivates the sampling trick in Negative Sampling.
Step 7 in symbols: the loss
If the true next item is , the training loss is the negative log of its probability, which is the Cross-Entropy Loss for a single example:
For the scarf example, and . The loss is zero only when the model gives the true item probability 1.
Worked examples
- The session is just socks (B), so the state is the socks embedding, .
- Scores are dot products with . Shoes A: . Socks B: . Sweater C: . Scarf D: . Hat E: .
- Exponentials: , , , , . Their sum is about 14.19.
- Divide each by 14.19: shoes 0.07, socks 0.19, sweater 0.19, scarf 0.52, hat 0.03.
- Interpretation: one click already shifts the scarf to the front, because the scarf's embedding points furthest along the socks direction. A single item carries less information than a longer session, so the probabilities are spread out.
- For each test session, find the rank the model gave the true next item. Four sessions have ranks 1, 3, 2 and 7.
- Hit@K counts a case as a hit when the true item is in the top K. Hit@2 counts ranks 1 and 2: two hits.
- Divide by the number of sessions: Hit@2 = 2 / 4 = 0.50.
- Hit@3 counts ranks 1, 2 and 3: three hits, so Hit@3 = 3 / 4 = 0.75.
- Interpretation: the top 3 contains the true next item for three quarters of the sessions, while the top 2 contains it for half. Each extra slot in the list is worth something, and the choice of K should match how many items the interface shows at once.
The SASRec paper on MovieLens-1M reports NDCG@10 of 0.587 when it uses the last 200 items of each history, and 0.596 with the last 500. Training took 341 seconds at length 200 and 1406 seconds at length 500.
- The accuracy gain from 200 to 500 is 0.596 − 0.587 = 0.009 NDCG points.
- The time cost is 1406 − 341 = 1065 extra seconds.
- Compare the ratio of the two training times: 1406 / 341 is about 4.1.
- Each step in a long history costs the model more, because self-attention compares every item with every other item in the window.
- Try it: a music service has users with hundreds of plays and a budget that allows training to take about 3 times as long as the length-200 run. Would length 500 fit, and what does it buy? Answer: it does not fit: 4.1 times the length-200 time is more than the 3 times the budget allows. Length 200 is the right choice, with a loss of about 0.009 NDCG@10 against the best result on this data.
Common mistakes and tradeoffs
It is tempting to think that a sequential recommender is just an ordinary recommender with timestamps added. In fact the order is the signal. The same two items in a different order predict different next items, and a model that ignores order cannot see the difference. An ordinary Matrix Factorization model learns one vector per user and is blind to the order of their actions.
It is tempting to think that an item a user never clicked is one they dislike. In fact the data is Implicit Feedback: clicks and views are recorded when they happen, and a missing click only means the user did not see or choose the item in that session. Training therefore treats unseen items as negatives by sampling, as in Negative Sampling, and accepts that some of those negatives are items the user would have liked.
It is tempting to think a longer history always helps. In fact the SASRec gains flatten out at a few hundred items, while training time keeps rising, as Example 3 shows. A longer window is worth it only when the extra items carry information the short window misses.
Tradeoffs in choosing a model:
| Approach | Uses order | Cost and strength |
|---|---|---|
| Matrix factorization | No | Cheap; strong on stable tastes, blind to session context |
| Markov chain on the last item | Only the last item | Simple; cannot use longer patterns |
| Recurrent network (Session-based Recommendations with Recurrent Neural Networks) | Yes, in sequence | Good on short sessions; slow to train on long ones |
| Self-attention (Self-Attentive Sequential Recommendation) | Yes, all at once | Strong on long histories; cost grows with length |
Use a sequential recommender when recent order matters, as in sessions, feeds and queues. Do not use it when the catalogue is small and changes little, when users have very few actions, or when the question is about long-term taste rather than the next click.
History
Before sequence models, shops used item-to-item rules: for each item, find the items most often bought or viewed with it. In 2003 Amazon described this item-to-item collaborative filtering as a way to scale recommendations to a large catalogue. It is simple and fast, but it looks at one item at a time.
Markov-chain models that use the last basket or item came next, and they added order in a limited way. Recurrent networks then made it possible to use a whole session. Session-based Recommendations with Recurrent Neural Networks, published on arXiv in 2015, showed that a GRU trained on click sessions could beat item-to-item baselines. Self-attention followed: Self-Attentive Sequential Recommendation in 2018 used attention blocks over the history and reported gains on four datasets. BERT4Rec: Sequential Recommendation with Bidirectional Encoder Representations in 2019 borrowed the masked-prediction idea from language models, and Actions Speak Louder than Words: Generative Recommenders (HSTU) in 2024 treated a user's actions as one long sequence at industrial scale. Each step kept the same core idea and changed how the sequence is read.
Recap
- A sequential recommender treats a user's recent actions as an ordered sequence, not as a set.
- It turns the sequence into one state vector, scores every item against that state with a dot product, and turns the scores into probabilities with softmax.
- Training minimises the cross-entropy of the true next item, and sampled negatives keep the cost manageable for large catalogues.
- The main choice is the sequence encoder: a recurrent network, self-attention, or a masked model, each with different costs on long histories.
Read next: Session-based Recommendations with Recurrent Neural Networks for the recurrent approach on click sessions, Self-Attentive Sequential Recommendation for self-attention on long histories, and BERT4Rec: Sequential Recommendation with Bidirectional Encoder Representations for the masked version.
Check yourself
QuestionIn one sentence, what does a sequential recommender use that an ordinary matrix-factorization recommender does not?Show answer
It uses the order of the user's recent actions. Matrix factorization gives each user one vector from their set of past items, so reordering the same items gives the same prediction. A sequential recommender builds its state from the sequence, so the order changes the prediction.
QuestionA state vector is (1, 2) and an item embedding is (3, −1). What is the score, and is that score a probability?Show answer
The score is the dot product: 1 × 3 + 2 × (−1) = 3 − 2 = 1. It is not a probability. Scores can be negative or larger than 1. The softmax over all items turns scores into probabilities that sum to 1.
QuestionWhy does training use sampled negatives rather than the full softmax over the catalogue, and what does this trade away?Show answer
The full softmax sums exponentials over every item, which is expensive when the catalogue has millions of items, and it must be repeated for each training example. Sampled negatives compare the true item against a few random items, so each update is cheap. The trade-off is that a sampled negative may be an item the user would have liked, so the model's estimates are noisier than they would be with the full sum.
QuestionAn online shop has 300 items and users make about 5 clicks per visit. Would you start with a sequential recommender, and what would you measure first?Show answer
Probably not as a first choice. With 300 items and short sessions there is little order to learn, and a simple item-to-item or matrix-factorization baseline may be as good and much cheaper. Measure Hit@K on held-out sessions for the simple baseline first. If the sequential model beats it by a clear margin at the list length the shop shows, the extra cost is justified.
Sources
- Hidasi et al., Session-based Recommendations with Recurrent Neural Networks, arXiv:1511.06939, arxiv.org/abs/1511.06939
- Kang and McAuley, Self-Attentive Sequential Recommendation, arXiv:1808.09781, arxiv.org/abs/1808.09781
- Wikipedia, Recommender system