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


