-
-
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
Implement shrink_windows
argument for Word2Vec.
#3169
Conversation
Co-Authored-By: Mathis Demay <mathis.demay.md@gmail.com>
ba747a7
to
f323b68
Compare
f323b68
to
77698cd
Compare
Tests are failing on old models' reloading due to the added |
In general the right place to path older models, upon load, to ensure they have all currently-required properties is Separately: it occurs to me that it should be checked that the new option will work in |
Thank you for clarifying things about old models' reloading; it is now fixed. I also deployed the parameter to On my Linux machine all tests passed, although I am wondering whether it would be appropriate to tweak those as well, so that some test models may also run with the non-default |
reduced_windows
argument for Word2Vec.shrink_windows
argument for Word2Vec.
Yes, it'd be good to ensure at least one test method, for each class, tries the non-default If there were an easy way to test that it's having the desired effect, that'd be great - but it's a subtle thing to test. Its effects on final vectors aren't easily characterizable. The tangible external effect will be: training runs about twice as long. But, running some training twice, once each way, timing it, and asserting one was "around" twice as long seems a bit flaky & low-value to me, compared to the amount of runtime it'd add to the automated testing. Because of that, I think careful review of the source for intent, along with the old path definitely still working as before, and the new path not erroring, is enough for the unit-testing of this. It might make sense to mark the doc-comment for such a new, niche feature as: "New in 4.1. Experimental." |
* `c.reduced_windows[:] = 0` syntax is not supported; as a consequence, all zero-value assignments due to `shrink_windows=False` have been rewritten as `for` loops. * Those changes have now been tested (cf. next commit). * NOTE: another way to proceed could be to initialize the `reduced_windows` array with zeros as values; it would then only be altered when `shrink_windows=True`.
Alright, I just pushed three additional commits: (1) Backend changes; as it turns out, the previously-suggested use of (2) Additional tests, following @gojomo's recommendations. I actually added 4 tests to both Word2Vec, Doc2Vec and FastText so as to test all underlying code branches (cbow and skipgram, on an iterable corpus or on a corpus file). If you would rather remove some of these to limit the extra testing time, feel free to trim some off. (3) As you suggested, I added the "experimental" status line to the parameter's docstring in all three models. On the side, I note that:
|
This is desirable, thanks. The for-loop is clearer and I see you also made it more efficient (less looping). |
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.
Looking good! I'm late to the party, so I see most of the issues have been ironed out. I did manage to find a few nitpicks and left you some comments. Please have a look.
@@ -365,8 +365,12 @@ def train_document_dbow(model, doc_words, doctag_indexes, alpha, work=None, | |||
|
|||
if c.train_words: | |||
# single randint() call avoids a big thread-synchronization slowdown | |||
for i, item in enumerate(model.random.randint(0, c.window, c.document_len)): | |||
c.reduced_windows[i] = item | |||
if model.shrink_windows: |
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.
This is a bit repetitive.
Wouldn't it be better to define a model.initialize_reduced_windows method that does this, and call it when needed?
def initialize_reduced_windows(self, ...):
if model.shrink_windows:
for i, item in ...:
...
else:
...
That way, if we find a logic error in the initialization code, we don't have to remember to fix it in a half a dozen other places (e.g. https://github.com/RaRe-Technologies/gensim/pull/3169/files#r649243075).
We should probably also do a bounds check on c.reduced_windows to ensure that it's at least document_len elements long.
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.
I agree with you on the potential for refactored code that would ease future maintenance and updates. That being said, I actually built on the existing, repetitive code, so in my humble opinion this could be handled as a distinct PR (this one would add a feature, while a second one would enhance the validated, working code).
I also think that your suggestion of a model.initialize_reduced_windows
is not entirely suitable here:
If I am not mistaken, in this code, c
is not model
: it is a special C structure (of a different class depending on the trained model class: word2vec, doc2vec or fasttext), and the reduced_windows
PyArray is initialized to have the proper size, so there should not be a need to do a bounds check.
I do not know how we could implement a common method (or function) that would also fill the array with either deterministic or random values; but I do agree with you that it could be worth it.
To be honest I lack experience with cython, so that I do not feel entirely at ease with diving into this refactoring. I am willing to give it a try at some point if you want, but would prefer it to be a distinct effort (and PR) from the current one - partly because Mathis and I are pushing the shrink_windows
feature due to our needing it for a current research project at work, while contributing to refactoring the code base would be something I would (willingly) do on my time off.
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.
Alas, there's a lot of pre-existing cut & paste duplication between all these related algorithms & modes that could be refactored. I think that'd be OK to note-and-defer-for-later, as either an issue or a FIXME comment in the code.
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.
OK, we can deal with this separately.
@@ -497,12 +497,12 @@ def test_sg_hs_training(self): | |||
overlap_count, 2, | |||
"only %i overlap in expected %s & actual %s" % (overlap_count, expected_sims_words, sims_gensim_words)) | |||
|
|||
def test_sg_hs_training_fromfile(self): | |||
def test_sg_hs_training_fromfile(self, shrink_windows=True): |
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.
https://docs.pytest.org/en/6.2.x/example/parametrize.html
def test_sg_hs_training_fromfile(self, shrink_windows=True): | |
@pytest.mark.parametrize('shrink_windows', [True, False]) | |
def test_sg_hs_training_fromfile(self, shrink_windows): |
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.
I pushed two commits making use of that feature. The first one only applied to the FastText tests, as they were the one you attached this review to. The second one applies to the other two test sets (wor2vec and doc2vec); if you feel like the latter should not have been changed, we can discard this commit.
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.
Update: actually, pytest.mark.parametrize
does not work as expected on methods (although it does on functions). I will try to find a way to make it work, otherwise I will undo the last two commits.
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.
Oh, in fact it probably will not work due to the test classes being unittest.TestCase
subclasses.
https://docs.pytest.org/en/6.2.x/unittest.html
The following pytest features do not work [in unittest.TestCase subclasses], and probably never will due to different design philosophies: Fixtures (except for autouse fixtures, see below); Parametrization; Custom hooks;
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.
I am revoking the last two commits as a consequence.
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.
Thanks for pointing that out. I didn't know pytest parameterization is incompatible with unittest.TestCase.
Luckily, in this particular case, the unittest.TestCase class isn't doing anything. I'll get parameterization working in a separate commit.
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.
Well, we both learned something :)
3481607
to
49cf50e
Compare
Merged. Thank you @M-Demay ! |
The repo wasn't accepting maintainer commits, so I'm taking care of this here.
Fixes #3159.
The idea is to allow the user to use a window with fixed-size, contrary to the current default behaviour which is to pick the window size in [1,
window
] uniformly at random. We want this alternative behaviour for a project.We did it by adding a boolean parameter
reduced_windows
. For compatibility, we put it at the end of the arguments list, default toFalse
.The user can set it directly in the constructor of the class
Word2Vec
, or at any further step (for example, to retrain the model in a second step), since we propagated it to the downstream calls.