BERT4Rec: Sequential Recommendation with Bidirectional Encoder Representations from Transformer

Fei Sun · OpenReview

TL;DR

This paper introduces BERT4Rec, a novel sequential recommendation model that leverages bidirectional self-attention, inspired by BERT, to better capture user preferences. By employing a "Cloze task" objective (predicting masked items), BERT4Rec overcomes the limitations of traditional left-to-right models, leading to significant performance improvements across multiple benchmark datasets.

What Problem Does This Paper Address?

Recommending items to users is a core task in many online platforms. A key challenge is understanding and predicting users' dynamic preferences, which evolve over time and are reflected in their interaction history (e.g., purchases, clicks). Traditional sequential recommendation models typically process user interactions in a strict left-to-right order, using recurrent neural networks (RNNs) or unidirectional Transformers. The authors argue that this unidirectional approach is suboptimal for several reasons:

  1. Limited Representation Power: A left-to-right model can only incorporate information from past interactions, potentially missing crucial context from future interactions within the same sequence.
  2. Rigid Order Assumption: User behavior sequences are not always strictly ordered. Forcing a rigid temporal order can be an oversimplification and might not accurately reflect user intent.

These limitations hinder the ability of models to learn rich, context-aware representations of user behavior, ultimately impacting recommendation accuracy.

Methods

BERT4Rec tackles these issues by introducing two main innovations:

  1. Bidirectional Self-Attention: Instead of a unidirectional Transformer, BERT4Rec uses a deep bidirectional self-attention mechanism. This allows each item in a user's interaction sequence to attend to and incorporate information from all other items in the sequence, regardless of their temporal order. This is achieved by stacking multiple Transformer layers, each containing a multi-head self-attention sub-layer and a position-wise feed-forward network.
  2. Cloze Task Objective: A direct application of bidirectional self-attention to predict the next item would lead to information leakage (the model would "see" the future item it's supposed to predict). To circumvent this, the authors adapt the "Cloze task" from Natural Language Processing. During training, a certain percentage of items in the input sequence are randomly masked (replaced with a special [mask] token). The model is then trained to predict the original IDs of these masked items by conditioning on both their left and right context. This forces the model to learn a truly bidirectional representation without leaking future information. For inference, the last item is masked, and the model predicts it.

The model also incorporates positional embeddings to retain sequence order information and uses standard components like residual connections, layer normalization, and dropout for stable training.

Datasets

The authors evaluated BERT4Rec on four publicly available benchmark datasets:

  • Amazon Beauty: A dataset of product reviews from Amazon, focusing on the "Beauty" category.
  • Steam: A dataset collected from the Steam online game distribution platform.
  • MovieLens 1M & 20M: Two popular versions of the MovieLens dataset, widely used for recommender system research.

These datasets vary in domain and sparsity, allowing for a comprehensive evaluation. The authors followed common preprocessing steps, converting all interactions to implicit feedback (1) and keeping users with at least five interactions.

Key Insights and Ideas

  • Bidirectionality is Key: The paper strongly suggests that modeling user behavior sequences bidirectionally, rather than unidirectionally, is crucial for capturing richer user preferences and achieving better recommendation performance.
  • Cloze Task as a Smart Objective: Adapting the Cloze task from NLP provides an elegant solution to train bidirectional models for sequential recommendation without information leakage, enabling the model to learn from both past and future context within a sequence.
  • Self-Attention's Power: The success of BERT4Rec reinforces the idea that self-attention mechanisms, which can capture long-range dependencies effectively, are highly suitable for sequential recommendation tasks.
  • Hyperparameter Sensitivity: The optimal performance of BERT4Rec is influenced by hyperparameters like the mask proportion (p) and maximum sequence length (N), which often depend on the characteristics of the dataset (e.g., average sequence length).

Key Results

BERT4Rec consistently outperformed state-of-the-art sequential recommendation baselines across all four datasets.

  • Overall Performance: The model achieved significant improvements, with average gains of 7.24% in HR@10, 11.03% in NDCG@10, and 11.46% in MRR over the strongest baselines.
  • Bidirectional vs. Unidirectional: An ablation study showed that BERT4Rec with a single mask significantly outperformed SASRec (a unidirectional Transformer-based model), highlighting the benefit of bidirectionality.
  • Cloze Task Effectiveness: The Cloze task was shown to be effective, with further analysis indicating that the mask proportion needs careful tuning based on dataset characteristics. For instance, shorter sequences benefit from higher mask proportions, while longer sequences require lower proportions to avoid making the task too difficult.
  • Hyperparameter Impact: The paper provides insights into the impact of hidden dimensionality, mask proportion, and maximum sequence length, demonstrating that BERT4Rec is robust and performs well across various settings.

Limitations and Future Work

  • Computational Complexity: The self-attention mechanism has a quadratic complexity ($O(n^2d)$) with respect to sequence length ($n$), which can be a bottleneck for very long sequences. However, the authors note that GPUs can effectively parallelize this computation.
  • Future Directions: The authors suggest several avenues for future research:
  • Incorporating Rich Item Features: Extending BERT4Rec to leverage item attributes (e.g., category, price, cast) beyond just item IDs.
  • User Component Modeling: Introducing a user component to explicitly model users with multiple sessions, which is not directly handled by the current model.

Summary updated

BibTeX
@inproceedings{sun2019bert,
  author = {Fei Sun},
  title = {{BERT4Rec: Sequential Recommendation with Bidirectional Encoder Representations from Transformer}},
  booktitle = {ACM International Conference on Information and Knowledge Management},
  year = {2019},
  doi = {10.1145/3357384.3357895},
}
Figure