liveBook · Manning

Vocabulary

As explained in Deep Learning with PyTorch, Second Edition

The set of distinct symbols or words a language model can assign probabilities to in this character- or word-based setting.

A vocabulary is the distinct set of symbols or words a language model can recognize and use. Each element is a token, and each token receives a unique integer representation. section 9.1

The vocabulary determines the model’s encoding space. Character vocabularies are compact, while word vocabularies can produce extremely wide one-hot vectors and require handling words absent from the dictionary. section 4.5.3

A vocabulary supports several related operations:

  • Map each token to an integer with a character-to-index or word-to-index dictionary. section 4.5.3 section 9.1
  • Represent a name or sentence as a sequence of token indices. section 9.1
  • Provide the output classes whose probabilities can be produced by a language model. section 9.1

Character and word vocabularies

For name generation, the vocabulary "$abcdefghijklmnopqrstuvwxyz" assigns "$" index 0 and letters successive indices; “ada” becomes [0, 1, 4, 1, 0], where "$" marks both boundaries. For word-level encoding, a cleaned sentence is mapped into an 11-by-7,261 tensor, with one column for each dictionary word. section 9.1 section 4.5.3

Listing 9.1 Setting up vocabulary and character mappings
# In[]:
vocab = "$abcdefghijklmnopqrstuvwxyz"
vocab_size = len(vocab)
ch_to_i = {char: i for i, char in enumerate(vocab)}
i_to_ch = {i: char for i, char in enumerate(vocab)}

Inspect the compact character vocabulary and its forward and reverse Python mappings. section 9.1

# In[8]:
word_t = torch.zeros(len(words_in_line), len(word2index_dict))
for i, word in enumerate(words_in_line):
    word_index = word2index_dict[word]
    word_t[i][word_index] = 1
    print('{:2} {:4} {}'.format(i, word_index, word))

print(word_t.shape)


# Out[8]:
 0 3394 impossible
 1 4305 mr
 2  813 bennet
 3 3394 impossible
 4 7078 when
 5 3315 i
 6  415 am
 7 4436 not
 8  239 acquainted
 9 7148 with
10 3215 him
torch.Size([11, 7261])

Inspect how a word vocabulary becomes a one-hot tensor with one vector per word. section 4.5.3

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains token mappings and tensor construction, then stops short of efficient embeddings and intermediate subword representations.
Examples
Demonstrates lowercase character names, a boundary token, cleaned words, dictionary indices, and an 11-by-7,261 one-hot tensor.
What it assumes
Assumes basic Python dictionaries and tensors while introducing tokenization, vocabulary construction, and integer encoding concretely.
What you can do after
Build it from scratch with Python dictionaries, encode names or sentences as integer sequences, and construct one-hot tensors.
How it connects
Connects tokens and sequences with one-hot encoding, embeddings, softmax, and language-model generation.

Also explained in

Compare how 13 books explain vocabulary →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.