liveBook · Manning

Gemma

As explained in Deep Learning with R, Third Edition

Gemma is a family of pretrained generative language models released in specialized instruction-tuned variants.

Gemma is a family of pretrained generative language models. Gemma 3 includes a compact version with about 1 billion parameters, trained on roughly 2 trillion tokens. Its next-token pretraining objective produces coherent continuations, but not necessarily useful answers without suitable prompting. section 16.3 section 16.3.1

Listing 16.11 Instantiating a pretrained LLM with KerasHub
py_require("keras-hub")
keras_hub <- import("keras_hub")

gemma_lm <- keras_hub$models$CausalLM$from_preset(
  "gemma3_1b",
  dtype = "float32"
)

Load the pretrained Gemma 3 1B causal language model and its matching tokenizer through KerasHub. section 16.3.1

Generate text with Gemma

After accepting the Gemma terms of use and authenticating with Kaggle, configure a sampler and generate a continuation: `gemma_lm$compile(sampler = "greedy")` `gemma_lm$generate("How can I make brownies?", max_length = 40L)` A prompt that begins a recipe works better: `gemma_lm$generate("The following brownie recipe is easy to make in just a few steps.\n\nYou can start by", max_length = 40L)` This produces procedural text such as melting butter and sugar, then adding eggs and vanilla. section 16.3.1

Gemma also supports multimodal prompting. A separate 420-million-parameter image encoder divides an image into 256 patches and converts them into soft tokens matching the Transformer's hidden dimension. These image representations can be spliced into the text sequence. section 16.4.2

gemma_lm$preprocessor$max_images_per_prompt <- 1L  #1
gemma_lm$preprocessor$sequence_length <- 512L 
prompt <- "What is going on in this image? Be concise!<start_of_image>"
gemma_lm$generate(list(
  prompts = template_format(prompt),
  images = list(image)
))

<start_of_turn>user

What is going on in this image? Be concise!

<start_of_image>

<end_of_turn>

<start_of_turn>model

A snake wearing glasses is sitting in a leather armchair, surrounded by a large

bookshelf, and reading a book. It’s a whimsical, slightly surreal image.

<end_of_turn>

prompt = "What is the snake wearing?<start_of_image>"
gemma_lm$generate(list(
  prompts = template_format(prompt),
  images = list(image)
))

<start_of_turn>user

What is the snake wearing?





<start_of_image>

<end_of_turn>

<start_of_turn>model

The snake is wearing a pair of glasses! They are red-framed and perched on its

head.<end_of_turn>

Prompt Gemma with an image placeholder and an image object to ask questions about visual content. section 16.4.2

Ask about an image

Use `<start_of_image>` in the prompt and pass the image separately. Gemma can describe the scene or answer a focused question, such as identifying the red-framed glasses worn by a snake in an armchair.

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains loading, tokenization, generation, next-token prediction, and image soft tokens, but stops short of detailing fine-tuning.
Examples
Demonstrates advice, brownie recipes, an impossible-president question, image descriptions, and visual question answering.
What you can do after
Operate/configure Gemma through KerasHub, generate prompted continuations, and submit image-and-text inputs.
How it connects
Connects causal language modeling with tokenizers, Transformers, pretraining, fine-tuning, and multimodal learning.

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 R, Third Edition. The section links point to the parts of the book it draws on.