Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NaN outputs #80

Closed
yangji9181 opened this issue Feb 7, 2018 · 5 comments
Closed

NaN outputs #80

yangji9181 opened this issue Feb 7, 2018 · 5 comments

Comments

@yangji9181
Copy link

Hi Ben,

It feels like the current code is still suffering this same issue (#19 (comment)):

when I tried to run the 'basic usage' on a few small 100*200 random 0-1 matrixes, the output scores are always nan. Could you try to look into that again?

Thanks.

@yangji9181
Copy link
Author

Here is the exact code I have tried. I am not sure what is causing the problem, and my purpose here is just to make sure your framework works before I deploy it into my experiments.

X=np.random.randint(5, size=(100,200))
X0=coo_matrix(X, dtype=np.float64)
model = implicit.als.AlternatingLeastSquares(factors=50)
model.fit(X0)
print(model.user_factors)
print(model.item_factors)

Thanks!

@benfred
Copy link
Owner

benfred commented Feb 8, 2018

Hmm - this works for me (on both OSX and Ubuntu). What version of implicit are you running? Also what version of python/numpy etc are you running ?

In [1]: import numpy as np

In [2]: from scipy.sparse import coo_matrix

In [3]: import implicit.als

In [4]: X=np.random.randint(5, size=(100,200))
   ...: X0=coo_matrix(X, dtype=np.float64)
   ...: model = implicit.als.AlternatingLeastSquares(factors=50)
   ...: model.fit(X0)
   ...: print(model.user_factors)
   ...: print(model.item_factors)
   ...: 
[[-0.50830597  1.77890754  3.72887993 ...,  5.42687702  0.73224628
   2.95396447]
 [ 4.16104603  3.23049259  1.36413717 ...,  2.49195838  1.41964936
   1.75384963]
 [ 1.88410449  1.15888965  1.82405496 ...,  2.37375712  2.4774456
   0.28280351]
 ..., 
 [ 2.13711357  0.86102092 -0.43549636 ...,  2.66490889  4.2622385
   3.61764646]
 [ 3.21060467  1.86145854  3.29952288 ...,  2.49085307  2.59494758
   0.53273898]
 [ 1.63236988  3.20456839  2.73510027 ...,  2.82707405  2.82944465
   1.39465821]]
[[-0.0089963   0.09548787 -0.03623041 ...,  0.01521569  0.02308306
   0.05071232]
 [-0.00933617 -0.00377901  0.04466964 ...,  0.06146013 -0.00124603
   0.01044547]
 [ 0.00631106  0.01320372  0.03328578 ...,  0.00114081  0.01793537
  -0.07993541]
 ..., 
 [-0.08164594 -0.07154793  0.06172384 ..., -0.02570276  0.06908227
  -0.02065015]
 [-0.00280522  0.04038418  0.05980453 ...,  0.07462172  0.01221859
   0.01574233]
 [-0.00381051 -0.00444142 -0.00367571 ..., -0.00306982 -0.02002124
   0.01943841]]

In [5]: np.isnan(model.user_factors).any()
Out[5]: False

In [6]: np.isnan(model.item_factors).any()
Out[6]: False

@yangji9181
Copy link
Author

Hi Ben,

Thanks for replying. I was using miniconda2, but after I switched back to Mac's default python, it worked fine.

@yangji9181
Copy link
Author

Hi Ben,

By the way, I noticed that the model is prone to overfitting. Besides setting larger regularization term, is there a way to do something like adding a mask on the training data, so that the recovery loss is only computed on non-zero values? I think this might help reducing overfitting too.

Thanks.

@benfred
Copy link
Owner

benfred commented Feb 9, 2018

For the NaN issue - my best guess is that you still have an older version installed with your miniconda2 install.

For overfitting you can either increase the regularization parameter, or alternatively you can decrease the number of iterations you train for.

Its not possible to compute the loss function here without the non-zero values - they are the negative examples, and computing the loss with only positive examples would mean that a model that predicted 1 for all items would score perfectly (if that makes sense). If you want to weight the non-zero values higher, you should just increase the confidence in the Ciu matrix.

@benfred benfred closed this as completed Feb 20, 2018
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

No branches or pull requests

2 participants