Getting different output from same code each time
Used this article to help use the implicit package.
However, every time I run my code, I get a different set of recommendations. It seems like there might be an issue with the seed, so I tried setting random.seed(1), but the problem still persists. What can I do to make sure I get the same result each time?
alpha_val = 15
model = implicit.als.AlternatingLeastSquares(factors=20, regularization=0.1, iterations=20)
data_conf = (sparse_item_account * alpha_val).astype('double')
model.fit(data_conf)
Implicit uses at least two kinds of random number generator.
- C++ random number generator (BPR, LMF Only)
- Numpy random number generator
Numpy rng can be fixed using np.random.seed(), but I don't know how to fix c++ random number generator...
If you really need the seed to be fixed, try setting the number of threads to one. I am not positive how this ALS implementation works internally, but BPR uses the numpy random to seed c++ random generators -one per thread. This makes the generated number sequence fixed, which makes the results fixed as long as the same number is used for the same part of the matrix, which can be only guaranteed if there is a single thread - otherwise, the execution order is unknown and results can still vary.