question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

QUESTION:lightFM,predict()

See original GitHub issue

Hi,Maciej! I would like to ask how predict() is calculated? I think it should be like this, like user 1 for item 1:

user_biases = model.get_user_representations()[0]
user_embeddings = model.get_user_representations()[1]
item_biases = model.get_item_representations()[0]
item_embeddings = model.get_item_representations()[1]
user1 = user_embeddings[0]
item1 = item_embeddings[0]
score = u1.dot(i1.T)
ub1 = user_biases[0]
ib1 = item_biases[0]
result = score + ub1 + ib1

However, the calculation result is inconsistent with predict(1,[1]), please answer the wrong place, thank you for your reply!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
FankLicommented, Nov 22, 2018

Not all of the values match exactly. How could that be?

# find the best model
model_bpr_ult = model_bpr[np.argmax(bpr_map_test)]

# reformulate the interaction matrix. 
user_biases = model_bpr_ult.get_user_representations()[0]
user_embeddings = model_bpr_ult.get_user_representations()[1]
item_biases = model_bpr_ult.get_item_representations()[0]
item_embeddings = model_bpr_ult.get_item_representations()[1]
user1 = user_embeddings[0]
item1 = item_embeddings
score = np.matmul(user1, item1.transpose())
ub1 = user_biases[0]
ib1 = item_biases

# test on user_1
result = score + ub1 + ib1
predict = model_bpr_ult.predict(0, list(range(len(item1))))

Counter(np.argsort(result.round(3)) == np.argsort(predict.round(3)))

Counter({False: 340, True: 13201})

for example: There are 943’s user and 1682’2 item in movielens100k, model.predict(943,[1]) or model.predict(1,[1682]) will error. So, model.predict(942,[1]) is user943 to item0.

1reaction
maciejkulacommented, Aug 22, 2018

This passes, using your code from above, fixing variables u1 -> user1 and i1 to item1:

    import numpy as np
    from lightfm import LightFM, datasets
    data = datasets.fetch_movielens()

    model = LightFM(learning_rate=0.05, loss="bpr", random_state=SEED)
    model.fit_partial(data['train'], epochs=10)

    user_biases = model.get_user_representations()[0]
    user_embeddings = model.get_user_representations()[1]
    item_biases = model.get_item_representations()[0]
    item_embeddings = model.get_item_representations()[1]
    user1 = user_embeddings[1]
    item1 = item_embeddings[1]
    score = user1.dot(item1.T)
    ub1 = user_biases[1]
    ib1 = item_biases[1]

    result = score + ub1 + ib1
    predict = model.predict(1, [1])

    assert np.allclose(result, predict[0])
Read more comments on GitHub >

github_iconTop Results From Across the Web

predict new user using lightfm - python - Stack Overflow
I was having the same problem, What I did was. Created a user_features matrix (based on their preferences) using Dataset class
Read more >
Item cold-start: recommending StackExchange questions
A hybrid model . We can do much better by employing LightFM's hybrid model capabilities. The StackExchange data comes with content information...
Read more >
How I would explain building “LightFM Hybrid Recommenders ...
Worked example to build user/item features to tackle the cold start problem and predict ratings for new users. Note: The 5-yr old must...
Read more >
lightfm-rec/Lobby - Gitter
https://stackoverflow.com/questions/50086764/python-lightfm-valueerror-the-user- ... When I call model.predict() passing in the user ID and the item ID, ...
Read more >
LightFm - replicate precision@k score with predict vs ...
Ask Question. Asked 2 years, 1 month ago. Modified 2 years ago ... LightFm has two methods to predict: predict() and predict_rank() ....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found