我正在尝试了解如何为 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/