Lester
Lester

Reputation: 33

LightFM user / item producing Nan embeddings

I am attempting to create cold-start recommendations using the LightFM library in python. https://github.com/lyst/lightfm

This works as expected for collaborative filtering, without user and item features i.e:

from lightfm import LightFM
interaction_matrix

<322139x42715 sparse matrix of type '<type 'numpy.float32'>'
    with 4571208 stored elements in COOrdinate format>

model = LightFM(no_components=50)
model.fit(interaction_matrix, epochs=1, num_threads=32)
predictions = model.predict(12, np.arange(250), num_threads=32)

This produces predictions fine. However when I add:

members_features, item_features

(<322139x2790 sparse matrix of type '<type 'numpy.float32'>'
    with 19840665 stored elements in Compressed Sparse Row format>,
 <42715x2790 sparse matrix of type '<type 'numpy.float32'>'
    with 355006 stored elements in Compressed Sparse Row format>)

model2 = LightFM(no_components=100, loss='warp', item_alpha=0.001, user_alpha=0.001)

model2.fit(interaction_matrix, user_features=members_features, item_features=item_features, sample_weight=None, \
                  verbose=True, epochs=2, num_threads=32)

I get Nan's for the user and item embeddings.

model2.item_embeddings

array([[ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       ..., 
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan]], dtype=float32)

Upvotes: 0

Views: 1188

Answers (1)

Maciej Kula
Maciej Kula

Reputation: 889

You should try updating to LightFM 1.12 (via pip install lightfm==1.12). This version fixes a number of numerical instability issues that may lead to the results you are seeing.

If you are interested in the gory details, you can have a look at this Github issue.

Upvotes: 1

Related Questions