Search

Search papers and lessons

Session-based Recommendations with Recurrent Neural Networks

Balázs Hidasi, Alexandros Karatzoglou, Linas Baltrunas et al. · 2015

GRU4Rec models a shopping or browsing session as a sequence and predicts the next click with a gated recurrent network trained on a ranking loss, beating item-to-item baselines on two real click datasets.

On this page

  • Colour = topic mix
  • Neural network basics
  • Calculus
  • Discrete math
  • Linear algebra
  • Probability
  • Losses & regularization
  • ReadReady to readNot yet

TL;DR

Hidasi and colleagues showed that a gated recurrent network can read a shopper's clicks in order and predict the next one. The usual recommender methods of the time needed a long user history, and most web shops do not have one: many visitors are anonymous and have only one short session. The authors trained the network with a pairwise ranking loss, which tells the model that the clicked item should score above sampled items the user did not click. On two real click datasets their best variants reached a Recall@20 roughly 20 to 25 percent above the strongest item-to-item baseline.

Builds on: Gated Recurrent Unit, Backpropagation Through Time, Recommender System, Negative Sampling, Pairwise Ranking Loss · Leads to: the family of sequence-based recommenders that followed it

Why it mattered

Session-based recommenders are the default for many small shops, news sites and video services, where the visitor is not logged in. The paper showed that a recurrent network trained on the whole click sequence beat the simple item-to-item rules that dominated that setting. It also set out practical choices, such as how to build mini-batches from sessions and how to sample negatives cheaply, that later sequence-based recommenders could build on. The paper is widely cited in recurrent recommendation work.

The problem

A recommender usually works by learning a vector for each user and each item and taking the dot product to score a pair. This is the Matrix Factorization approach, and it needs a user profile built from many past events. In a session there is no profile: the user is anonymous, and the session may contain only two or three clicks. The common fallback was item-to-item similarity, computed from co-occurrence counts. It uses only the last click, so it ignores what the visitor looked at earlier in the same session.

Two further problems follow. First, the catalogue can have tens of thousands to millions of items, so scoring every item at each step is too slow. Second, the training signal is sparse: the user did not click most items, but an unclicked item is not necessarily one the user dislikes. The paper has to model the order of clicks, scale to a large catalogue, and learn from positive clicks against negatives in a way that ranks the right items at the top.

How it works

The pipeline runs in this order. Each stage is shown on one event so the flow is clear.

  1. Input encoding: each click is a one-hot vector over all items. If the catalogue has 37,483 items, the vector has that many entries, with a 1 at the clicked item and zeros elsewhere. The paper found that this was slightly better than using a learned embedding of the items. Check: a click on item 5 in a 10-item catalogue gives the vector (0,0,0,0,1,0,0,0,0,0)(0, 0, 0, 0, 1, 0, 0, 0, 0, 0).

  2. Recurrent state: a single layer of GRU units reads the click and updates its hidden state ht\mathbf{h}_t, which summarises the session so far. The paper uses one GRU layer, with 100 or 1000 hidden units. Check: after the first click the state depends only on that click; after the second, it depends on both.

  3. Output scores: a linear output layer turns the hidden state into one score per item. The paper found it helpful to apply tanh⁡\tanh to the output layer. Check: the output is one number per item, and a higher number means the item is predicted to come next.

  4. Session-parallel mini-batches: many sessions are processed side by side. Each slot in the batch holds one session. When a session ends, the slot is given the next session and its hidden state is reset to zero, because sessions are independent. This keeps every batch full even though sessions have different lengths, from 2 events to a few hundred. Check: a session never sees the hidden state of the session before it.

  5. Sampled output: scoring every item at every step is too costly, so the loss only uses the target item and a few negatives. The negatives are the targets of the other sessions in the same mini-batch. Because popular items appear more often as targets, these negatives are sampled roughly in proportion to popularity. Check: with a batch of 50 sessions there are 49 negatives per example at no extra sampling cost.

  6. Ranking loss: the model is trained so that the target item scores above the negatives. The paper uses two losses: a pairwise BPR loss and a new loss, TOP1, described in the math below. Check: lowering the target's score or raising a negative's score must increase the loss.

flowchart LR
  A["Session clicks, one-hot"] --> B["GRU hidden state h_t"]
  B --> C["Score for every item, tanh output"]
  C --> D["Target item vs sampled negatives"]
  D --> E["Ranking loss, BPR or TOP1"]
  E -->|"gradients through time"| B

The math

The GRU update

A recurrent unit in the paper follows the gated update of Cho and colleagues. The update gate zt\mathbf{z}_t and reset gate rt\mathbf{r}_t are computed from the input xt\mathbf{x}_t and the previous state ht−1\mathbf{h}_{t-1}:

zt=σ(Wzxt+Uzht−1),rt=σ(Wrxt+Urht−1).\mathbf{z}_t = \sigma(W_z \mathbf{x}_t + U_z \mathbf{h}_{t-1}), \qquad \mathbf{r}_t = \sigma(W_r \mathbf{x}_t + U_r \mathbf{h}_{t-1}).

The candidate state is

h^t=tanh⁡(Wxt+U(rt⊙ht−1)),\hat{\mathbf{h}}_t = \tanh\left(W \mathbf{x}_t + U(\mathbf{r}_t \odot \mathbf{h}_{t-1})\right),

and the new state is a weighted average of the old state and the candidate:

ht=(1−zt)⊙ht−1+zt⊙h^t.\mathbf{h}_t = (1 - \mathbf{z}_t) \odot \mathbf{h}_{t-1} + \mathbf{z}_t \odot \hat{\mathbf{h}}_t.

Here σ\sigma is the logistic sigmoid, ⊙\odot is elementwise multiplication, and each WW and UU is a learned matrix. When zt\mathbf{z}_t is near zero the state is copied forward, which is how the unit keeps information over long spans.

Step 3 in symbols: the scores

The output for item kk at step tt is a score r^t,k\hat{r}_{t,k}, computed from ht\mathbf{h}_t by a linear map followed by tanh⁡\tanh. The paper's choice of tanh⁡\tanh at the output layer is reported as beneficial.

Step 5 in symbols: the sampled loss

For one step of one session, let ii be the target item and j=1,…,NSj = 1, \ldots, N_S the sampled negative items. The BPR loss from Bayesian personalised ranking compares the target with each negative:

LBPR=−1NS∑j=1NSlog⁡σ(r^i−r^j).L_{\mathrm{BPR}} = -\frac{1}{N_S} \sum_{j=1}^{N_S} \log \sigma\left(\hat{r}_{i} - \hat{r}_{j}\right).

Minimising this pushes r^i\hat{r}_i above each r^j\hat{r}_j. The TOP1 loss introduced in the paper approximates the relative rank of the target. The rank is the number of negatives that score above the target, and the paper replaces the indicator "negative scores above target" with a sigmoid. It adds a regulariser that pulls each negative score toward zero:

LTOP1=1NS∑j=1NS[σ(r^j−r^i)+σ(r^j 2)].L_{\mathrm{TOP1}} = \frac{1}{N_S} \sum_{j=1}^{N_S} \left[\sigma\left(\hat{r}_{j} - \hat{r}_{i}\right) + \sigma\left(\hat{r}_{j}^{\,2}\right)\right].

The first term is the sigmoid approximation of "negative beats target". The second term pulls the negative scores toward zero, which the paper added to stop scores drifting upward during training.

Results

The evaluation feeds each test session one click at a time and asks where the next clicked item ranks. The main metric is Recall@20, the share of test cases where the next item is in the top 20. The second metric is MRR@20, the average reciprocal rank, set to zero when the item is below rank 20.

The two datasets are RSC15, built from the RecSys Challenge 2015 e-commerce click stream, and VIDEO, from a video service. RSC15 has 7,966,257 training sessions with 31,637,239 clicks on 37,483 items, and its test set has 15,324 sessions with 71,222 events. VIDEO has about 3 million training sessions with about 13 million watch events on 330 thousand videos, and about 37 thousand test sessions.

Item-KNN, the strongest baseline, scores Recall@20 of 0.5065 and MRR@20 of 0.2048 on RSC15, and Recall@20 of 0.5508 and MRR@20 of 0.3381 on VIDEO. The paper's Table 3 shows the best GRU variants:

Loss and unitsRSC15 Recall@20RSC15 MRR@20VIDEO Recall@20VIDEO MRR@20
TOP1, 1000.58530.23050.61410.3511
TOP1, 10000.62060.26930.66240.3891
BPR, 10000.63220.24670.63110.3136
Cross-entropy, 1000.60740.24300.63720.3720

The best Recall@20 on RSC15 is BPR with 1000 units, at 0.6322 (+24.82 percent over Item-KNN). The best MRR@20 on RSC15 and the best results on VIDEO come from TOP1 with 1000 units, which reaches 0.6624 Recall@20 on VIDEO, 20.27 percent above Item-KNN. The paper's own summary is that the TOP1 loss gives "~20-30%" accuracy gain over the best baseline on these two datasets.

The limits are important. Cross-entropy was numerically unstable: only 10 of 100 random runs were stable on RSC15 and 6 of 100 on VIDEO, and the paper gives no result for cross-entropy with 1000 units on VIDEO. BPR was worse than Item-KNN on MRR@20 for VIDEO. Both datasets come from single services, the experiments are offline, and the baselines are the ones available in 2015. The results show that the approach is strong on these data, not that it wins everywhere.

Reading guide

Check yourself

QuestionRecall: why does the paper use session-parallel mini-batches instead of cutting sessions into fixed-length windows, as NLP models usually do?Show answer

Sessions vary in length, from 2 events to a few hundred, and the goal is to model how one session evolves over time. Cutting a session into windows would break that evolution. The paper therefore keeps each session whole and refills an empty slot with the next session, resetting the hidden state.

QuestionComputation: with the TOP1 loss, one target has score 2 and one sampled negative has score 0. What is the loss? Use NS=1N_S = 1.Show answer

The loss is σ(r^j−r^i)+σ(r^j2)=σ(0−2)+σ(0)=σ(−2)+0.5\sigma(\hat{r}_j - \hat{r}_i) + \sigma(\hat{r}_j^2) = \sigma(0 - 2) + \sigma(0) = \sigma(-2) + 0.5. Since σ(−2)=1/(1+e2)≈0.119\sigma(-2) = 1/(1 + e^2) \approx 0.119, the loss is about 0.6190.619.

QuestionWhy: the TOP1 loss has a second term, σ(r^j2)\sigma(\hat{r}_j^2), that pulls each negative score toward zero. What would go wrong without it?Show answer

The first term only asks the target to score above the negatives. Any item that is a target in one step is a negative in another, so without the second term its score would be pushed up in one step and not pushed down in the other, and the scores would keep growing. The paper found training unstable in that case. The regulariser keeps negative scores near zero, which is a natural reference point for them.

QuestionApplied: a small shop has only anonymous sessions of 2 to 4 clicks and a catalogue of 2,000 items. Should it expect GRU4Rec to beat item-to-item similarity?Show answer

The paper's setting is close to this one, since its main problem is anonymous sessions and item-to-item rules that use only the last click. The gain is most likely where the session order matters. But a small catalogue means fewer negatives and less training data, and the paper's gains were measured on millions of sessions. The shop should check the claim with a held-out test set of its own sessions, comparing Recall@20 against item-to-item similarity, and not assume the reported gains transfer.

Sources

Read the full paper

Balázs Hidasi, Alexandros Karatzoglou, Linas Baltrunas et al., 2015. arXiv:1511.06939