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

オープン 初心者向け
#330 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

MTSWebServices/RecTools のほかの issue

MTSWebServices/RecTools の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。