Skip to content

[BUG] ZeroDivisionError when fitting BPR on small samples #675

@jonibr22

Description

@jonibr22

Description

This problem occurs when i use a small dataset to fit my BPR model. For example, I build a dataset of 3 users, 3 items, where each user has interacted with at least 2 items. The matrix is kinda like this:

   [[1, 1, 0],
    [1, 0, 1],
    [0, 1, 1]]

When i fit the BPR model using that dataset, it throws a ZeroDivisionError exception.

I tried to analyze the cause, and seems that in the fit method, self._fit_sgd returns skipped that also equals to len(user_ids), which makes the calculation (100.0 * correct / (len(user_ids) - skipped) be a zero division operation.

(See the source in /cornac/models/bpr/recom_bpr.pyx).

In which platform does it happen?

Any platform, but in my case, i use cornac-2.3.5 on Google Colab (Python 3.12)

How do we replicate the issue?

Run this code, it throws ZeroDivisionError: float division by zero

from cornac.models import BPR
from cornac.data import Dataset
model = BPR()
mat = Dataset.from_uir([(1,1,1),(1,2,1),(2,1,1),(2,3,1),(3,2,1),(3,3,1)])
model.fit(mat)

Expected behavior (i.e. solution)

If my dataset were valid, it would be nice to have a logic that handles len(user_ids) == skipped in the fit method. If not, an additional note on the documentation would be helpful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions