Generating Names with Deep Learning
In this chapter, we explore the concept of generating unique, pronounceable human names in English using deep learning techniques. The process involves creating names character by character, leveraging the power of language models.
Understanding the Vocabulary and Tokens
In our approach, the vocabulary consists of all the lowercase letters of the English alphabet. Each letter, or token, is assigned a unique integer representation. For example, the letter "a" is represented as 1, "b" as 2, and so forth. This integer representation allows us to express any name as a sequence of these integers. For instance, the name "ada" would be represented as the sequence [1, 4, 1]. Additionally, a special token "$" is used to indicate the start or end of a name.
Figure 9.1 General terminology in language models relating vocabulary, token, and sequence. The "$" token is a special token used to indicate the start or end of a name.
Initial Approach: Random Name Generation
As a preliminary step, we can generate names by randomly selecting letters and stringing them together. This involves generating a uniform distribution of the letters, where each letter appears with equal probability. The softmax function is utilized to convert raw scores into probabilities, ensuring that the sum of probabilities equals one.
Here is a simple implementation using PyTorch:
# [In[]:](https://livebook.manning.com/book/deep-learning-with-pytorch-second-edition/chapter-9/12)
import torch
import torch.nn as nn
import torch.nn.functional as F
equal_probs = F.softmax(torch.ones(vocab_size), dim=0) #1
for i in range(5):
generated = ""
while True:
random_int = torch.multinomial(equal_probs, 1).item() #2
random_char = i_to_ch[random_int]
if random_char == "$":
break
generated += random_char
print(f"name {i}: {generated}")
# [Out[]:](https://livebook.manning.com/book/deep-learning-with-pytorch-second-edition/chapter-9/12)
name 0: zrcgahwsalcydnvq
name 1: allatytiyxpckqwbwmeyi
name 2: jrwymeibyroivykbrqqhhbowjpm
name 3: ancvvzdtzhzlweiew
name 4: fvydasrmttj
- We pass a vector of identical values to
softmax, resulting in a uniform distribution (1/27) over the characters. - The
torch.multinomialfunction samples from this probability distribution, returning a single index corresponding to a character.
However, the output from this method is not satisfactory. The generated names are neither readable nor pronounceable. This is because treating all characters with equal probability does not reflect the natural distribution of letters in human names. For instance, characters like "x" or "q" are rarely found in names, so assigning them equal probability with more common letters is not ideal.
Improving Name Generation
To improve the quality of generated names, we need to consider a more sophisticated approach that accounts for the varying frequencies of different letters in human names. This involves training a model to learn these distributions and generate names that are more realistic and pronounceable.
FAQ (Frequently asked questions)
What does 'name' refer to in Chapter 9.1 of 'Deep Learning with PyTorch, Second Edition'?
What is the goal of generating names character by character in Chapter 9.1?