liveBook · Manning

Gemma

As explained in Deep Learning with Python, Third Edition

A pretrained large language model family used here for text generation and later instruction fine-tuning.

Gemma is a pretrained large language model for generating text. The smallest Gemma 3 variation used here has about 1 billion parameters and was pretrained on roughly 2 trillion tokens, making it far larger than a toy model trained from scratch. section 16.3

Load and generate text

KerasHub packages Gemma’s architecture, tokenizer, and pretrained weights through the CausalLM task API: gemma_lm = keras_hub.models.CausalLM.from_preset( "gemma3_1b", dtype="float32", ) gemma_lm.compile(sampler="greedy") gemma_lm.generate("A piece of advice", max_length=40) Generation produces coherent continuations, including advice-like prose and recipe instructions. section 16.3.1

Gemma’s coherence does not make it a reliable assistant by default. Its pretraining objective is next-word prediction, so it can produce plausible-looking continuations without clearly understanding the user’s task. A prompt about a nonexistent 542nd president, for example, receives a confident but incorrect answer. section 16.3.1

Use Gemma with these practical checks:

  • Choose a sampling strategy through CausalLM’s generation interface rather than implementing generation from scratch. section 16.3.1
  • Provide continuation-style prompts when testing pretrained behavior; structured prompts can elicit recipe-like text, but do not guarantee factual answers. section 16.3.1
  • Accept the Gemma Terms of Use and authenticate with Kaggle before downloading the model weights. section 16.3.1
Listing 16.11 Instantiating a pretrained LLM with KerasHub
gemma_lm = keras_hub.models.CausalLM.from_preset(
    "gemma3_1b",
    dtype="float32",
)

Inspect the minimal KerasHub instantiation needed to load the Gemma 3 1B preset. section 16.3.1

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains model scale, loading, tokenization, generation, sampling, and pretrained limitations, but stops short of training internals.
Examples
Demonstrates advice completion, brownie instructions, and a fabricated answer about a nonexistent president.
What it assumes
Assumes familiarity with KerasHub and notebook authentication while introducing pretrained LLM loading through a high-level task API.
What you can do after
Operate/configure Gemma through KerasHub, authenticate for its weights, generate continuations, and recognize unsupported factual output.
How it connects
Connects Gemma to large language models, CausalLM, tokenization, pretrained weights, sampling, and next-word prediction.

Also explained in

Compare how 3 books explain Gemma →

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.