Project 9 of 14, for Caitlin
Text Generator
A character-level text generator that writes shipping forecasts: a vocabulary, a model that counts which character follows which, and the same model learnt by gradient descent through embeddings.
Sign up free to open itLog in
Act 7 needs a free account, so your work is saved as you go. Act 1 is open to everyone.
The brief
- CaitlinMy sailing game's got a radio that reads the shipping forecast for the game's sea areas.
- CaitlinI wrote thirty forecasts and kept five back, and I want the game to make up its own.
- YahyaIt'll be writing the forecast by Friday.
- PaulIt'll be writing something by Friday.
In words.py, turn the forecasts into numbers and build a model that guesses each next character by counting.
build_vocab(text): every character in the text once, sorted.encode(text, vocab): a list of ids, each character's place invocab;decode(ids, vocab)turns them back into text.count_pairs(ids, size): asizebysizearray, wherecounts[a, b]is how often idbcomes straight after ida.probabilities(counts, smoothing=0.1): addsmoothingto every count, then divide each row by its total.average_nll(probs, ids): the average of minus the log of the chance of each real next character.perplexity(probs, ids)is e to that.sample(probs, start, length, rng):lengthids, each drawn withrng.choice(len(probs), p=probs[current])from the row of the id before, starting afterstart.- Run as a program, it scores the held-back forecasts and makes up three of its own.
- In
learn.py, the same model learnt by gradient descent, with an embedding for every character.
Vocabulary: 39 characters Perplexity on forecasts it never saw: 7.05 Guessing evenly would be 39 -we rngor. fy d. od. 4 5. thes. qsherr. gout ooubesinoord.kk. am d. or windwes. eveck3nars. 6 hesaiomo mon sherly omorigherough-weshea9-3 goorably norstester. e. orsththor.
What you learn
What's in it
- Text into numbers
- Every character once
- Counting what comes next
- How surprised is it?
- Writing, a character at a time
- How Paul would do it
- Fix Caitlin's version
- Counting the forecasts
- A row of numbers per token
- A row of the table
- Which rows point alike
- Learning what counting knew
- Downhill to the same place
- Ship it