Python 用ELMo嵌入段落

Python 用ELMo嵌入段落,python,tensorflow,nlp,tensorflow-hub,elmo,Python,Tensorflow,Nlp,Tensorflow Hub,Elmo,我试图理解如何为ELMo矢量化准备段落 本教程仅演示如何同时嵌入多个句子/单词 例如 据我所知,这将返回2个向量,每个向量代表一个给定的句子。 我将如何准备输入数据,以矢量化包含多个句子的整个段落。请注意,我希望使用自己的预处理 可以这样做吗 sentences = [["<s>" "the", "cat", "is", "on", "the", "mat", ".", "</s>", "<s>", "dogs", "are",

我试图理解如何为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

我认为你不需要在第二句话中添加额外的填充
,因为
顺序
会解决这个问题

更新

据我所知,这将返回2个向量,每个向量代表一个给定的句子

不,这将为每个单词、每个句子返回一个向量。如果你想让整段文字成为上下文(每个单词),只需将其改为


我更感兴趣的是elmo如何使用我的输入数据(令牌)。整个段落可以像我的第二个例子那样表示吗?段落中的单词和句子是否正确地上下文化?ELMo是否关心它收到的是一句话还是一整段话?是的,从工程学的角度来看,这段话可以是上下文。但请注意,它将消耗更多内存,并且难以扩展更长的上下文。不管这是不是一个好主意,你可能想要尝试。你可能想要使用,这是由ELMo作者写的。我发现它比tf模块更容易使用。我想你应该像你那样标记句子。然后平均一个句子中的所有单词向量。然后平均所有的句子向量。思想?
sentences = [["the", "cat", "is", "on", "the", "mat", ".", 
              "dogs", "are", "in", "the", "fog", "."]]
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]
sentences = [["the", "cat", "is", "on", "the", "mat", "dogs", "are", "in", "the", "fog"]]
...
"sequence_len": [11]