Skip to content

Add seq_utils shared utils for NextItemRecommender#697

Merged
hieuddo merged 3 commits into
PreferredAI:masterfrom
hieuddo:seq-utils
Jun 2, 2026
Merged

Add seq_utils shared utils for NextItemRecommender#697
hieuddo merged 3 commits into
PreferredAI:masterfrom
hieuddo:seq-utils

Conversation

@hieuddo
Copy link
Copy Markdown
Member

@hieuddo hieuddo commented Jun 1, 2026

Description

  • Add some shared utils for NextItemRecommender, including iterators, loss functions, and optimizers.
    • iterators: io_iter is moved from cornac/models/gru4rec/gru4rec.py, with session_seq_iter for upcoming models
    • loss functions: including all losses from GRU4Rec, with several new loss functions
    • optimizers: only IndexedAdagradM for now, used by GRU4Rec
  • Remove util functions from GRU4Rec
  • Fix some type errors

Checklist:

  • I have added tests.
  • I have updated the documentation accordingly.
  • I have updated README.md (if you are adding a new model).
  • I have updated examples/README.md (if you are adding a new example).
  • I have updated datasets/README.md (if you are adding a new dataset).

Copilot AI review requested due to automatic review settings June 1, 2026 11:15
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new cornac.models.seq_utils module to centralize shared utilities for NextItemRecommender-style sequential models, and refactors GRU4Rec to consume these shared iterators/losses/optimizer instead of defining them inline.

Changes:

  • Added seq_utils package with session iterators, loss functions, and IndexedAdagradM.
  • Refactored GRU4Rec training to use seq_utils losses/optimizer/iterator and added stronger loss-name validation.
  • Cleaned up GRU4Rec internals (removed embedded loss helpers; added padding rows to embeddings for safer scoring behavior).

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
cornac/models/seq_utils/optim.py Adds shared IndexedAdagradM optimizer implementation.
cornac/models/seq_utils/losses.py Adds centralized loss functions and a name lookup utility for sequential models.
cornac/models/seq_utils/iterators.py Adds shared session-based training iterators (io_iter, session_seq_iter).
cornac/models/seq_utils/__init__.py Exposes seq_utils public API and documents expected iterator/loss conventions.
cornac/models/gru4rec/recom_gru4rec.py Switches GRU4Rec training to shared seq_utils components; adds loss validation and scoring trim logic.
cornac/models/gru4rec/gru4rec.py Removes inline iterator/optimizer/loss helpers; adjusts embeddings to include a padding row and improves weight reset logic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +23 to +30
def softmax_neg(X):
"""Softmax over negatives, masking out the diagonal (positives)."""
hm = 1.0 - torch.eye(*X.shape, out=torch.empty_like(X))
X = X * hm
e_x = torch.exp(X - X.max(dim=1, keepdim=True)[0]) * hm
if e_x.size(0) == 1:
return e_x
return e_x / (e_x.sum(dim=1, keepdim=True) + 1e-24)
out_iids,
)
buffer_uids, buffer_hist, buffer_target = [], [], []
if len(buffer_uids) > 1:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be len(buffer_uids) > 0?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems that this one is for upcoming models, not really sure if this is intentional

Comment on lines +23 to +24
class IndexedAdagradM(Optimizer):
"""Sparse-aware Adagrad with momentum, used by GRU4Rec and FPMC."""
Comment on lines 49 to +51
loss: str, optional, default: 'cross-entropy'
Select the loss function.
Loss function. Supported: 'cross-entropy', 'bpr', 'bpr-max', 'top1',
'bce', 'ce'.
Comment on lines +23 to +30
def softmax_neg(X):
"""Softmax over negatives, masking out the diagonal (positives)."""
hm = 1.0 - torch.eye(*X.shape, out=torch.empty_like(X))
X = X * hm
e_x = torch.exp(X - X.max(dim=1, keepdim=True)[0]) * hm
if e_x.size(0) == 1:
return e_x
return e_x / (e_x.sum(dim=1, keepdim=True) + 1e-24)
out_iids,
)
buffer_uids, buffer_hist, buffer_target = [], [], []
if len(buffer_uids) > 1:
Comment on lines +23 to +24
class IndexedAdagradM(Optimizer):
"""Sparse-aware Adagrad with momentum, used by GRU4Rec and FPMC."""
Comment on lines 49 to +51
loss: str, optional, default: 'cross-entropy'
Select the loss function.
Loss function. Supported: 'cross-entropy', 'bpr', 'bpr-max', 'top1',
'bce', 'ce'.
@hieuddo hieuddo requested review from lthoang and qtuantruong June 1, 2026 11:36
@hieuddo hieuddo merged commit 08c6026 into PreferredAI:master Jun 2, 2026
19 checks passed
@hieuddo hieuddo deleted the seq-utils branch June 4, 2026 05:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants