Word Prediction LSTM
A language model that reads the start of a line and predicts the next word. It learned from Shakespeare's Hamlet, and comes with a Streamlit app for trying it out.
I built the full NLP pipeline, from tokenization and sequence padding to the train-test split, and trained the LSTM model in TensorFlow.
- Date
- Apr 2026
- Type
- Personal project
- Stack
- Python · TensorFlow · NumPy · scikit-learn
Data
One play, word by word
The text of Hamlet from the Gutenberg collection in NLTK, more than 15,000 words, saved as hamlet.txt and lower-cased. It keeps the play's old spellings, like “tragedie”, “heauen” and “starres”, and the speaker names at the start of lines, so the model learns those as words too.
4,817
Different words, each given a number
25,732
Training examples made from the lines
14
Words in the longest example, so every example is padded to 14
80/20
Train and test split: 20,585 and 5,147 examples
Pipeline
From text to training examples
Tokenize
Keras's Tokenizer gives every word a number, most frequent first: “the” is 1, “and” is 2, “to” is 3. Number 0 is kept for padding.
Sequences
Each line becomes several examples: its first two words, its first three, and so on up to the whole line.
Pad
Zeros go in front of every example to make it 14 numbers long. The first 13 are the input; the last is the word to predict, one-hot encoded over all 4,818 outputs.
Split
scikit-learn's train_test_split holds back 20% of the examples for testing.
Line 1 of hamlet.txt
[The Tragedie of Hamlet by William Shakespeare 1599]
- the1
- tragedie687
- of4
- hamlet45
- by41
- william1886
- shakespeare1887
- 15991888
Seven examples from this line: the first is the word “the”, padded with twelve zeros, followed by “tragedie”; the last is the first seven words, padded with six zeros, followed by “1599”.
Model
Embedding, two LSTM layers, softmax
A Keras Sequential model with 1,219,418 parameters. The shapes and counts are the ones the notebook prints.
Embedding
Output: 13 × 100
Turns each word number into 100 values
481,800 parameters
LSTM
Output: 13 × 150
150 units, passing on its output at every step
150,600 parameters
Dropout
Output: 13 × 150
Drops 20% of the values while training
0 parameters
LSTM
Output: 100
100 units, passing on only its last output
100,400 parameters
Dense
Output: 4,818
Softmax over all 4,818 outputs: a probability for each word
486,618 parameters
Training
How it was trained
- Loss
- Categorical cross-entropy.
- Optimiser
- Adam.
- Batches
- 32 examples each, 644 batches per epoch.
- Epochs
- 50, checking the held-out 20% after each one.
The trained model (next_word_lstm.h5) and the fitted tokenizer are saved in the repository, so the app runs without training again.
App
Type, then predict
A Streamlit page with a text box, which opens with “To be or not to”, and a “Predict Next Word” button. The app turns the words into numbers with the same tokenizer, keeps the last 13 and pads them as in training, then shows the word the model rates most likely.
Stack
Built with
Python
Everything, in a Jupyter notebook and the app.
TensorFlow
Building and training the model, with Keras for the tokenizer, the padding and the layers.
NumPy
The padded arrays of examples.
scikit-learn
The train-test split.
NLTK
The text of Hamlet, from its Gutenberg corpus.
Streamlit
The app.