liveBook · Manning

Causal language model

As explained in Deep Learning with Python, Third Edition

A language model trained to predict the next token from past tokens only, without access to future context.

A causal language model predicts the next token from tokens that came before it. It cannot use future context, so generation proceeds one token at a time—like autocomplete extended into a full sequence. section 15.4.1 section 16.3.1

Prompting a pretrained causal model

With KerasHub, a pretrained Gemma CausalLM can continue prompts such as “The following brownie recipe is easy to make in just a few steps. You can start by” with procedural text. The same next-token objective can also produce coherent but wrong continuations, such as inventing a 542nd U.S. president. section 16.3.1

The objective explains both the strength and the limitation: the model learns to produce likely text, not necessarily useful answers. Prompt wording can steer the continuation toward a recipe, dialogue, or another text pattern, but plausibility is not a guarantee of truth or task completion. section 16.3.1

This differs from masked language modeling. A causal model predicts a token from its past, whereas a masked model predicts hidden tokens from surrounding context; the latter is suited to contextual representation rather than generation in a loop. section 15.4.1

Listing 16.11 Instantiating a pretrained LLM with KerasHub
gemma_lm = keras_hub.models.CausalLM.from_preset(
    "gemma3_1b",
    dtype="float32",
)

Instantiate a pretrained causal language model with KerasHub’s high-level CausalLM API. section 16.3.1

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines the past-only prediction objective, contrasts masked modeling, and demonstrates generation without deriving model internals.
Examples
Uses Gemma prompts for advice, brownie recipes, and a fabricated historical answer to illustrate continuation quality and failure.
What it assumes
Assumes basic familiarity with tokens and Transformer language modeling while contrasting the concept with masked prediction.
What you can do after
Apply it via KerasHub to instantiate a pretrained CausalLM, choose sampling behavior, and generate text from prompts.
How it connects
Connects causal language modeling with Transformer models, masked language modeling, tokenization, sampling, and text generation.

Also explained in

Compare how 3 books explain causal 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.