Skip to content

Add epsilon to MTER progress postfix to prevent ZeroDivisionError - #723

Merged
qtuantruong merged 1 commit into
PreferredAI:masterfrom
Shxiao101:fix/mter-zero-division
Sep 13, 2026
Merged

qtuantruong merged 1 commit into
PreferredAI:masterfrom
Shxiao101:fix/mter-zero-division

Conversation

@Shxiao101

Copy link
Copy Markdown
Contributor

Description

MTER.fit can still raise ZeroDivisionError when every sampled BPR pair is skipped, because the "correct" percentage in the progress postfix divides by n_bpr_samples - skipped.

#677 and #686 added + 1e-8 to this denominator in BPR, WBPR, Companion, ComparERSub and LRPPM, but cornac/models/mter/recom_mter.pyx was not included. This PR applies the same one-line change to MTER.

Note that the postfix is computed even with verbose=False (only the bar display is disabled), so the error also happens in non-verbose runs.

Reproduction (2 users x 2 items, all rated 5.0, so every sampled pair is skipped):

from cornac.data import SentimentModality
from cornac.eval_methods import BaseMethod
from cornac.models import MTER

ratings = [("u1", "i1", 5.0), ("u1", "i2", 5.0), ("u2", "i1", 5.0), ("u2", "i2", 5.0)]
sentiment = [
    ("u1", "i1", [("a1", "o1", 1.0)]), ("u1", "i2", [("a2", "o2", 1.0)]),
    ("u2", "i1", [("a1", "o2", -1.0)]), ("u2", "i2", [("a2", "o1", 1.0)]),
]
bm = BaseMethod.from_splits(train_data=ratings, test_data=ratings, exclude_unknowns=True,
                            sentiment=SentimentModality(data=sentiment), seed=123)
MTER(n_user_factors=2, n_item_factors=2, n_aspect_factors=2, n_opinion_factors=2,
     n_bpr_samples=10, n_element_samples=4, max_iter=3, n_threads=1, seed=123).fit(bm.train_set)

Before this change:

  File "cornac/models/mter/recom_mter.pyx", line 422, in cornac.models.mter.recom_mter.MTER.fit
ZeroDivisionError: float division by zero

After this change, fit completes.

Testing (Windows, Python 3.12, MSVC build via python setup.py build_ext --inplace):

  • Reproduction above: fails on master, passes with this change.
  • flake8 . --select=E9,F63,F7,F82 (same as CI): 0.
  • pytest tests/cornac/models tests/cornac/data tests/cornac/eval_methods: 139 passed.

Related Issues

Follow-up to #675 (fixed for BPR in #677 and for other BPR-based models in #686).

Checklist:

🤖 Generated with Claude Code

When every sampled BPR pair is skipped (e.g. on very small datasets where
all sampled pairs have equal ratings), n_bpr_samples - skipped is 0 and
computing the "correct" percentage raises ZeroDivisionError. Add + 1e-8
to the denominator, consistent with the other BPR-based models fixed in
PreferredAI#677 and PreferredAI#686.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

@qtuantruong qtuantruong left a comment

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.

Thanks for the fix!

@qtuantruong
qtuantruong merged commit 9e16f46 into PreferredAI:master Sep 13, 2026
2 of 17 checks passed
@Shxiao101
Shxiao101 deleted the fix/mter-zero-division branch September 14, 2026 00:11
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.

2 participants