Add seq_utils shared utils for NextItemRecommender#697
Merged
Conversation
There was a problem hiding this comment.
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_utilspackage with session iterators, loss functions, andIndexedAdagradM. - Refactored GRU4Rec training to use
seq_utilslosses/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: |
Member
There was a problem hiding this comment.
should it be len(buffer_uids) > 0?
Member
There was a problem hiding this comment.
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'. |
qtuantruong
approved these changes
Jun 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
NextItemRecommender, including iterators, loss functions, and optimizers.io_iteris moved fromcornac/models/gru4rec/gru4rec.py, withsession_seq_iterfor upcoming modelsGRU4Rec, with several new loss functionsIndexedAdagradMfor now, used byGRU4RecGRU4RecChecklist:
README.md(if you are adding a new model).examples/README.md(if you are adding a new example).datasets/README.md(if you are adding a new dataset).