Profile

Settings

Loading…

← Papers

Help

FAQ

Which features require signing in?

You can read the commentary, paper info, Model Structure and related papers of public papers without signing in. Sign in to manage your account or contact us about your inquiries.

Can I upload a PDF file?

Upload papers with an arXiv or DOI link; the commentary is public to everyone. Papers whose source and terms of use can't be verified, so the commentary can't be made public, can't be uploaded. Uploading PDF files and keeping papers private are for subscribers only, and a paper uploaded as a file is saved as private only. Searching for official info by title runs only when you press the button.

Are the figures and AI commentary accurate?

Figures come only from the original paper, and only those whose terms of use are verified are shown. The commentary is written by AI and may be wrong. Check important claims, numbers and figure context against the evidence in the original paper.

I want to report a copyright issue.

Terms of use and the source of the original paper are on the Paper info tab. If there's a problem, choose "Paper content, figures and copyright" under Contact us and send the paper link, the part in question, and evidence that you hold the rights. Receiving your report doesn't confirm any rights or remove anything right away. You can see the result of the review under My inquiries.

I forgot my password.

If you signed up with email, use Forgot password in the sign-in window to reset it with your verified email. If you signed up with Google, Naver or Kakao, change your password with that service.

What should I include in an inquiry?

Tell us which screen you were on and what you were doing when the problem happened, along with the paper link. Don't include personal information such as passwords, API keys, verification codes or phone numbers. Inquiries and replies are stored in Refivu and you can see them under My inquiries.

How do I delete my account?

In Settings, under My account, press Delete account. Review what will be deleted and complete identity verification to delete your account. For other requests, choose "Account" under Contact us.

Paper commentary

BERT4Rec: Sequential Recommendation with Bidirectional Encoder Representations from Transformer

Bidirectional self-attention models user behaviour sequences better than left-to-right recommenders

  • Problem Left-to-right sequential recommenders encode each item only from the items before it. They assume a rigid order that user histories often do not follow.
  • Method BERT4Rec stacks bidirectional self-attention layers over a user's item history. It trains by masking items and predicting them from both sides, using the Cloze task instead of next-item prediction.
  • Results BERT4Rec reaches 0.3025 HR@10 on Beauty against 0.2653 for SASRec, the strongest sequential baseline in that table.

Why left-to-right models fall short

Sequential recommenders usually compress a user's history into one vector by reading the items from oldest to newest, so each position's hidden state only sees the items that come before it. The authors see two consequences. A representation of an earlier item cannot draw on later items, which limits how much context each position carries.

The left-to-right design was carried over from text and time series, where order is natural, and user logs do not always follow such a rigid order.

The choice of the next item in a purchase or viewing history can depend on external factors that do not appear in the sequence, so two consecutive items need not be ordered in a strict way. The authors therefore argue that a model of such histories should use context from both directions.

Training without revealing the target

If a bidirectional encoder reads the right context of each position during next-item training, the target leaks into the input and the task becomes trivial. The authors avoid this by using the Cloze objective, which removes the target from the input and asks the model to recover it from the surrounding items.

At each training step a proportion rho of the items in an input sequence is replaced by the [mask] token, and the original IDs of those items are the training targets. The paper's example masks the second and fourth items of a five-item sequence, so the model must fill those two positions from the items on either side.

Training a bidirectional model on next-item targets would require one separate subsequence for each position, which is slow. Masking yields many different training inputs from one sequence, and the paper counts the masked variants of a length-n sequence with k masked items as a binomial coefficient.

Stacked bidirectional Transformer layers

Each input position combines an item embedding with a learnable positional embedding, which lets the attention layers, which otherwise ignore order, use the position of each item. The learned table fixes the maximum length N, so longer histories are truncated to their last N items, with N set to 200 for ML-1m and ML-20m and 50 for Beauty and Steam.

Each of the L layers projects the layer input into h subspaces, computes scaled dot-product attention in each head, and concatenates and projects the head outputs. The attention is computed as \\( \\mathrm{Attention}(Q,K,V)=\\mathrm{softmax}\\left(\\frac{QK^\\top}{\\sqrt{d/h}}\\right)V \\), where Q, K and V are all projected from the same layer input.

The authors divide the dot products by the square root of d/h, which they say produces a softer attention distribution and avoids extremely small gradients.

Each sub-layer output is combined with its input through a residual connection, passed through dropout, and then normalized with layer normalization, so the update of each layer follows that order. The feed-forward sub-layer applies two affine maps to each position separately, with an inner width of 4d and a GELU activation between them. The authors use GELU rather than ReLU, following OpenAI GPT and BERT.

From hidden states to the training loss and test prediction

The hidden state at a masked position passes through a two-layer feed-forward network with GELU, and the result is multiplied by the transpose of the shared item embedding matrix E before a softmax over the item set. Sharing E between the input and the output reduces the model size, and the authors say it also helps against overfitting.

The training loss averages the negative log-probability of each true item over the masked items of a masked sequence, so minimizing it raises the probability of the correct items. Written with the masked items set S^m_u and the masked version S'_u of the history, the loss is \\( L = \\frac{1}{|S^m_u|}\\sum_{v_m \\in S^m_u} -\\log P(v_m = v^*_m \\mid S'_u) \\).

At test time a [mask] token is appended to the end of the user sequence, and the output at that final position is used to rank items for the next step. The authors also add training samples in which only the last item is masked, which they describe as a fine-tuning step toward the next-item task.

Positioning against SASRec, CBOW and BERT

The authors present the combination of deep bidirectional self-attention and the Cloze objective as the first such study in recommendation. SASRec is a left-to-right version of the same architecture with a single attention head and a causal mask, so the two models differ mainly in direction and training objective.

CBOW and skip-gram can be read as reduced cases of the Cloze objective, using one attention layer with uniform weights, no positional embedding and a single masked item. The authors note that CBOW aims at word representations rather than sentence representations, whereas BERT4Rec aims at a behaviour sequence representation.

BERT is pre-trained on large task-independent corpora, while BERT4Rec is trained end-to-end on each recommendation dataset, because the authors argue that the shared language background does not carry over to recommendation. The authors also drop the next-sentence loss and the segment embeddings, since a user's history is modelled as a single sequence.

Datasets and the ranking protocol

The experiments use four datasets, Amazon Beauty, Steam, ML-1m and ML-20m, which the authors chose to differ in domain and sparsity. Interactions are sorted by timestamp within each user, and users with at least five feedbacks are kept. Table 1 lists 40,226 users for Beauty with an average sequence length of 8.8, and 6040 users for ML-1m with an average length of 163.5.

The protocol is leave-one-out: the last item of each sequence is the test item, the item before it is the validation item, and the rest is used for training. Each test item is ranked against 100 negative items sampled according to popularity. Hit Ratio, NDCG and MRR are reported, with HR and NDCG at k = 1, 5 and 10, and with one ground-truth item per user HR@k equals Recall@k.

The authors note that the POP scores are low because the negatives are sampled by popularity.

Baselines are POP, BPR-MF, NCF, FPMC, GRU4Rec, GRU4Rec+, Caser and SASRec, and each baseline is run under its best hyper-parameter setting. BERT4Rec uses L = 2 and h = 2, is trained with Adam at a learning rate of 1e-4 from scratch, and has its mask proportion chosen on the validation set as 0.6 for Beauty, 0.4 for Steam and 0.2 for ML-1m and ML-20m.

Next-item results on four datasets

The authors report that BERT4Rec is best among all methods on the four datasets across all evaluation metrics. On Beauty, HR@10 is 0.3025 for BERT4Rec against 0.2653 for SASRec, the strongest value in the same table among the compared sequential baselines. On ML-20m, HR@1 is 0.3440 against 0.2544 for SASRec, a 35.22% improvement in the table's column.

Averaged over the four datasets, the reported improvements against the strongest baselines are 7.24% for HR@10, 11.03% for NDCG@10 and 11.46% for MRR. The table caption states that the improvements are statistically significant with p < 0.01.

Sequential baselines beat non-sequential ones on every dataset, which the authors take as evidence that sequence information helps. SASRec outperforms GRU4Rec and GRU4Rec+ in the authors' comparison, which they read as a sign that self-attention is a stronger tool for sequential recommendation. Caser performs worse than GRU4Rec+ and SASRec on sparse datasets, which the authors link to the small order needed by high-order Markov chains.

Bidirectionality or the Cloze objective

To separate the two factors, the Cloze task is restricted to one masked item, so the remaining difference from SASRec is the bidirectional context. Table 3 reports these comparisons at d = 256 for Beauty and ML-1m only, because of space limitations.

On Beauty, HR@10 is 0.2653 for SASRec, 0.2940 for BERT4Rec with one mask and 0.3025 for the full model. On ML-1m, HR@10 is 0.6629 for SASRec, 0.6869 for BERT4Rec with one mask and 0.6970 for the full model. The one-mask variant also scores higher than SASRec on NDCG@10 and MRR for both datasets.

The authors read the one-mask gain as evidence that bidirectional representations matter, and the further gain of the full model as evidence that the Cloze objective adds to it.

What the attention maps show

The authors average the attention weights over the last 10 positions during testing on Beauty and show four heat-maps from different layers and heads.

In layer 2 the attention concentrates on the more recent items, which the authors attribute to that layer feeding the output directly.

Unlike a unidirectional model, which attends only to the left, items in BERT4Rec attend to items on both sides, and the authors take this as evidence that bidirectionality is beneficial for behaviour sequences.

Mask proportion and sequence length

A rho of 0.2 is better than 0.1 on all datasets, which the authors use to support their claim that a very small proportion does not train a strong model.

The best rho is 0.6 for Beauty and 0.4 for Steam, which have short sequences, while ML-1m and ML-20m prefer 0.2.

The maximum length N follows the same pattern: Beauty scores best at N = 20 in the length sweep, whereas ML-1m is best at N = 200, while the main settings use N = 50 for Beauty.

Effect of hidden dimensionality

Figure 3 varies d from 16 to 256 and reports NDCG@10 and HR@10 for the neural sequential models. Performance tends to level off as d grows, and a larger d does not always help on sparse datasets such as Beauty and Steam, which the authors suspect is overfitting.

Caser is unstable across the four datasets, while SASRec and BERT4Rec perform best among the models in this sweep. BERT4Rec stays ahead of the other baselines even at small d, and the authors report results at d = 64 later because their model is satisfactory from d of 64 upward.

Which components matter

The ablation keeps d = 64 and the default L = 2 and h = 2, and changes one element at a time among positional embedding, position-wise feed-forward network, layer normalization, residual connection, dropout, layer number and head number. Removing the positional embedding lowers NDCG@10 from 0.4759 to 0.2155 on ML-1m and from 0.4513 to 0.2867 on ML-20m.

The authors explain that without positions the hidden state of each item depends only on its embedding, so different targets get the same [mask] representation.

Removing layer normalization lowers Beauty NDCG@10 from 0.1832 to 0.1642, and removing the position-wise feed-forward network lowers ML-20m NDCG@10 from 0.4513 to 0.4296. On ML-20m with L = 4, the authors report that NDCG@10 falls by about 10% without the residual connection.

On Beauty, L = 3 gives 0.1859 and L = 4 gives 0.1834, and the authors attribute the lower value at L = 4 to overfitting. Longer sequence datasets benefit from more heads: on ML-20m NDCG@10 rises from 0.4402 with one head to 0.4550 with eight, while Beauty is slightly better with fewer heads.

Limitations

The per-layer cost of self-attention is O(n^2 d) in the sequence length n, and the authors report that the attention layer parallelizes effectively on GPUs. Sequence length is capped by N, so longer histories lose their older items.

Because the Cloze objective masks items inside the sequence, the test procedure has to append a [mask] token at the end to match next-item prediction, which the authors note is a mismatch between training and the final task. Training speed is reported only for BERT4Rec across sequence lengths, not as a direct speed comparison with the baselines.

The experiments use four datasets, and BERT4Rec is trained from scratch without pre-training on a single GPU. Items are modelled only by their IDs, and the authors list adding item features and an explicit user component for multiple sessions as future directions.

Paper info

Authors
Fei Sun, Jun Liu, Jian Wu, Changhua Pei, Xiao Lin, Wenwu Ou, Peng Jiang
Venue
arXiv · Preprint
First published
2019-4-14
Added
2026-11-12
Updated
2019-8-21
Version
v2
arXiv
1904.06690
Category
cs.IR · cs.LG
Original paper terms
arXiv non-exclusive 1.0
What we show
Paper info and commentary

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

Quote from the paper

Fei Sun, Jun Liu, Jian Wu, Changhua Pei, Xiao Lin, Wenwu Ou, Peng Jiang (2019). BERT4Rec: Sequential Recommendation with Bidirectional Encoder Representations from Transformer. arXiv:1904.06690v2. https://arxiv.org/abs/1904.06690v2

BibTeX
@misc{sun2019bert4rec,
  title = {{BERT4Rec: Sequential Recommendation with Bidirectional Encoder Representations from Transformer}},
  author = {Fei Sun and Jun Liu and Jian Wu and Changhua Pei and Xiao Lin and Wenwu Ou and Peng Jiang},
  year = {2019},
  eprint = {1904.06690},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1904.06690v2}
}

See related papersBrowse other papers

Paper preview
Account security

Connect Google account

If you connect a Google account, you can sign in with it too.

Refivu

Delete account

Refivu
Refivu

Sign up