【问题标题】:does doc2vec(gensim) infer_vector needs window-size padded sentence?doc2vec(gensim) infer_vector 需要窗口大小的填充句吗?
【发布时间】:2018-05-29 08:02:10
【问题描述】:

根据原论文Distributed Representations of Sentences and Documents,对看不见的段落的推断可以通过

训练“推理阶段”以获得新的段落向量 D 段落(以前从未见过)通过添加更多列 在 D 和梯度下降 D 同时保持 W, U, b 固定

这个推理阶段可以在 gensim 中由 infer_vector() 完成。 如果我有 window = 5 用于 doc2vec 模型,并尝试推断其某些句子为 len(sentence) < 5 的段落。

如:

model = Doc2Vec(window=5) paragraph = [['I', 'am', 'groot'], ['I', 'am', 'groot', 'I', 'am', 'groot']] model.infer_vector(paragraph)

在这种情况下,我是否应该用特殊的 NULL 词符号预先填充我的推断向量,以便段落中的所有句子长度都应该大于窗口大小?

如:

paragraph = [['I', 'am', 'groot', NULL, NULL], ['I', 'am', 'groot', 'I', 'am', 'groot']]

【问题讨论】:

    标签: gensim doc2vec


    【解决方案1】:

    你永远不需要做任何显式的填充。

    在默认和常见的Doc2Vec 模式下,如果焦点词的任一侧没有足够的上下文,则有效的window 会在该侧缩小以匹配可用的内容。

    (在非默认dm=1, dm_concat=1 模式下,必要时会自动填充。但这种模式会导致模型更大、速度更慢,需要更多数据来训练,并且在任何经过​​验证的设置中其值都不是很清楚。这种模式不太可能获得好的结果,除非是拥有大量数据并且能够修改非默认参数的高级用户。)

    【讨论】:

      【解决方案2】:

      我发现 gensim 在训练和推断阶段都会自动预填充文档。

      gensim.models.doc2vec.train_document_dm_concat

          null_word = model.vocab['\0']
          pre_pad_count = model.window
          post_pad_count = model.window
          padded_document_indexes = (
              (pre_pad_count * [null_word.index])  # pre-padding
              + [word.index for word in word_vocabs if word is not None]  # elide out-of-Vocabulary words
              + (post_pad_count * [null_word.index])  # post-padding
          )
      

      【讨论】:

      • 这只是在慢速、大模型、非默认dm_concat=1 模式下。在其他模式下,没有填充,只是将有效的window 减少到可用的值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      相关资源
      最近更新 更多