Contrastive Learning with Bidirectional Transformers for Sequential Recommendation

Hanwen Du, Hui Shi, Pengpeng Zhao, Deqing Wang, Victor S. Sheng, Yanchi Liu, Guanfeng Liu, Lei Zhao

Introduction

Sequential recommendation systems aim to predict a user's next interaction based on their historical behavior. Traditional approaches, such as SASRec, utilize unidirectional Transformers that rely on "causal masking," processing sequences strictly from left to right. However, real-world user behavior is often non-linear and complex, making bidirectional context—the ability to look at both preceding and succeeding items—highly valuable.

While bidirectional models like BERT4Rec exist, they have historically been difficult to integrate with Contrastive Learning (CL). Standard CL data augmentations, such as masking or cropping, often conflict with the bidirectional training objective (the "cloze task"). This paper introduces CBiT (Contrastive learning with Bidirectional Transformers), a framework that harmonizes bidirectional encoding with a novel multi-pair contrastive learning strategy to achieve state-of-the-art performance.

The CBiT Framework

CBiT addresses the limitations of existing models through several architectural and procedural innovations:

  1. Slide Window Technique: To manage long user histories without losing data to truncation, CBiT employs a sliding window. This approach preserves the entire sequence and allows the model to capture fine-grained, localized behavioral patterns across overlapping segments.
  2. Hybrid Augmentation Strategy: To generate effective "positive samples" for contrastive learning, CBiT combines data-level and model-level augmentations. It utilizes the cloze task mask (randomly masking items in the sequence) alongside a dropout mask (applying different dropout patterns within the Transformer layers). This creates multiple distinct "views" of the same sequence for the model to compare.
  3. Multi-Pair Contrastive Learning: Unlike standard CL, which typically compares a single pair of positive samples, CBiT scales this to $m$ pairs. This increases the density of training signals and forces the model to learn more robust, higher-order features.
  4. Dynamic Loss Reweighting: Balancing the recommendation task (cloze loss) and the contrastive task is a known challenge. CBiT implements a dynamic reweighting strategy that automatically adjusts the influence of the contrastive loss during training, ensuring stable convergence.
The CBiT architecture: User sequences pass through random masks and are encoded by bidirectional Transformers with different dropout masks to generate multiple positive samples for contrastive learning.

Experimental Results

The authors evaluated CBiT on three standard datasets: Amazon Beauty, Amazon Toys, and MovieLens-1M (ML-1M). These datasets represent a range of sequence lengths and sparsity levels.

Performance Gains

CBiT consistently outperformed state-of-the-art baselines, including DuoRec and BERT4Rec. On the Amazon Beauty dataset, CBiT achieved a 13.95% improvement in HR@5 and a 19.31% improvement in NDCG@5 over the strongest baseline. The model demonstrated significant robustness, performing well on both the short, sparse sequences of the Amazon datasets and the longer sequences of ML-1M.

Ablation and Convergence

Ablation studies confirmed that the synergy between the cloze task and dropout masks is critical. The hybrid augmentation strategy significantly outperformed configurations using only one type of mask or no contrastive learning at all. Furthermore, the dynamic loss reweighting strategy was shown to provide superior convergence compared to static proportional loss methods.

Ablation study (left) showing the superiority of the hybrid mask strategy, and training convergence (right) comparing dynamic loss reweighting against static proportional loss.

Conclusion

CBiT demonstrates that bidirectional Transformers, when paired with a sophisticated multi-pair contrastive learning framework, can significantly enhance sequential recommendation. By utilizing sliding windows and hybrid augmentations, the model captures a more holistic view of user behavior. Future work may explore more complex augmentation strategies and the integration of rich item metadata to further refine recommendation accuracy.

Summary updated

BibTeX
@inproceedings{du2022contrastive,
  author = {Hanwen Du and Hui Shi and Pengpeng Zhao and Deqing Wang and Victor S. Sheng and Yanchi Liu and Guanfeng Liu and Lei Zhao},
  title = {{Contrastive Learning with Bidirectional Transformers for Sequential Recommendation}},
  booktitle = {ACM International Conference on Information and Knowledge Management},
  year = {2022},
  url = {https://openreview.net/forum?id=r3ca47O8jRt},
}
Figure