本仓库为 GRID (Generative Recommendation with Semantic IDs) 的复现项目,基于 Snap Research GRID 与 NeurIPS 2023 论文 TIGER (Recommender Systems with Generative Retrieval) 实现。在 Amazon Review(Beauty / Sports / Toys)三个数据集上完成端到端 Pipeline 复现,并对 Semantic ID 码本质量做了系统评估。
| 项目 | 链接 |
|---|---|
| TIGER (生成式召回) | NeurIPS 2023 |
| GRID (Semantic ID + 实践) | CIKM 2025 · GitHub |
- Semantic ID:用 RQ-KMeans 对 Item 的 Flan-T5 文本嵌入做残差量化,得到层次化离散编码(3 层语义 + 1 层消歧),语义相近的 Item 共享前缀。
- 生成式召回:T5 Encoder-Decoder 以用户历史 Semantic ID 序列为输入,自回归生成下一个 Item 的 Semantic ID,经 Beam Search 得到 Top-K 推荐,无需向量索引,推理阶段 O(1) 查表。
Pipeline 共 5 步:Item Embedding → 训练 RQ-KMeans 码本 → 生成 Semantic ID → 训练 TIGER 推荐模型 → 推理生成推荐列表。
- Python 3.10+
- CUDA(建议单卡 24GB+,如 RTX 4090)
- 依赖见
requirements.txt,或参考 GRID 官方 安装
pip install -r requirements.txt使用与 P5 / GRID 一致的 Amazon Review 预处理数据,目录结构:
data/
└── amazon_data/
├── beauty/
│ ├── training/ # TFRecord 交互序列
│ ├── evaluation/
│ ├── testing/
│ └── items/ # Item 文本
├── sports/
└── toys/
数据可从 GRID README 中的 Google Drive 链接 下载。
所有脚本位于 reproduction/sh/,请在仓库根目录下执行(即包含 src/、configs/ 的目录):
# 进入项目根目录
cd /path/to/this-repo
# Step 1: 用 Flan-T5-Base 生成 Item Embedding(Beauty/Sports/Toys)
bash reproduction/sh/step1_all_datasets.sh
# Step 2: 训练 RQ-KMeans 码本(按数据集分别运行)
bash reproduction/sh/step2_train_semantic_id_beauty.sh
bash reproduction/sh/step2_train_semantic_id_sports.sh
bash reproduction/sh/step2_train_semantic_id_toys.sh
# Step 3: 为每个 Item 生成 Semantic ID
bash reproduction/sh/step3_generate_semantic_ids.sh
# Step 4: 训练 TIGER 推荐生成器
bash reproduction/sh/step4_train_tiger_recommender.sh
# Step 5: 推理得到 Top-10 推荐
bash reproduction/sh/step5_tiger_inference.sh单数据集示例(仅 Beauty):
bash reproduction/sh/step1_generate_embeddings_t5base.sh # 需根据脚本内 data_dir 修改
bash reproduction/sh/step2_train_semantic_id_beauty.sh
# Step 3/4/5 需在脚本中指定对应 embedding_path / semantic_id_path / ckpt_path| 数据集 | Recall@5 | Recall@10 | NDCG@5 | NDCG@10 |
|---|---|---|---|---|
| Beauty | 0.0427 | 0.0631 | 0.0279 | 0.0345 |
| Sports | 0.0225 | 0.0338 | 0.0147 | 0.0183 |
| Toys | 0.0380 | 0.0552 | 0.0248 | 0.0303 |
- 利用率:前 3 层均为 100%(无 dead code),第 4 层为消歧层,利用率约 7–8% 符合预期。
- 冲突率:前 3 层存在 15–24% 冲突,全 4 层冲突率为 0%,每个 Item 唯一标识。
- 均匀度:归一化熵 > 0.91,基尼系数约 0.47–0.54。
更多细节与训练配置见 REPRODUCTION_REPORT.md。
├── configs/ # Hydra 配置
│ ├── experiment/ # 各步骤实验配置
│ └── ...
├── src/ # 训练与推理代码
│ ├── train.py
│ ├── inference.py
│ ├── models/
│ ├── data/
│ └── utils/
├── reproduction/
│ └── sh/ # 复现用 Shell 脚本
│ ├── step1_all_datasets.sh
│ ├── step2_train_semantic_id_*.sh
│ ├── step3_generate_semantic_ids.sh
│ ├── step4_train_tiger_recommender.sh
│ └── step5_tiger_inference.sh
├── REPRODUCTION_REPORT.md # 复现过程与指标说明
├── README.md
└── requirements.txt
- 框架:PyTorch、PyTorch Lightning、Hydra
- 模型:Flan-T5-Base(Embedding)、T5 Encoder-Decoder(TIGER)、RQ-KMeans(Semantic ID)
- 精度:Embedding 与 TIGER 训练使用
bf16-mixed,避免 T5 Post-LN 在 FP16 下溢出 - 评估:Codebook 利用率 / 冲突率 / 熵 / 基尼系数;推荐侧 Recall@K、NDCG@K
若使用本复现代码或思路,可引用原论文与 GRID:
@inproceedings{tiger2023,
title = {Recommender Systems with Generative Retrieval},
author = {Rajput, Shashank and others},
booktitle = {NeurIPS},
year = {2023}
}
@inproceedings{grid2025,
title = {Generative Recommendation with Semantic IDs: A Practitioner's Handbook},
author = {Ju, Clark Mingxuan and others},
booktitle = {CIKM},
year = {2025}
}