18.4 Pretraining word2vec

We go on to implement the skip-gram model defined in Section 18.1. Then we will pretrain word2vec using negative sampling on the PTB dataset. First of all, let's obtain the data iterator and the vocabulary for this dataset by calling the d2l.load_data_ptb function, which was described in Section 18.3

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)

18.4.1 The Skip-Gram Model#

We implement the skip-gram model by using embedding layers and batch matrix multiplications. First, let's review how embedding layers work.

18.4.1.1 Embedding Layer#

As described in Section 12.2, an embedding layer maps a token's index to its feature vector. The weight of this layer is a matrix whose number of rows equals to the dictionary size (input_dim) and number of columns equals to the vector dimension for each token (output_dim). After a word embedding model is trained, this weight is what we need.

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)

The input of an embedding layer is the index of a token (word). For any token index ii, its vector representation can be obtained from the ithi^\textrm{th} row of the weight matrix in the embedding layer. Since the vector dimension (output_dim) was set to 4, the embedding layer returns vectors with shape (2, 3, 4) for a minibatch of token indices with shape (2, 3).

x = d2l.tensor([[1, 2, 3], [4, 5, 6]])
embed(x)
tensor([[[ 0.7737, -1.4625,  0.4046,  0.9423],
         [ 1.2913, -0.3003, -2.6288,  0.6327],
         [ 0.8458, -2.6503, -0.6972, -1.7333]],

        [[ 0.2792, -1.0551,  0.1862,  0.9358],
         [-0.6069, -0.6454,  1.9835,  1.3281],
         [-1.6159, -0.3401,  1.9392, -1.4623]]], grad_fn=<EmbeddingBackward0>)

18.4.1.2 Defining the Forward Propagation#

In the forward propagation, the input of the skip-gram model includes the center word indices center of shape (batch size, 1) and the concatenated context and noise word indices contexts_and_negatives of shape (batch size, max_len), where max_len is defined in Section 18.3.5. These two variables are first transformed from the token indices into vectors via the embedding layer, then their batch matrix multiplication returns an output of shape (batch size, 1, max_len). Each element in the output is the dot product of a center word vector and a context or noise word vector.

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

Let's print the output shape of this skip_gram function for some example inputs.

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

18.4.2 Training#

Before training the skip-gram model with negative sampling, let's first define its loss function.

18.4.2.1 Binary Cross-Entropy Loss#

According to the definition of the loss function for negative sampling in Section 18.2.1, we will use the binary cross-entropy loss.

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()

Recall our descriptions of the mask variable and the label variable in Section 18.3.5. The following calculates the binary cross-entropy loss for the given variables.

pred = d2l.tensor([[1.1, -2.2, 3.3, -4.4]] * 2)
label = d2l.tensor([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0]])
mask = d2l.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])

Below shows how the above results are calculated (in a less efficient way) using the sigmoid activation function in the binary cross-entropy loss. We can consider the two outputs as two normalized losses that are averaged over non-masked predictions.

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

18.4.2.2 Initializing Model Parameters#

We define two embedding layers for all the words in the vocabulary when they are used as center words and context words, respectively. The word vector dimension embed_size is set to 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))

18.4.2.3 Defining the Training Loop#

The training loop is defined below. Because of the existence of padding, the calculation of the loss function is slightly different compared to the previous training functions.

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)}')

Now we can train a skip-gram model using negative sampling.

lr, num_epochs = 0.002, 5
train(net, data_iter, lr, num_epochs)
loss 0.410, 459217.6 tokens/sec on cuda:0

18.4.3 Applying Word Embeddings#

After training the word2vec model, we can use the cosine similarity of word vectors from the trained model to find words from the dictionary that are most semantically similar to an input word.

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.680: memory
cosine sim=0.664: drives
cosine sim=0.650: chips

18.4.4 Summary#

  • We can train a skip-gram model with negative sampling using embedding layers and the binary cross-entropy loss.
  • Applications of word embeddings include finding semantically similar words for a given word based on the cosine similarity of word vectors.

18.4.5 Exercises#

  1. Using the trained model, find semantically similar words for other input words. Can you improve the results by tuning hyperparameters?
  2. The loss that train reports is the binary cross-entropy averaged over the non-padded positions of each example. Example ii has nin_i context words and KniK n_i noise words, where KK is num_noise_words.
    1. At initialization all dot products are close to zero. Show that the loss is then close to log⁡2≈0.693\log 2 \approx 0.693.
    2. Consider a predictor that outputs the same logit bb for every pair of a center word and a context or noise word. Find the bb that minimizes the loss, and show that the minimum is the binary entropy of 1/(K+1)1/(K + 1), about 0.451 for K=5K = 5. Show that the skip-gram model can represent this predictor although it has no bias terms.
    3. Compare both values with the loss in the training plot. What fraction of the decrease below log⁡2\log 2 does the constant predictor already achieve?
  3. The training loop multiplies the masked loss of each example by mask.shape[1] / mask.sum(axis=1), so that each center word contributes the mean of the losses at its (K+1)ni(K + 1) n_i non-padded positions, where nin_i is its number of context words and KK the number of noise words per context word.
    1. Show that the negative sampling objective of Section 18.2, a sum over all (center word, context word) pairs, instead gives every non-padded position the same weight.
    2. Relative to that objective, by what factor does the training loop weight the loss of one (center word, context word) pair, as a function of nin_i? Which pairs does it weight up: those of center words near either end of a sentence or with a small sampled window, or the others?
  4. get_similar_tokens compares center word vectors, the rows of net[0] (embed_v in TensorFlow).
    1. Pass the context word table net[1] (embed_u) instead, and compare the neighbors of "chip" and of two query words of your choice with those from the center word table.
    2. Write a variant that ranks every word ww by the cosine similarity between the center word vector vq\mathbf{v}_q of the query word qq and the context word vector uw\mathbf{u}_w, and report its five nearest words for "bank", "stock", and "president".
    3. Training increases uw⊤vq\mathbf{u}_w^\top \mathbf{v}_q for words ww that occur near qq. Explain why the variant of the previous part tends to return words that occur next to the query word, whereas the center word table tends to return words that occur in contexts similar to those of the query word.
  5. When a training corpus is huge, we often sample context words and noise words for the center words in the current minibatch when updating model parameters. In other words, the same center word may have different context words or noise words in different training epochs. What are the benefits of this method? Try to implement this training method.

Discussions