How should I modify the code `X = model[model.wv.vocab]` with an AttributeError: The vocab attribute was removed from KeyedVector in Gensim 4.0.0

Viewed 17

I am using gensim word2vec package in python with the following code:

model = Word2Vec(window = 10, sg = 1, hs = 0,
                 negative = 10, # for negative sampling
                 alpha=0.03, min_alpha=0.0007,
                 seed = 14)
model.build_vocab(purchases_train, progress_per=200)
model.train(purchases_train, total_examples = model.corpus_count,
            epochs=10, report_delay=1)
model.init_sims(replace=True)
X = model[model.wv.vocab]

And an error occurs: AttributeError: The vocab attribute was removed from KeyedVector in Gensim 4.0.0.

How should I modify the code X = model[model.wv.vocab] to make it eligible for Gensim 4.0.0.?

1 Answers

There's a project help page for converting older code to work with Gensim changes in Gensim 4.0+ at: https://github.com/RaRe-Technologies/gensim/wiki/Migrating-from-Gensim-3.x-to-4

Before Gensim 4.0, the .vocab property used to be a dict with known-word keys and values that were specialized objects of type Vocab, containing info about that word, such as occurrence counts and which position (slot) in an all-vectors array held that word. In Gensim 4.0 and after, a more compact & simple .key_to_index dict instead just stores the word->slot mapping, without the overhead of specialized Vocab objects, with other word info stored differently.

Because here, you're just using .vocab to get the list of keys to do a multi-index lookup, the most literally-similar update would be to use .key_to_index instead exactly where you used to use .vocab: it's got all the same keys you needed.

But, that's probably not the best approach – as the original code, even when it worked, wasn't itself a great approach.

If you really just want a indexable object from which you can read the vectors, where each slot has one of the model vectors, that already exists inside the model, in its .vectors array. You should probably just use that directly:

X = model.wv.vectors

As long as you're not modifying the rows of X, this will get what you want instantly, without any copy/lookup-of-every-key.

Separately, your shown training parameters show some common mistakes that suggest you may be learning from bad tutorials online. Specifically, the values alpha=0.03, min_alpha=0.0007 make little sense - most users don't need to change the defaults, those that might want to should pick them via some optimization process rather than picking those arbitrary values. If you see an online example using those values without explaining why they were chosen over the default, the author of that example probably barely understood what they were writing.

Related