# rme **Repository Path**: ouyueyang/rme ## Basic Information - **Project Name**: rme - **Description**: repo for rec models training (to eval) - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2026-03-24 - **Last Updated**: 2026-04-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 0. 准备数据 在datasets/raw/下准备MIND和ml-1m数据集。 # 1. 预处理数据 ```bash python scripts/preprocess_ml1m.py --ratings_path datasets/raw/ml-1m/ratings.dat --movies_path datasets/raw/ml-1m/movies.dat --users_path datasets/raw/ml-1m/users.dat --output_dir datasets/processed/ml-1m python scripts/preprocess_mind.py --train_news_path datasets/raw/MINDsmall_train/news.tsv --train_behaviors_path datasets/raw/MINDsmall_train/behaviors.tsv --dev_news_path datasets/raw/MINDsmall_dev/news.tsv --dev_behaviors_path datasets/raw/MINDsmall_dev/behaviors.tsv --output_dir datasets/processed/mind ``` 对于ml1m数据集: 我们首先定义正样本:将用户对电影的评分(rating)>= 4的记录视为“正向”交互。 在这一步,预处理脚本只保留那些拥有至少3条正向交互记录的用户,以确保每个用户都有足够的历史数据来训练和评估模型。这一步之后,用户总数固定为 6035。 接着,对每个用户,按其交互时间戳排序,得到一个按时间先后排列的物品交互列表。对每个用户的时序交互序列单独处理: - 确定测试样本:从该用户的序列中,取出最后一个交互物品作为测试集的正样本(target item)。 - 确定验证样本:取出倒数第二个交互物品作为验证集的正样本(用于超参数调优或早停)。 - 确定训练序列:用户序列中剩下的所有历史交互,构成了该用户的训练序列。 概览: ```bash { "positive_threshold": 4.0, "min_user_pos": 3, "n_users": 6035, "n_items": 3533, "n_train": 563206, "n_valid": 6035, "n_test": 6035, "n_all_positive": 575276, "n_full_ratings_kept": 999421 } ``` # 2. 在ML-1M上训练模型 以 train_lightgcn_ml1m.py 为例: 它使用 BPRTrainer (Bayesian Personalized Ranking Trainer)。BPR 是一种为隐式反馈(即只有正样本,没有负样本)设计的训练方法。它通过在训练时为每个正样本(用户交互过的物品)随机采样一个用户未交互过的物品作为负样本,来构建训练数据对,从而优化模型。 ```bash python scripts/train_lightgcn_ml1m.py --config recsys/configs/lightgcn_ml1m.yaml python scripts/train_sasrec_ml1m.py --config recsys/configs/sasrec_ml1m.yaml python scripts/train_deepfm_ml1m.py --config recsys/configs/deepfm_ml1m.yaml ``` # 3. 在MIND上训练模型 ```bash python scripts/train_lightgcn_mind.py --config recsys/configs/lightgcn_mind.yaml python scripts/train_sasrec_mind.py --config recsys/configs/sasrec_mind.yaml python scripts/train_nrms_mind.py --config recsys/configs/nrms_mind.yaml ``` 上述训练完成后,top10推荐结果默认已经生成(除deepfm)。 # 4. 为DeepFM生成Top-K推荐结果 DeepFM是CTR预测模型,需要对所有候选items进行评分,然后选择Top-K。 这个脚本为测试集中的每个用户生成Top-K推荐。 ```bash python scripts/generate_deepfm_topk.py \ --model_path checkpoints/deepfm_ml1m_best.pt \ --data_dir datasets/processed/ml-1m \ --output_file results/topk/deepfm_ml1m_top10.csv \ --k 10 ``` # 5. 构造评估样本 将推荐模型生成的Top-K推荐结果转换为评估器可用的json标准评估样本(尽管mae不使用,而是直接使用3、4生成的topk表格)。 每个评估样本的格式为:(user_id, algorithm, recommendation_list) 用法: ```bash # MovieLens-1M python scripts/build_evaluation_samples.py \ --dataset ml1m \ --topk_dir results/topk \ --output_dir results/evaluation_samples # MIND-small python scripts/build_evaluation_samples.py \ --dataset mind \ --topk_dir results/topk \ --output_dir results/evaluation_samples ``` 输出: ```bash results/evaluation_samples/ ├── ml1m_evaluation_samples.json └── mind_evaluation_samples.json ``` # 6. 构造外部参照 为评估样本构造list-level的外部参照分数,作为满意度代理标签。 对于MovieLens-1M: - 使用用户对推荐列表中item的真实评分构造满意度标签 - 支持多种计算方式:平均评分、加权评分等 - 归一化到1-5区间 对于MIND-small: - 使用用户的点击行为构造满意度标签 - 支持多种计算方式:点击率、位置加权点击效用等 - 归一化到1-5区间 用法: ```bash # MovieLens-1M python scripts/build_reference_signals.py \ --dataset ml1m \ --samples_file results/evaluation_samples/ml1m_evaluation_samples.json \ --data_dir datasets/processed/ml-1m \ --output_file results/evaluation_samples/ml1m_reference_signals.json \ --method avg_rating # MIND-small python scripts/build_reference_signals.py \ --dataset mind \ --samples_file results/evaluation_samples/mind_evaluation_samples.json \ --data_dir datasets/processed/mind \ --output_file results/evaluation_samples/mind_reference_signals.json \ --method click_rate ``` 输出格式(json): ```bash [ { "user_id": "1", "algorithm": "lightgcn", "reference_score": 3.8, # 1-5 scale "raw_score": 4.2, # before normalization "n_hits": 5, # number of hits in recommendations "n_total": 10 # total recommendations }, ... ] ``` # 7. 为MAE准备输入 将数据集整合为mae需求的格式。train_data实际包含了完整的数据集,和item_meta一并用以给llm提供用户的完整历史交互信息,test_data则用于确定评估的用户范围和基准。 用法: ```bash # MovieLens-1M python scripts/prepare_mae_inputs.py \ --dataset ml1m \ --samples_file results/evaluation_samples/ml1m_evaluation_samples.json \ --data_dir datasets/processed/ml-1m \ --output_dir results/mae_inputs/ml1m # MIND-small python scripts/prepare_mae_inputs.py \ --dataset mind \ --samples_file results/evaluation_samples/mind_evaluation_samples.json \ --data_dir datasets/processed/mind \ --output_dir results/mae_inputs/mind ``` 输出: ```bash results/mae_inputs// ├── train_data.csv # 训练交互数据 ├── test_data.csv # 测试交互数据 ├── item_meta.csv # item元数据 └── _rec.csv # 每个算法的推荐结果 ``` # 8. 为MAE准备rag库 # 9. 使用MAE评估模型推荐结果 修改脚本参数: 对于ml1m数据集: ```python output_dir = "./results/mae_outputs/" # ... ml1m_exp_config = ExperimentConfig( sample_user_limit=2, # 小批量测试 # ... ) # ... ml1m_train = pd.read_csv("results/mae_inputs/ml1m/train_data.csv") ml1m_test = pd.read_csv("results/mae_inputs/ml1m/test_data.csv") ml1m_item_meta = pd.read_csv("results/mae_inputs/ml1m/item_meta.csv") ml1m_rec_map = { "LightGCN": pd.read_csv("results/mae_inputs/ml1m/lightgcn_rec.csv"), "SASRec": pd.read_csv("results/mae_inputs/ml1m/sasrec_rec.csv"), "DeepFM": pd.read_csv("results/mae_inputs/ml1m/deepfm_rec.csv"), } # ... ml1m_detail, ml1m_summary, ml1m_comp = evaluate_multiple_algorithms( # ... rag_library_path="evaluators/MAEv2/ml1m/ml1m_rag_library.jsonl", # 在第8步准备 ) save_outputs(ml1m_detail, ml1m_summary, ml1m_comp, output_dir, prefix="ml1m_all_algorithms") ``` 对于mind数据集同理。 运行脚本评估: ```bash python evaluators/MAEv2/rs_llm_eval_pipeline_v2.py ```