Python >> Tutoriel Python >  >> Python Tag >> Gensim

gensim word2vec :trouver le nombre de mots dans le vocabulaire

Le vocabulaire est dans le vocab champ du wv du modèle Word2Vec propriété, comme un dictionnaire, les clés étant chaque jeton (mot). C'est donc juste le Python habituel pour obtenir la longueur d'un dictionnaire :

len(w2v_model.wv.vocab)

(Dans les anciennes versions de gensim avant 0.13, vocab apparaissait directement sur le modèle. Donc, vous utiliseriez w2v_model.vocab au lieu de w2v_model.wv.vocab .)


Une autre façon d'obtenir la taille du vocabulaire est à partir de la matrice d'intégration elle-même comme dans :

In [33]: from gensim.models import Word2Vec

# load the pretrained model
In [34]: model = Word2Vec.load(pretrained_model)

# get the shape of embedding matrix    
In [35]: model.wv.vectors.shape
Out[35]: (662109, 300)

# `vocabulary_size` is just the number of rows (i.e. axis 0)
In [36]: model.wv.vectors.shape[0]
Out[36]: 662109