I'm trying to understand the PV-DM implementation with averaging in gensim.
In the function train_document_dm
in doc2vec.py
the return value ("errors") of train_cbow_pair
is in the case of averaging (cbow_mean=1
) not divided by the number of input vectors (count
).
According to this explanation there should be a division by the number of documents in the case of averaging the input vectors: word2vec Parameter Learning Explained, equation (23).
Here is the code from 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]