python - 使用 ELMo 嵌入段落

标签 python tensorflow nlp tensorflow-hub elmo

我正在尝试了解如何为 ELMo 向量化准备段落。

docs一次只显示如何嵌入多个句子/单词。

例如。

sentences = [["the", "cat", "is", "on", "the", "mat"],
         ["dogs", "are", "in", "the", "fog", ""]]
elmo(
     inputs={
          "tokens": sentences,
          "sequence_len": [6, 5]
            },
     signature="tokens",
     as_dict=True
    )["elmo"]

据我所知,这将返回 2 个向量,每个向量代表一个给定的句子。 我将如何准备输入数据以矢量化包含多个句子的整个段落。请注意,我想使用自己的预处理。

这样可以吗?

sentences = [["<s>" "the", "cat", "is", "on", "the", "mat", ".", "</s>", 
              "<s>", "dogs", "are", "in", "the", "fog", ".", "</s>"]]

或者可能是这样?

sentences = [["the", "cat", "is", "on", "the", "mat", ".", 
              "dogs", "are", "in", "the", "fog", "."]]

最佳答案

ELMo 生成上下文词向量。因此,对应于一个词的词向量是该词和它出现在的上下文(例如句子)的函数。

就像您在文档中的示例一样,您希望您的段落是一个句子列表,也就是标记列表。所以你的第二个例子。要获得这种格式,您可以使用 spacy tokenizer

import spacy

# you need to install the language model first. See spacy docs.
nlp = spacy.load('en_core_web_sm')

text = "The cat is on the mat. Dogs are in the fog."
toks = nlp(text)
sentences = [[w.text for w in s] for s in toks.sents]

我认为您不需要在第二个句子中使用额外的填充 "",因为 sequence_len 会处理这个问题。

更新:

As I understand, this will return 2 vectors each representing a given sentence

不,这将为每个句子中的每个单词返回一个向量。如果您希望整个段落成为上下文(对于每个单词),只需将其更改为

sentences = [["the", "cat", "is", "on", "the", "mat", "dogs", "are", "in", "the", "fog"]]

...
"sequence_len": [11]

关于python - 使用 ELMo 嵌入段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53570918/

相关文章:

python flask 在 html 页面上显示图像

python - Pandas SQL 等价于 update where group by

tensorflow - 批量训练使用更新总和?或平均更新?

python - "LookupError: Function ` __class__ ` does not exist."使用 tf.function 时

tensorflow - 如何从保存的元图中恢复我的损失?

python - Pytorch-索引错误 : index out of range in self

python - 一个 Django 项目 - 根据域呈现不同的内容

python - 为什么 '\b' 在 Python 中作为句子的最后一个字符看起来像是无效的?

javascript - 从用户查询中提取区域名称

python - 仅获取标记化句子作为 Stanford Core NLP 的输出