How to save word2vec vectors in a simple text file?

1,041 views
Skip to first unread message

santosh.b...@gmail.com

unread,
Apr 19, 2021, 10:23:10 AM4/19/21
to Gensim
Hello Community, 

I was wondering if there was a way to export the keyed word2vec vectors previously saved in binary format to a simple text file in the following format:
sausage 0.1234 -0.5555 1.4149
(i.e. word, space, float, space, float, space float … newline).

Thanks!
sbs

santosh.b...@gmail.com

unread,
Apr 19, 2021, 11:28:57 AM4/19/21
to Gensim
Oh, got it. Was able to do it as follows:

w2v.save_word2vec_format('corpus_vectors.txt', binary= False, write_header= False)

Gordon Mohr

unread,
Apr 19, 2021, 3:28:25 PM4/19/21
to Gensim
Note that by using `write_header=False`, there's no initial header line declaring the count of vectors included - that makes the text file unlike the format used by Google's original `word2vec.c` tool, but like that used by GLoVe. 

To re-read such vectors into Gensim requires the non-default `no_header=True` argument to `load_word2vec_format()`, and takes about twice as much time/IO, because one whole extra pass is required to discover the count of vectors included. 

- Gordon

Tedo Vrbanec

unread,
Apr 19, 2021, 3:28:47 PM4/19/21
to Gensim
Hm.
I've got:
AttributeError: 'Word2Vec' object has no attribute 'save_word2vec_format'

Tedo Vrbanec

unread,
Apr 19, 2021, 3:43:15 PM4/19/21
to Gensim
You need w2v.wv.save_word2vec_format('corpus_vectors.txt', binary= False, write_header= False)
Reply all
Reply to author
Forward
0 new messages