liveBook · Manning

Language model

As explained in Deep Learning with Python, Third Edition

A language model trained on large text corpora that can generate human-sounding responses, mentioned here as an example of a system that may be modular but still lacks recombination ability.

Predicting an entire sentence as one output is impractical: a 20,000-word vocabulary allows 160 quadrillion four-word sequences. A language model makes the problem manageable by predicting one token at a time. Given past tokens, it estimates a probability distribution over what comes next; repeating that prediction generates text. section 15.1

Generate Shakespeare-like text

A character-level model starts with a prompt such as “KING RICHARD III:”. Feed the prompt’s characters through the model to establish its starting state, choose a predicted next character, then feed that character back in. Repeat to produce dialogue-like text. The resulting passage imitates the style without being a quotation. section 15.1.2

Listing 15.8 Predicting with the language model a token at a time
import numpy as np
 
generated_ids = []
max_length = 250
for i in range(max_length):                                      #1
    next_char = int(np.argmax(predictions, axis=-1)[0])          #2
    generated_ids.append(next_char)
    inputs = keras.ops.expand_dims([next_char], axis=0)
    predictions, state = generation_model.predict((inputs, state), verbose=0)

The generation loop shows the step that a single prediction call cannot perform: each selected token becomes the next input. section 15.1.2

Related setups change what information is available when predicting tokens:

  • A masked language model predicts tokens from surrounding tokens and can support text or token classification. section 15.6
  • A sequence-to-sequence language model predicts target tokens using both earlier target tokens and a separate source sequence; translation is one application. section 15.6
  • A Transformer decoder uses causal attention for next-token prediction, while a Transformer encoder uses bidirectional attention to represent a sequence. section 15.6

Generating fluent text is not the same as reliably solving a problem. A model may answer a familiar math question correctly yet fail when names or numbers change. Prompts that ask it to show intermediate steps can improve results, but individual outputs still need checking. section 16.4.4

For large language models, bigger is not the only practical choice. With a fixed pretraining compute budget, training a smaller model on more data may outperform training a larger one; deployment cost also favors models that fit cheaper hardware. section 16.5

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines the next-token distribution and works through an inference loop, then distinguishes masked and sequence-to-sequence variants.
Examples
Builds Shakespeare-like character generation from a prompt and tests a large language model on a letter-counting word problem.
What it assumes
Assumes basic text preprocessing and modeling, then develops next-token prediction from the problem of predicting token sequences.
What you can do after
Build it from scratch using a Keras token-by-token generation loop that feeds each predicted character back into the model.
How it connects
Connects language models to tokenization, recurrent neural networks, masked language modeling, sequence-to-sequence modeling, attention, and Transformers.

Also explained in

Compare how 67 books explain language model →

Related concepts

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