Gradient LabSign up free

Cheat sheets › Chapter 10

Sequences and text cheat sheet

Models that read words, sounds and time series. Every lesson in one page: the key idea, the pseudocode and the terms.

Tokenization

Splitting on spaces is a start. But English has hundreds of thousands of words, plus names, typos and new slang. A word-only vocabulary would be huge and still miss things.

text   = "unbelievably good"
words  = ["unbelievably", "good"]
tokens = ["un", "believ", "ably", " good"]     # sub-word pieces
ids    = [403, 18821, 1867, 1695]              # example ID numbers
model sees: [403, 18821, 1867, 1695]

Terms: vocabulary, token

Word embeddings

An embedding turns each token into a list of numbers: a point in space. Before training, the points are scattered at random and mean nothing.

embedding = a learned list of numbers for each token
king  → [0.8, 0.3, 0.9, …]
queen → [0.8, 0.9, 0.9, …]
similar meanings → nearby vectors
king − man + woman ≈ queen      # directions carry meaning

Terms: embedding

Recurrent networks

Language comes in order. A recurrent network reads a sentence one word at a time, from left to right, just as you do.

memory = empty
for each word in the sentence:
    memory = update(memory, word)     # the same small network every step
prediction = output(memory)

# trouble: the memory of early words fades in long sentences

Terms: recurrent network, hidden state

LSTM and GRU

An lstm adds a separate memory “belt” that runs along the whole sequence. Information can ride on it for a long time without being squeezed at every step.

memory belt = carried along every step
forget gate: how much to erase from the belt
input gate:  how much new information to write
output gate: how much to reveal right now

# GRU: a simpler cousin with two gates instead of three

Terms: lstm, gate

Revise with flashcards and quizzes. Free practice for every lesson in this chapter.

Open in the app