与word2vec的余弦相似度

时间:2018-05-17 16:13:44

标签: scikit-learn nlp word2vec gensim cosine-similarity

我加载了一个word2vec格式文件,我想计算向量之间的相似性,但我不知道这个问题意味着什么。

from gensim.models import Word2Vec
from sklearn.metrics.pairwise import cosine_similarity
from gensim.models import KeyedVectors
import numpy as np

model = KeyedVectors.load_word2vec_format('it-vectors.100.5.50.w2v')

similarities = cosine_similarity(model.vectors)


---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
<ipython-input-54-1d4e62f55ebf> in <module>()
----> 1 similarities = cosine_similarity(model.vectors)

/usr/local/lib/python3.5/dist-packages/sklearn/metrics/pairwise.py in cosine_similarity(X, Y, dense_output)
    923         Y_normalized = normalize(Y, copy=True)
    924 
--> 925     K = safe_sparse_dot(X_normalized, Y_normalized.T, dense_output=dense_output)
    926 
    927     return K

/usr/local/lib/python3.5/dist-packages/sklearn/utils/extmath.py in safe_sparse_dot(a, b, dense_output)
    138         return ret
    139     else:
--> 140         return np.dot(a, b)
    141 
    142 

MemoryError: 

这意味着什么? 谢谢!

1 个答案:

答案 0 :(得分:4)

MemoryError表示没有足够的内存来完成操作。

'it-vectors.100.5.50.w2v'设置了多少个向量?

请注意cosine_similarity()创建一个(n x n)结果矩阵。因此,如果您的集合中有100,000个向量,则需要一个大小为的结果数组:

100,000^2 * 4 bytes/float = 40GB

你有那么多可寻址的记忆吗?