我试图通过 gensim 中的平均来理解 PV-DM 的实现。在函数train_document_dm
中doc2vec.py
的返回值(“错误”)train_cbow_pair
是在求平均值的情况下(cbow_mean=1
)不除以输入向量的数量(count
)。根据这个解释,在对输入向量求平均的情况下应该除以文档数:word2vec Parameter Learning Explained, equation (23)。这是来自的代码train_document_dm
:
l1 = np_sum(word_vectors[word2_indexes], axis=0)+np_sum(doctag_vectors[doctag_indexes], axis=0)
count = len(word2_indexes) + len(doctag_indexes)
if model.cbow_mean and count > 1:
l1 /= count
neu1e = train_cbow_pair(model, word, word2_indexes, l1, alpha,
learn_vectors=False, learn_hidden=learn_hidden)
if not model.cbow_mean and count > 1:
neu1e /= count
if learn_doctags:
for i in doctag_indexes:
doctag_vectors[i] += neu1e * doctag_locks[i]
if learn_words:
for i in word2_indexes:
word_vectors[i] += neu1e * word_locks[i]