All projects

Word Prediction LSTM

PIPELINETokenizePadSplitTrainPADDED SEQUENCES00000100001687000168740016874450168744541LSTMNEXT WORDMost likely wordTrainTest

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

  1. 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.

  2. Sequences

    Each line becomes several examples: its first two words, its first three, and so on up to the whole line.

  3. 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.

  4. 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]

  1. the1
  2. tragedie687
  3. of4
  4. hamlet45
  5. by41
  6. william1886
  7. shakespeare1887
  8. 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”.

The first line of the text, as the model sees it: seven examples, each 13 numbers of input and, in coral, the word to predict. These are the first rows the notebook prints.

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.

  1. Embedding

    Output: 13 × 100

    Turns each word number into 100 values

    481,800 parameters

  2. LSTM

    Output: 13 × 150

    150 units, passing on its output at every step

    150,600 parameters

  3. Dropout

    Output: 13 × 150

    Drops 20% of the values while training

    0 parameters

  4. LSTM

    Output: 100

    100 units, passing on only its last output

    100,400 parameters

  5. Dense

    Output: 4,818

    Softmax over all 4,818 outputs: a probability for each word

    486,618 parameters

1,219,418 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.