如何从 gensim W2V 模型中获取所有向量的转储?
How to get a dump of all vectors from a gensim W2V model?
使用 KeyedVectors 对象,我可以获得 W2V 向量,给定一个词,就像这样。
from gensim.models import KeyedVectors
model = KeyedVectors.load('vectors.kv')
model.get_vector('example') # output => [0.12, 0.41, ..., 0.92]
对于模型中包含的每个项(键),我如何做同样的事情?
请注意,这 不一定 是 KeyedVectors 对象,它也可以是 Word2Vec 对象。
编辑 - 感谢 gojomo:
vector_dct = {}
for word in kv_model.index2word:
vector_dct[word] = kv_model.get_vector(word)
df = pd.DataFrame(vector_dct).T
for word in kv_model.index2word: # changes to kv_model.index_to_key in gensim-4.0.0
kv_model.get_vector(word)
使用 KeyedVectors 对象,我可以获得 W2V 向量,给定一个词,就像这样。
from gensim.models import KeyedVectors
model = KeyedVectors.load('vectors.kv')
model.get_vector('example') # output => [0.12, 0.41, ..., 0.92]
对于模型中包含的每个项(键),我如何做同样的事情?
请注意,这 不一定 是 KeyedVectors 对象,它也可以是 Word2Vec 对象。
编辑 - 感谢 gojomo:
vector_dct = {}
for word in kv_model.index2word:
vector_dct[word] = kv_model.get_vector(word)
df = pd.DataFrame(vector_dct).T
for word in kv_model.index2word: # changes to kv_model.index_to_key in gensim-4.0.0
kv_model.get_vector(word)