Skip to content

[BUG] NeuMF - PyTorch backend ignores num_factors, causing assertion failure #678

@Gabriel-Kissin

Description

@Gabriel-Kissin

Description

When using the PyTorch backend to run the NeuMF model, Cornac ignores the num_factors argument provided during model initialisation.
As a result, the PyTorch NeuMF model always receives the default num_factors=8, even when the user explicitly sets a different value.

This leads to an assertion failure in backend_pt.py#L140 on assert layers[-1] == num_factors. As layers[-1] is the user-provided layers, but num_factors is from the default 8, not the value given by the user.

How do we replicate the issue?

model = cornac.models.NeuMF(
    num_factors=11,
    layers=(64, 16, 8, 11),
    backend="pytorch",
)
model._build_model_pt()    # AssertionError 

Cause of the problem

Looks to me that this is in (1) recom_neumf.py#L263

        model = NeuMF(
            num_users=self.num_users,
            num_items=self.num_items,
            layers=self.layers,
            act_fn=self.act_fn,
        )

which instantiates the class in (2) backend_pt.py#L123

class NeuMF(nn.Module):
    def __init__(
        self,
        num_users: int,
        num_items: int,
        num_factors: int = 8,
        layers=(64, 32, 16, 8),
        act_fn="relu",
    ):

Note that the class in (2) accepts param num_factors with default of 8; however this param is not passed at initialisation in (1).

Proposed fix

        model = NeuMF(
            num_users=self.num_users,
            num_items=self.num_items,
            layers=self.layers,
            num_factors=self.num_factors,  # add this!
            act_fn=self.act_fn,
        )

My other suggestion would be to have a more helpful error message which displays what layers and num_factors are

Workaround

Use tensorflow backend - this correctly uses num_factors here.

model = cornac.models.NeuMF(
    num_factors=11,
    layers=(64, 16, 8, 11),
    backend="tensorflow",
)
model._build_model_tf()

should work though I've not tested it.

Note: setting num_factors=None doesn't help with pytorch backend, as the None doesn't get passed either.

Thanks for the great package!

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