'AttributeError: The vocab attribute was removed from KeyedVector in Gensim 4.0.0

AttributeError Traceback (most recent call last) ~/Desktop/implimentaion/train.py in 31 use_w2v = True 32 ---> 33 train_df, embeddings = make_w2v_embeddings(train_df, embedding_dim=embedding_dim, empty_w2v=not use_w2v) 34 35 # Split to train validation

~/Desktop/implimentaion/util.py in make_w2v_embeddings(df, embedding_dim, empty_w2v) 90 91 # If a word is missing from word2vec model. ---> 92 if word not in word2vec.vocab: 93 if word not in vocabs_not_w2v: 94 vocabs_not_w2v_cnt += 1

~/opt/anaconda3/lib/python3.8/site-packages/gensim/models/keyedvectors.py in vocab(self) 643 @property 644 def vocab(self): --> 645 raise AttributeError( 646 "The vocab attribute was removed from KeyedVector in Gensim 4.0.0.\n" 647 "Use KeyedVector's .key_to_index dict, .index_to_key list, and methods "

AttributeError: The vocab attribute was removed from KeyedVector in Gensim 4.0.0. Use KeyedVector's .key_to_index dict, .index_to_key list, and methods .get_vecattr(key, attr) and .set_vecattr(key, attr, new_val) instead. See https://github.com/RaRe-Technologies/gensim/wiki/Migrating-from-Gensim-3.x-to-4



Solution 1:[1]

As the error suggests, "See https://github.com/RaRe-Technologies/gensim/wiki/Migrating-from-Gensim-3.x-to-4".

That page explains how to adapt older code to work with gensim-4.0.0 & higher Gensim versions.

(You will want to change your ~/Desktop/implimentaion/util.py file to no longer use the .vocab property to test if a word is in a model. The fix for the particular case shown in your error message may be as simple as changing if word not in word2vec.vocab to if word not in word2vec.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gojomo