预训练word2vec

:label:sec_word2vec_pretraining

我们继续实现 :numref:sec_word2vec中定义的跳元语法模型。然后,我们将在PTB数据集上使用负采样预训练word2vec。首先,让我们通过调用d2l.load_data_ptb函数来获得该数据集的数据迭代器和词表,该函数在 :numref:sec_word2vec_data中进行了描述。

```{.python .input} from d2l import mxnet as d2l import math from mxnet import autograd, gluon, np, npx from mxnet.gluon import nn npx.set_np()

batch_size, max_window_size, num_noise_words = 512, 5, 5 data_iter, vocab = d2l.load_data_ptb(batch_size, max_window_size, num_noise_words)

  1. ```{.python .input}
  2. #@tab pytorch
  3. from d2l import torch as d2l
  4. import math
  5. import torch
  6. from torch import nn
  7. batch_size, max_window_size, num_noise_words = 512, 5, 5
  8. data_iter, vocab = d2l.load_data_ptb(batch_size, max_window_size,
  9. num_noise_words)

跳元模型

我们通过嵌入层和批量矩阵乘法实现了跳元模型。首先,让我们回顾一下嵌入层是如何工作的。

嵌入层

如 :numref:sec_seq2seq中所述,嵌入层将词元的索引映射到其特征向量。该层的权重是一个矩阵,其行数等于字典大小(input_dim),列数等于每个标记的向量维数(output_dim)。在词嵌入模型训练之后,这个权重就是我们所需要的。

```{.python .input} embed = nn.Embedding(input_dim=20, output_dim=4) embed.initialize() embed.weight

  1. ```{.python .input}
  2. #@tab pytorch
  3. embed = nn.Embedding(num_embeddings=20, embedding_dim=4)
  4. print(f'Parameter embedding_weight ({embed.weight.shape}, '
  5. f'dtype={embed.weight.dtype})')

嵌入层的输入是词元(词)的索引。对于任何词元索引$i$,其向量表示可以从嵌入层中的权重矩阵的第$i$行获得。由于向量维度(output_dim)被设置为4,因此当小批量词元索引的形状为(2,3)时,嵌入层返回具有形状(2,3,4)的向量。

```{.python .input}

@tab all

x = d2l.tensor([[1, 2, 3], [4, 5, 6]]) embed(x)

  1. ### 定义前向传播
  2. 在前向传播中,跳元语法模型的输入包括形状为(批量大小,1)的中心词索引`center`和形状为(批量大小,`max_len`)的上下文与噪声词索引`contexts_and_negatives`,其中`max_len` :numref:`subsec_word2vec-minibatch-loading`中定义。这两个变量首先通过嵌入层从词元索引转换成向量,然后它们的批量矩阵相乘(在 :numref:`subsec_batch_dot`中描述)返回形状为(批量大小,1`max_len`)的输出。输出中的每个元素是中心词向量和上下文或噪声词向量的点积。
  3. ```{.python .input}
  4. def skip_gram(center, contexts_and_negatives, embed_v, embed_u):
  5. v = embed_v(center)
  6. u = embed_u(contexts_and_negatives)
  7. pred = npx.batch_dot(v, u.swapaxes(1, 2))
  8. return pred

```{.python .input}

@tab pytorch

def skip_gram(center, contexts_and_negatives, embed_v, embed_u): v = embed_v(center) u = embed_u(contexts_and_negatives) pred = torch.bmm(v, u.permute(0, 2, 1)) return pred

  1. 让我们为一些样例输入打印此`skip_gram`函数的输出形状。
  2. ```{.python .input}
  3. skip_gram(np.ones((2, 1)), np.ones((2, 4)), embed, embed).shape

```{.python .input}

@tab pytorch

skip_gram(torch.ones((2, 1), dtype=torch.long), torch.ones((2, 4), dtype=torch.long), embed, embed).shape

  1. ## 训练
  2. 在训练带负采样的跳元模型之前,我们先定义它的损失函数。
  3. ### 二元交叉熵损失
  4. 根据 :numref:`subsec_negative-sampling`中负采样损失函数的定义,我们将使用二元交叉熵损失。
  5. ```{.python .input}
  6. loss = gluon.loss.SigmoidBCELoss()

```{.python .input}

@tab pytorch

class SigmoidBCELoss(nn.Module):

  1. # 带掩码的二元交叉熵损失
  2. def __init__(self):
  3. super().__init__()
  4. def forward(self, inputs, target, mask=None):
  5. out = nn.functional.binary_cross_entropy_with_logits(
  6. inputs, target, weight=mask, reduction="none")
  7. return out.mean(dim=1)

loss = SigmoidBCELoss()

  1. 回想一下我们在 :numref:`subsec_word2vec-minibatch-loading`中对掩码变量和标签变量的描述。下面计算给定变量的二进制交叉熵损失。
  2. ```{.python .input}
  3. #@tab all
  4. pred = d2l.tensor([[1.1, -2.2, 3.3, -4.4]] * 2)
  5. label = d2l.tensor([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0]])
  6. mask = d2l.tensor([[1, 1, 1, 1], [1, 1, 0, 0]])
  7. loss(pred, label, mask) * mask.shape[1] / mask.sum(axis=1)

下面显示了如何使用二元交叉熵损失中的Sigmoid激活函数(以较低效率的方式)计算上述结果。我们可以将这两个输出视为两个规范化的损失,在非掩码预测上进行平均。

```{.python .input}

@tab all

def sigmd(x): return -math.log(1 / (1 + math.exp(-x)))

print(f’{(sigmd(1.1) + sigmd(2.2) + sigmd(-3.3) + sigmd(4.4)) / 4:.4f}’) print(f’{(sigmd(-1.1) + sigmd(-2.2)) / 2:.4f}’)

  1. ### 初始化模型参数
  2. 我们定义了两个嵌入层,将词表中的所有单词分别作为中心词和上下文词使用。字向量维度`embed_size`被设置为100
  3. ```{.python .input}
  4. embed_size = 100
  5. net = nn.Sequential()
  6. net.add(nn.Embedding(input_dim=len(vocab), output_dim=embed_size),
  7. nn.Embedding(input_dim=len(vocab), output_dim=embed_size))

```{.python .input}

@tab pytorch

embed_size = 100 net = nn.Sequential(nn.Embedding(num_embeddings=len(vocab), embedding_dim=embed_size), nn.Embedding(num_embeddings=len(vocab), embedding_dim=embed_size))

  1. ### 定义训练阶段代码
  2. 训练阶段代码实现定义如下。由于填充的存在,损失函数的计算与以前的训练函数略有不同。
  3. ```{.python .input}
  4. def train(net, data_iter, lr, num_epochs, device=d2l.try_gpu()):
  5. net.initialize(ctx=device, force_reinit=True)
  6. trainer = gluon.Trainer(net.collect_params(), 'adam',
  7. {'learning_rate': lr})
  8. animator = d2l.Animator(xlabel='epoch', ylabel='loss',
  9. xlim=[1, num_epochs])
  10. # 规范化的损失之和,规范化的损失数
  11. metric = d2l.Accumulator(2)
  12. for epoch in range(num_epochs):
  13. timer, num_batches = d2l.Timer(), len(data_iter)
  14. for i, batch in enumerate(data_iter):
  15. center, context_negative, mask, label = [
  16. data.as_in_ctx(device) for data in batch]
  17. with autograd.record():
  18. pred = skip_gram(center, context_negative, net[0], net[1])
  19. l = (loss(pred.reshape(label.shape), label, mask) *
  20. mask.shape[1] / mask.sum(axis=1))
  21. l.backward()
  22. trainer.step(batch_size)
  23. metric.add(l.sum(), l.size)
  24. if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
  25. animator.add(epoch + (i + 1) / num_batches,
  26. (metric[0] / metric[1],))
  27. print(f'loss {metric[0] / metric[1]:.3f}, '
  28. f'{metric[1] / timer.stop():.1f} tokens/sec on {str(device)}')

```{.python .input}

@tab pytorch

def train(net, dataiter, lr, num_epochs, device=d2l.try_gpu()): def init_weights(m): if type(m) == nn.Embedding: nn.init.xavier_uniform(m.weight) net.apply(init_weights) net = net.to(device) optimizer = torch.optim.Adam(net.parameters(), lr=lr) animator = d2l.Animator(xlabel=’epoch’, ylabel=’loss’, xlim=[1, num_epochs])

  1. # 规范化的损失之和,规范化的损失数
  2. metric = d2l.Accumulator(2)
  3. for epoch in range(num_epochs):
  4. timer, num_batches = d2l.Timer(), len(data_iter)
  5. for i, batch in enumerate(data_iter):
  6. optimizer.zero_grad()
  7. center, context_negative, mask, label = [
  8. data.to(device) for data in batch]
  9. pred = skip_gram(center, context_negative, net[0], net[1])
  10. l = (loss(pred.reshape(label.shape).float(), label.float(), mask)
  11. / mask.sum(axis=1) * mask.shape[1])
  12. l.sum().backward()
  13. optimizer.step()
  14. metric.add(l.sum(), l.numel())
  15. if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
  16. animator.add(epoch + (i + 1) / num_batches,
  17. (metric[0] / metric[1],))
  18. print(f'loss {metric[0] / metric[1]:.3f}, '
  19. f'{metric[1] / timer.stop():.1f} tokens/sec on {str(device)}')
  1. 现在,我们可以使用负采样来训练跳元模型。
  2. ```{.python .input}
  3. #@tab all
  4. lr, num_epochs = 0.002, 5
  5. train(net, data_iter, lr, num_epochs)

应用词嵌入

:label:subsec_apply-word-embed

在训练word2vec模型之后,我们可以使用训练好模型中词向量的余弦相似度来从词表中找到与输入单词语义最相似的单词。

```{.python .input} def get_similar_tokens(query_token, k, embed): W = embed.weight.data() x = W[vocab[query_token]]

  1. # 计算余弦相似性。增加1e-9以获得数值稳定性
  2. cos = np.dot(W, x) / np.sqrt(np.sum(W * W, axis=1) * np.sum(x * x) + \
  3. 1e-9)
  4. topk = npx.topk(cos, k=k+1, ret_typ='indices').asnumpy().astype('int32')
  5. for i in topk[1:]: # 删除输入词
  6. print(f'cosine sim={float(cos[i]):.3f}: {vocab.to_tokens(i)}')

get_similar_tokens(‘chip’, 3, net[0])

  1. ```{.python .input}
  2. #@tab pytorch
  3. def get_similar_tokens(query_token, k, embed):
  4. W = embed.weight.data
  5. x = W[vocab[query_token]]
  6. # 计算余弦相似性。增加1e-9以获得数值稳定性
  7. cos = torch.mv(W, x) / torch.sqrt(torch.sum(W * W, dim=1) *
  8. torch.sum(x * x) + 1e-9)
  9. topk = torch.topk(cos, k=k+1)[1].cpu().numpy().astype('int32')
  10. for i in topk[1:]: # 删除输入词
  11. print(f'cosine sim={float(cos[i]):.3f}: {vocab.to_tokens(i)}')
  12. get_similar_tokens('chip', 3, net[0])

小结

  • 我们可以使用嵌入层和二元交叉熵损失来训练带负采样的跳元模型。
  • 词嵌入的应用包括基于词向量的余弦相似度为给定词找到语义相似的词。

练习

  1. 使用训练好的模型,找出其他输入词在语义上相似的词。您能通过调优超参数来改进结果吗?
  2. 当训练语料库很大时,在更新模型参数时,我们经常对当前小批量的中心词进行上下文词和噪声词的采样。换言之,同一中心词在不同的训练迭代轮数可以有不同的上下文词或噪声词。这种方法的好处是什么?尝试实现这种训练方法。

:begin_tab:mxnet Discussions :end_tab:

:begin_tab:pytorch Discussions :end_tab: