• 教程 >
  • 序列模型和长短期记忆网络
快捷方式

序列模型和长短期记忆网络

到目前为止,我们已经看到了各种前馈网络。也就是说,网络根本不维护任何状态。这可能不是我们想要的行为。序列模型是 NLP 的核心:它们是模型,其中输入之间存在某种时间上的依赖关系。序列模型的经典示例是用于词性标注的隐马尔可夫模型。另一个示例是条件随机场。

循环神经网络是一种维护某种状态的网络。例如,它的输出可以用作下一个输入的一部分,以便信息可以在网络遍历序列时传播。在 LSTM 的情况下,对于序列中的每个元素,都有一个对应的隐藏状态 \(h_t\),它原则上可以包含来自序列中任意早期的信息。我们可以使用隐藏状态来预测语言模型中的单词、词性标记以及无数其他事物。

Pytorch 中的 LSTM

在进入示例之前,请注意以下几点。Pytorch 的 LSTM 期望所有输入都是 3 维张量。这些张量的轴的语义很重要。第一个轴是序列本身,第二个轴索引小批量中的实例,第三个轴索引输入的元素。我们还没有讨论小批量处理,所以让我们忽略它,并假设我们始终在第二个轴上只有 1 个维度。如果我们想对句子“The cow jumped”运行序列模型,我们的输入应该如下所示:

\[\begin{bmatrix} \overbrace{q_\text{The}}^\text{行向量} \\ q_\text{cow} \\ q_\text{jumped} \end{bmatrix}\]

但请记住,还有一个额外的第二维,大小为 1。

此外,您可以一次遍历一个序列,在这种情况下,第一个轴的大小也为 1。

让我们看一个简单的示例。

# Author: Robert Guthrie

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

torch.manual_seed(1)
<torch._C.Generator object at 0x7fa75abf3950>
lstm = nn.LSTM(3, 3)  # Input dim is 3, output dim is 3
inputs = [torch.randn(1, 3) for _ in range(5)]  # make a sequence of length 5

# initialize the hidden state.
hidden = (torch.randn(1, 1, 3),
          torch.randn(1, 1, 3))
for i in inputs:
    # Step through the sequence one element at a time.
    # after each step, hidden contains the hidden state.
    out, hidden = lstm(i.view(1, 1, -1), hidden)

# alternatively, we can do the entire sequence all at once.
# the first value returned by LSTM is all of the hidden states throughout
# the sequence. the second is just the most recent hidden state
# (compare the last slice of "out" with "hidden" below, they are the same)
# The reason for this is that:
# "out" will give you access to all hidden states in the sequence
# "hidden" will allow you to continue the sequence and backpropagate,
# by passing it as an argument  to the lstm at a later time
# Add the extra 2nd dimension
inputs = torch.cat(inputs).view(len(inputs), 1, -1)
hidden = (torch.randn(1, 1, 3), torch.randn(1, 1, 3))  # clean out hidden state
out, hidden = lstm(inputs, hidden)
print(out)
print(hidden)
tensor([[[-0.0187,  0.1713, -0.2944]],

        [[-0.3521,  0.1026, -0.2971]],

        [[-0.3191,  0.0781, -0.1957]],

        [[-0.1634,  0.0941, -0.1637]],

        [[-0.3368,  0.0959, -0.0538]]], grad_fn=<MkldnnRnnLayerBackward0>)
(tensor([[[-0.3368,  0.0959, -0.0538]]], grad_fn=<StackBackward0>), tensor([[[-0.9825,  0.4715, -0.0633]]], grad_fn=<StackBackward0>))

示例:用于词性标注的 LSTM

在本节中,我们将使用 LSTM 来获取词性标签。我们不会使用 Viterbi 或 Forward-Backward 或类似的东西,但作为一项(具有挑战性的)练习,请读者思考在您了解了正在发生的事情之后如何使用 Viterbi。在本例中,我们还提到了嵌入。如果您不熟悉嵌入,您可以在 此处 阅读有关嵌入的更多信息。

模型如下:令我们的输入句子为 \(w_1, \dots, w_M\),其中 \(w_i \in V\),我们的词汇。另外,令 \(T\) 为我们的标签集,\(y_i\) 为词 \(w_i\) 的标签。用 \(\hat{y}_i\) 表示我们对词 \(w_i\) 的标签的预测。

这是一个结构预测模型,我们的输出是一个序列 \(\hat{y}_1, \dots, \hat{y}_M\),其中 \(\hat{y}_i \in T\)

要进行预测,请在句子上传递一个 LSTM。将时间步 \(i\) 处的隐藏状态表示为 \(h_i\)。另外,为每个标签分配一个唯一的索引(就像我们在词嵌入部分中拥有 word_to_ix 一样)。然后,我们对 \(\hat{y}_i\) 的预测规则为:

\[\hat{y}_i = \text{argmax}_j \ (\log \text{Softmax}(Ah_i + b))_j \]

也就是说,取隐藏状态的仿射映射的对数 softmax,预测的标签是该向量中具有最大值的标签。注意,这立即意味着 \(A\) 的目标空间的维数为 \(|T|\)

准备数据

def prepare_sequence(seq, to_ix):
    idxs = [to_ix[w] for w in seq]
    return torch.tensor(idxs, dtype=torch.long)


training_data = [
    # Tags are: DET - determiner; NN - noun; V - verb
    # For example, the word "The" is a determiner
    ("The dog ate the apple".split(), ["DET", "NN", "V", "DET", "NN"]),
    ("Everybody read that book".split(), ["NN", "V", "DET", "NN"])
]
word_to_ix = {}
# For each words-list (sentence) and tags-list in each tuple of training_data
for sent, tags in training_data:
    for word in sent:
        if word not in word_to_ix:  # word has not been assigned an index yet
            word_to_ix[word] = len(word_to_ix)  # Assign each word with a unique index
print(word_to_ix)
tag_to_ix = {"DET": 0, "NN": 1, "V": 2}  # Assign each tag with a unique index

# These will usually be more like 32 or 64 dimensional.
# We will keep them small, so we can see how the weights change as we train.
EMBEDDING_DIM = 6
HIDDEN_DIM = 6
{'The': 0, 'dog': 1, 'ate': 2, 'the': 3, 'apple': 4, 'Everybody': 5, 'read': 6, 'that': 7, 'book': 8}

创建模型

class LSTMTagger(nn.Module):

    def __init__(self, embedding_dim, hidden_dim, vocab_size, tagset_size):
        super(LSTMTagger, self).__init__()
        self.hidden_dim = hidden_dim

        self.word_embeddings = nn.Embedding(vocab_size, embedding_dim)

        # The LSTM takes word embeddings as inputs, and outputs hidden states
        # with dimensionality hidden_dim.
        self.lstm = nn.LSTM(embedding_dim, hidden_dim)

        # The linear layer that maps from hidden state space to tag space
        self.hidden2tag = nn.Linear(hidden_dim, tagset_size)

    def forward(self, sentence):
        embeds = self.word_embeddings(sentence)
        lstm_out, _ = self.lstm(embeds.view(len(sentence), 1, -1))
        tag_space = self.hidden2tag(lstm_out.view(len(sentence), -1))
        tag_scores = F.log_softmax(tag_space, dim=1)
        return tag_scores

训练模型

model = LSTMTagger(EMBEDDING_DIM, HIDDEN_DIM, len(word_to_ix), len(tag_to_ix))
loss_function = nn.NLLLoss()
optimizer = optim.SGD(model.parameters(), lr=0.1)

# See what the scores are before training
# Note that element i,j of the output is the score for tag j for word i.
# Here we don't need to train, so the code is wrapped in torch.no_grad()
with torch.no_grad():
    inputs = prepare_sequence(training_data[0][0], word_to_ix)
    tag_scores = model(inputs)
    print(tag_scores)

for epoch in range(300):  # again, normally you would NOT do 300 epochs, it is toy data
    for sentence, tags in training_data:
        # Step 1. Remember that Pytorch accumulates gradients.
        # We need to clear them out before each instance
        model.zero_grad()

        # Step 2. Get our inputs ready for the network, that is, turn them into
        # Tensors of word indices.
        sentence_in = prepare_sequence(sentence, word_to_ix)
        targets = prepare_sequence(tags, tag_to_ix)

        # Step 3. Run our forward pass.
        tag_scores = model(sentence_in)

        # Step 4. Compute the loss, gradients, and update the parameters by
        #  calling optimizer.step()
        loss = loss_function(tag_scores, targets)
        loss.backward()
        optimizer.step()

# See what the scores are after training
with torch.no_grad():
    inputs = prepare_sequence(training_data[0][0], word_to_ix)
    tag_scores = model(inputs)

    # The sentence is "the dog ate the apple".  i,j corresponds to score for tag j
    # for word i. The predicted tag is the maximum scoring tag.
    # Here, we can see the predicted sequence below is 0 1 2 0 1
    # since 0 is index of the maximum value of row 1,
    # 1 is the index of maximum value of row 2, etc.
    # Which is DET NOUN VERB DET NOUN, the correct sequence!
    print(tag_scores)
tensor([[-1.1389, -1.2024, -0.9693],
        [-1.1065, -1.2200, -0.9834],
        [-1.1286, -1.2093, -0.9726],
        [-1.1190, -1.1960, -0.9916],
        [-1.0137, -1.2642, -1.0366]])
tensor([[-0.0462, -4.0106, -3.6096],
        [-4.8205, -0.0286, -3.9045],
        [-3.7876, -4.1355, -0.0394],
        [-0.0185, -4.7874, -4.6013],
        [-5.7881, -0.0186, -4.1778]])

练习:使用字符级特征增强 LSTM 词性标注器

在上面的示例中,每个词都有一个嵌入,用作我们序列模型的输入。让我们用从词的字符派生的表示来增强词嵌入。我们预计这将有很大帮助,因为字符级信息(如词缀)对词性有很大影响。例如,带有词缀-ly的词在英语中几乎总是被标记为副词。

为此,令 \(c_w\) 为词 \(w\) 的字符级表示。令 \(x_w\) 为词嵌入,如前所述。然后,我们序列模型的输入是 \(x_w\)\(c_w\) 的串联。因此,如果 \(x_w\) 的维数为 5,\(c_w\) 的维数为 3,则我们的 LSTM 应该接受维数为 8 的输入。

要获得字符级表示,请在词的字符上执行 LSTM,并令 \(c_w\) 为该 LSTM 的最终隐藏状态。提示

  • 您的新模型中将有两个 LSTM。第一个 LSTM 输出 POS 标签得分,第二个 LSTM 输出每个词的字符级表示。

  • 要对字符执行序列模型,您将必须嵌入字符。字符嵌入将作为字符 LSTM 的输入。

脚本的总运行时间:(0 分钟 0.849 秒)

Sphinx-Gallery 生成的图库

文档

访问 PyTorch 的全面的开发者文档

查看文档

教程

获取适用于初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并获得问题的解答

查看资源