15.4. 预训练word2vec¶ 在 SageMaker Studio Lab 中打开 Notebook
我们继续实现 :numref:`sec_word2vec` 中定义的跳字模型。然后,我们将在PTB数据集上使用负采样预训练word2vec。首先,通过调用在 :numref:`sec_word2vec-data` 中描述的 `d2l.load_data_ptb` 函数来获取该数据集的数据迭代器和词表。
import math
import torch
from torch import nn
from d2l import torch as d2l
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)
import math
from mxnet import autograd, gluon, np, npx
from mxnet.gluon import nn
from d2l import mxnet as d2l
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)
15.4.1. 跳字模型¶
我们通过使用嵌入层和批量矩阵乘法来实现跳字模型。首先,让我们回顾一下嵌入层的工作原理。
15.4.1.1. 嵌入层¶
如 :numref:`sec_seq2seq` 中所述,嵌入层将词元的索引映射到其特征向量。该层的权重是一个矩阵,其行数等于字典大小(`input_dim`),列数等于每个词元的向量维度(`output_dim`)。在词嵌入模型训练之后,这个权重就是我们所需要的。
embed = nn.Embedding(num_embeddings=20, embedding_dim=4)
print(f'Parameter embedding_weight ({embed.weight.shape}, '
f'dtype={embed.weight.dtype})')
Parameter embedding_weight (torch.Size([20, 4]), dtype=torch.float32)
embed = nn.Embedding(input_dim=20, output_dim=4)
embed.initialize()
embed.weight
[22:26:39] ../src/storage/storage.cc:196: Using Pooled (Naive) StorageManager for CPU
Parameter embedding0_weight (shape=(20, 4), dtype=float32)
嵌入层的输入是词元(单词)的索引。对于任何词元索引 \(i\),其向量表示可以从嵌入层权重矩阵的第 \(i\) 行获得。由于向量维度(`output_dim`)被设置为4,因此对于形状为 (2, 3) 的小批量词元索引,嵌入层返回形状为 (2, 3, 4) 的向量。
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
embed(x)
tensor([[[ 0.7606, 0.3872, -0.1864, 1.1732],
[ 1.5035, 2.3623, -1.7542, -1.4990],
[-1.2639, -1.5313, 2.1719, 0.4151]],
[[-1.9079, 0.2434, 1.5395, 1.2990],
[ 0.7470, 1.0129, 0.4039, 0.0591],
[-0.6293, -0.1814, -0.4782, -0.5289]]], grad_fn=<EmbeddingBackward0>)
x = np.array([[1, 2, 3], [4, 5, 6]])
embed(x)
array([[[ 0.01438687, 0.05011239, 0.00628365, 0.04861524],
[-0.01068833, 0.01729892, 0.02042518, -0.01618656],
[-0.00873779, -0.02834515, 0.05484822, -0.06206018]],
[[ 0.06491279, -0.03182812, -0.01631819, -0.00312688],
[ 0.0408415 , 0.04370362, 0.00404529, -0.0028032 ],
[ 0.00952624, -0.01501013, 0.05958354, 0.04705103]]])
15.4.1.2. 定义前向传播¶
在前向传播中,跳字模型的输入包括形状为(批量大小,1)的中心词索引 `center` 和形状为(批量大小,`max_len`)的拼接后的上下文词与噪声词索引 `contexts_and_negatives`,其中 `max_len` 在 :numref:`subsec_word2vec-minibatch-loading` 中定义。这两个变量首先通过嵌入层从词元索引转换为向量,然后它们的批量矩阵乘法(在 :numref:`subsec-batch-dot` 中描述)返回一个形状为(批量大小,1,`max_len`)的输出。输出中的每个元素都是中心词向量和上下文词或噪声词向量的点积。
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
def skip_gram(center, contexts_and_negatives, embed_v, embed_u):
v = embed_v(center)
u = embed_u(contexts_and_negatives)
pred = npx.batch_dot(v, u.swapaxes(1, 2))
return pred
让我们打印这个 `skip_gram` 函数对于一些示例输入的输出形状。
skip_gram(torch.ones((2, 1), dtype=torch.long),
torch.ones((2, 4), dtype=torch.long), embed, embed).shape
torch.Size([2, 1, 4])
skip_gram(np.ones((2, 1)), np.ones((2, 4)), embed, embed).shape
(2, 1, 4)
15.4.2. 训练¶
在使用负采样训练跳字模型之前,让我们先定义它的损失函数。
15.4.2.1. 二元交叉熵损失¶
根据 :numref:`subsec-negative-sampling` 中负采样损失函数的定义,我们将使用二元交叉熵损失。
class SigmoidBCELoss(nn.Module):
# Binary cross-entropy loss with masking
def __init__(self):
super().__init__()
def forward(self, inputs, target, mask=None):
out = nn.functional.binary_cross_entropy_with_logits(
inputs, target, weight=mask, reduction="none")
return out.mean(dim=1)
loss = SigmoidBCELoss()
loss = gluon.loss.SigmoidBCELoss()
回想一下我们在 :numref:`subsec_word2vec-minibatch-loading` 中对掩码变量和标签变量的描述。下面计算了给定变量的二元交叉熵损失。
pred = torch.tensor([[1.1, -2.2, 3.3, -4.4]] * 2)
label = torch.tensor([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0]])
mask = torch.tensor([[1, 1, 1, 1], [1, 1, 0, 0]])
loss(pred, label, mask) * mask.shape[1] / mask.sum(axis=1)
tensor([0.9352, 1.8462])
pred = np.array([[1.1, -2.2, 3.3, -4.4]] * 2)
label = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0]])
mask = np.array([[1, 1, 1, 1], [1, 1, 0, 0]])
loss(pred, label, mask) * mask.shape[1] / mask.sum(axis=1)
array([0.9352101, 1.8462093])
下面展示了如何使用二元交叉熵损失中的 sigmoid 激活函数以一种效率较低的方式计算上述结果。我们可以将这两个输出视为两个归一化的损失,它们是在非掩码预测上平均得到的。
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}')
0.9352
1.8462
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}')
0.9352
1.8462
15.4.2.2. 初始化模型参数¶
我们为词表中所有单词分别定义了两个嵌入层,当它们分别用作中心词和上下文词时。词向量维度 `embed_size` 设置为 100。
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))
embed_size = 100
net = nn.Sequential()
net.add(nn.Embedding(input_dim=len(vocab), output_dim=embed_size),
nn.Embedding(input_dim=len(vocab), output_dim=embed_size))
15.4.2.3. 定义训练循环¶
训练循环定义如下。由于填充的存在,损失函数的计算与之前的训练函数略有不同。
def train(net, data_iter, lr, num_epochs, device=d2l.try_gpu()):
def init_weights(module):
if type(module) == nn.Embedding:
nn.init.xavier_uniform_(module.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])
# Sum of normalized losses, no. of normalized losses
metric = d2l.Accumulator(2)
for epoch in range(num_epochs):
timer, num_batches = d2l.Timer(), len(data_iter)
for i, batch in enumerate(data_iter):
optimizer.zero_grad()
center, context_negative, mask, label = [
data.to(device) for data in batch]
pred = skip_gram(center, context_negative, net[0], net[1])
l = (loss(pred.reshape(label.shape).float(), label.float(), mask)
/ mask.sum(axis=1) * mask.shape[1])
l.sum().backward()
optimizer.step()
metric.add(l.sum(), l.numel())
if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
animator.add(epoch + (i + 1) / num_batches,
(metric[0] / metric[1],))
print(f'loss {metric[0] / metric[1]:.3f}, '
f'{metric[1] / timer.stop():.1f} tokens/sec on {str(device)}')
def train(net, data_iter, lr, num_epochs, device=d2l.try_gpu()):
net.initialize(ctx=device, force_reinit=True)
trainer = gluon.Trainer(net.collect_params(), 'adam',
{'learning_rate': lr})
animator = d2l.Animator(xlabel='epoch', ylabel='loss',
xlim=[1, num_epochs])
# Sum of normalized losses, no. of normalized losses
metric = d2l.Accumulator(2)
for epoch in range(num_epochs):
timer, num_batches = d2l.Timer(), len(data_iter)
for i, batch in enumerate(data_iter):
center, context_negative, mask, label = [
data.as_in_ctx(device) for data in batch]
with autograd.record():
pred = skip_gram(center, context_negative, net[0], net[1])
l = (loss(pred.reshape(label.shape), label, mask) *
mask.shape[1] / mask.sum(axis=1))
l.backward()
trainer.step(batch_size)
metric.add(l.sum(), l.size)
if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
animator.add(epoch + (i + 1) / num_batches,
(metric[0] / metric[1],))
print(f'loss {metric[0] / metric[1]:.3f}, '
f'{metric[1] / timer.stop():.1f} tokens/sec on {str(device)}')
现在我们可以使用负采样来训练一个跳字模型。
lr, num_epochs = 0.002, 5
train(net, data_iter, lr, num_epochs)
loss 0.410, 223485.0 tokens/sec on cuda:0
lr, num_epochs = 0.002, 5
train(net, data_iter, lr, num_epochs)
loss 0.408, 108453.4 tokens/sec on gpu(0)
15.4.3. 应用词嵌入¶
在训练完word2vec模型后,我们可以使用训练好的模型中词向量的余弦相似度,从词典中找出与输入词在语义上最相似的词。
def get_similar_tokens(query_token, k, embed):
W = embed.weight.data
x = W[vocab[query_token]]
# Compute the cosine similarity. Add 1e-9 for numerical stability
cos = torch.mv(W, x) / torch.sqrt(torch.sum(W * W, dim=1) *
torch.sum(x * x) + 1e-9)
topk = torch.topk(cos, k=k+1)[1].cpu().numpy().astype('int32')
for i in topk[1:]: # Remove the input words
print(f'cosine sim={float(cos[i]):.3f}: {vocab.to_tokens(i)}')
get_similar_tokens('chip', 3, net[0])
cosine sim=0.702: microprocessor
cosine sim=0.649: mips
cosine sim=0.643: intel
def get_similar_tokens(query_token, k, embed):
W = embed.weight.data()
x = W[vocab[query_token]]
# Compute the cosine similarity. Add 1e-9 for numerical stability
cos = np.dot(W, x) / np.sqrt(np.sum(W * W, axis=1) * np.sum(x * x) + 1e-9)
topk = npx.topk(cos, k=k+1, ret_typ='indices').asnumpy().astype('int32')
for i in topk[1:]: # Remove the input words
print(f'cosine sim={float(cos[i]):.3f}: {vocab.to_tokens(i)}')
get_similar_tokens('chip', 3, net[0])
cosine sim=0.681: intel
cosine sim=0.662: microprocessor
cosine sim=0.619: memory
15.4.4. 小结¶
我们可以使用嵌入层和二元交叉熵损失来训练一个带负采样的跳字模型。
词嵌入的应用包括基于词向量的余弦相似度,为给定词找到语义上相似的词。