我正在尝试通过本教程学习如何使用 Elmo 嵌入:
https://github.com/allenai/allennlp/blob/master/tutorials/how_to/elmo.md
我特别尝试使用如下所述的交互模式:
$ ipython
> from allennlp.commands.elmo import ElmoEmbedder
> elmo = ElmoEmbedder()
> tokens = ["I", "ate", "an", "apple", "for", "breakfast"]
> vectors = elmo.embed_sentence(tokens)
> assert(len(vectors) == 3) # one for each layer in the ELMo output
> assert(len(vectors[0]) == len(tokens)) # the vector elements
correspond with the input tokens
> import scipy
> vectors2 = elmo.embed_sentence(["I", "ate", "a", "carrot", "for",
"breakfast"])
> scipy.spatial.distance.cosine(vectors[2][3], vectors2[2][3]) # cosine
distance between "apple" and "carrot" in the last layer
0.18020617961883545
我的总体问题是如何确保在原始 5.5B 集上使用预训练的 elmo 模型(在此处描述:https ://allennlp.org/elmo )?
我不太明白为什么我们必须调用“断言”或为什么我们在向量输出上使用 [2][3] 索引。
我的最终目的是平均所有词嵌入以获得句子嵌入,所以我想确保我做对了!
感谢您的耐心等待,因为我对这一切都很陌生。