PyTorch implementation of SASRec (Self-Attentive Sequential Recommendation) based on the paper for studying sequential recommender system:
Self-Attentive Sequential Recommendation (SASRec) Wang-Cheng Kang, Julian McAuley (ICDM 2018) https://arxiv.org/abs/1808.09781
SASRec-Pytorch/
β
βββ model/
β βββ sasrec.py # SASRec model, evaluation, trainer
β
βββ src/
β βββ data.py # data loader, sampler, batch generator
β βββ path.py # project path manager
β βββ utils.py # yaml loader, seed, misc utilities
β βββ main.py # main training script
β
βββ data/ # dataset directory (raw + processed)
β βββ ratings.csv # Movielens input file
β
βββ config.yaml # all hyperparameters & experiment settings
βββ README.md
All hyperparameters are centralized in config.yaml:
data:
dataset: "movielens"
max_len: 50
num_workers: 4
train:
num_epochs: 100
batch_size: 128
num_batches_per_epoch: 1000
device: "cuda"
best_model_path: "./checkpoints/best_model.pth"
early_stopping:
patience: 5
min_delta: 0.0
model:
hidden_dim: 64
num_blocks: 2
num_heads: 1
dropout: 0.2
optimizer:
name: "adam"
lr: 0.001
weight_decay: 0.0
loss:
name: "bce_with_logits"- Place
ratings.csvinsidedata/
data/
βββ ratings.csv
- Run main script:
uv run main.pyDuring training youβll see:
[Epoch 10] Train Loss: 0.3421
Val - NDCG@10: 0.4821, Hit@10: 0.7013
Test - NDCG@10: 0.4751, Hit@10: 0.6942
** Best model updated and saved to './checkpoints/best_model.pth' **
The repository implements the common metrics in sequential recommendation:
- NDCG@10
- Hit Ratio@10
Evaluation uses the standard:
- 1 positive item
- 100 negative samples
as in the original SASRec paper.
(Values vary depending on hyperparameters)
| Metric | Score |
|---|---|
| NDCG@10 | ~0.48 |
| Hit@10 | ~0.70 |
Inspired by the official SASRec implementations from: