Python 了解ELMo';s演示文稿的数量

Python 了解ELMo';s演示文稿的数量,python,nlp,pytorch,allennlp,elmo,Python,Nlp,Pytorch,Allennlp,Elmo,我正在尝试我的手在埃尔莫简单地使用它作为一个更大的PyTorch模型的一部分。给出了一个基本的例子 这是一个torch.nn.Module子类,用于计算任意数量的ELMo 表示,并为每个表示引入可训练的标量权重。对于 例如,此代码段计算两层表示(如下所示) 在我们论文中的SNLI和团队模型中): 我的问题涉及“陈述”。您能将其与普通word2vec输出层进行比较吗?您可以选择返回多少ELMo(增加n维),但是这些生成的表示之间有什么区别,它们的典型用途是什么 为了让您了解一下,对于上面的代码,e

我正在尝试我的手在埃尔莫简单地使用它作为一个更大的PyTorch模型的一部分。给出了一个基本的例子

这是一个torch.nn.Module子类,用于计算任意数量的ELMo 表示,并为每个表示引入可训练的标量权重。对于 例如,此代码段计算两层表示(如下所示) 在我们论文中的SNLI和团队模型中):

我的问题涉及“陈述”。您能将其与普通word2vec输出层进行比较吗?您可以选择返回多少ELMo(增加n维),但是这些生成的表示之间有什么区别,它们的典型用途是什么

为了让您了解一下,对于上面的代码,
embeddings['elmo_representations']
返回两个项目(两个表示层)的列表,但它们是相同的

简言之,如何定义ELMo中的“表示”

见本手册第3.2节

ELMo是biLM中中间层表示的特定于任务的组合。对于每个令牌,L层biLM计算一组2L+1表示

前面第3.1节中提到:

最近最先进的神经语言模型计算与上下文无关的令牌表示(通过令牌嵌入或字符上的CNN),然后将其通过前向LSTM的L层。在每个位置k,每个LSTM层输出上下文相关表示。顶层LSTM输出用于预测具有Softmax层的下一个令牌

为了回答您的问题,这些表示是基于LSTM的上下文相关表示

from allennlp.modules.elmo import Elmo, batch_to_ids

options_file = "https://s3-us-west-2.amazonaws.com/allennlp/models/elmo/2x4096_512_2048cnn_2xhighway/elmo_2x4096_512_2048cnn_2xhighway_options.json"
weight_file = "https://s3-us-west-2.amazonaws.com/allennlp/models/elmo/2x4096_512_2048cnn_2xhighway/elmo_2x4096_512_2048cnn_2xhighway_weights.hdf5"

# Compute two different representation for each token.
# Each representation is a linear weighted combination for the
# 3 layers in ELMo (i.e., charcnn, the outputs of the two BiLSTM))
elmo = Elmo(options_file, weight_file, 2, dropout=0)

# use batch_to_ids to convert sentences to character ids
sentences = [['First', 'sentence', '.'], ['Another', '.']]
character_ids = batch_to_ids(sentences)

embeddings = elmo(character_ids)

# embeddings['elmo_representations'] is length two list of tensors.
# Each element contains one layer of ELMo representations with shape
# (2, 3, 1024).
#   2    - the batch size
#   3    - the sequence length of the batch
#   1024 - the length of each ELMo vector