Option to redraw negatives that coincide with the target in `CatalogUniformSampler`

未关闭 适合新手
#330 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
活跃
技术栈
python, pytorch

调研方向

从rectools/models/nn/transformers/negative_sampler.py开始,阅读CatalogUniformSampler.get_negatives;将其碰撞处理方式与rectools/fast_transformers/unisrec/model.py中的_NegativeSampler进行比较。通过negative_sampler_kwargs添加可选行为,同时保持默认行为不变,并检查 sampler 的单元测试。启用该行为时,发生碰撞的负样本会重新抽取,且默认情况下保留现有行为,即为完成。

由索引模型根据 Issue 内容生成。

描述

good first issue
Feature Description

Add an opt-in parameter to CatalogUniformSampler (for example avoid_positive_collisions: bool = False) that redraws sampled negatives equal to the target item at the same position. It would be enabled through the existing negative_sampler_kwargs:

SASRecModel(loss="gBCE", n_negatives=256, negative_sampler_kwargs={"avoid_positive_collisions": True})

The default would keep the current behaviour, so existing configs and seeded results do not change.

Why this feature?

CatalogUniformSampler.get_negatives (rectools/models/nn/transformers/negative_sampler.py) samples negatives uniformly from all catalog items (as its docstring says) using torch.randint, without excluding the target. With BCE, gBCE or sampled_softmax, the same item can then be scored as both the positive and a negative in one loss term.

The UniSRec model added in 0.19.0 already handles this: _NegativeSampler in rectools/fast_transformers/unisrec/model.py redraws colliding negatives once. The transformer models in rectools.models.nn (SASRec, BERT4Rec, HSTU) have no such option, although users can subclass TransformerNegativeSamplerBase to get it.

The collision probability per negative is 1 / n_items. With n_negatives=256:

catalog size share of negatives equal to the target share of positions with at least one such negative
3,416 (ML-1M as used in the SASRec/gSASRec papers) 0.03% 7.2%
500 0.2% 40%
100 1.0% 92%

On a synthetic dataset with 100 items (2,000 users, 22 interactions each), SASRecModel with loss="sampled_softmax" and n_negatives=256 reached NDCG@10 of about 0.127 (RecTools NDCG(k=10), default settings). Redrawing colliding negatives until none were left changed this by -0.0009 on average over 3 seeds, which is within run-to-run noise. So I don't expect a measurable metric gain; the aim is consistency with UniSRec and sampling negatives the way the gBCE sampling rate alpha = n_negatives / (n_items - 1) already assumes.

Additional context

I have a small patch ready (one redraw pass, same as _NegativeSampler, plus unit tests and a changelog entry) and can open a PR if this is welcome.

主要语言
Python
星标
457
派生
55
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

MTSWebServices/RecTools 的其他 Issue

查看 MTSWebServices/RecTools 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。