-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Added random_seed parameter to make LsiModel reproducible #3194
Added random_seed parameter to make LsiModel reproducible #3194
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good feature and nicely crafted PR.
I left some code style nitpicks in the comments. I realize some of the existing code may not follow this code style (LSI is the oldest part of Gensim) but please try to follow / update the code style where you can. Thanks!
Looks good to me. Let's wait for @mpenkov 's blessing & merge. |
@mpenkov and @piskvorky : A gentle reminder |
Approved by me; but @mpenkov is busy, thanks for your patience. |
@@ -411,7 +425,8 @@ def __init__( | |||
|
|||
self.docs_processed = 0 | |||
self.projection = Projection( | |||
self.num_terms, self.num_topics, power_iters=self.power_iters, extra_dims=self.extra_samples, dtype=dtype | |||
self.num_terms, self.num_topics, power_iters=self.power_iters, | |||
extra_dims=self.extra_samples, dtype=dtype, random_seed=self.random_seed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra_dims=self.extra_samples, dtype=dtype, random_seed=self.random_seed | |
extra_dims=self.extra_samples, dtype=dtype, random_seed=self.random_seed, |
@mpenkov was good merge for 4.1.0, seems we have missed this PR: https://github.com/RaRe-Technologies/gensim/milestone/5 Do we merge now or anything missing? |
Yeah, looks like we missed this PR. I'll merge it in now. Do we want to do 4.1.3 (or 4.2.0) or can that wait till later? |
I think the version decision can wait – 4.1.3devX is fine for now. Thanks! |
Finally merged. Thank you @parashardhapola !! |
The results from multiple runs of LsiModel on same input data produces different results.
This is primarily due to the random gaussian matrix creation step in
stochastic_svd
function.Hence, it is hard to setup a reproducible pipeline without resorting to changing global randomness controlling methods.
What was done in this PR :
numpy.random.RandomState
) was created in thestochastic_svd
function. This random state can be seeded by a user provided parameter (random_seed
). The gaussian matrices are drawn using this random state.random_seed
parameter was exposed in calls tostochastic_svd
fromLsiModel.add_documents
andProjection.__init__
LsiModel
andProjection
classes.Projection
fromLsiModel
methods (__init__
andadd_documents
) now expose therandom_seed
parameterrandom_seed
inProjection
,LsiModel
andstochastic_svd
was set to None. This means the current behavior is persevered by default.Overall, the above changes allows relaying a 'random_seed' from LsiModel class to downstream calls. The changes suggested in this PR are highly localized and are not expected to have any global impact.
With
random_seed
parameter introduced in this PR: