liveBook · Manning

Data modality

As explained in Deep Learning with Python, Third Edition

A type of data such as text, images, or audio that can be represented as sequences for model processing.

A data modality is a type of input or output data, such as text, images, audio, or structured data. For sequence models, the practical challenge is to represent each modality as a sequence that a Transformer can process. section 16.4.2

Turning an image into sequence data

Gemma handles an image with a separate 420-million-parameter image encoder. The encoder divides the image into 256 patches and converts them into a (256, 2560) sequence, matching the Transformer's hidden dimension. That sequence can then be spliced into the text sequence after token embedding, functioning like 256 image-specific soft tokens. section 16.4.2

A useful implementation pattern is:

  • Choose an encoder suited to the modality, such as an image encoder for visual input. section 16.4.2
  • Convert the encoder output into vectors with the same dimensionality as the Transformer hidden states. section 16.4.2
  • Insert those vectors into the model's sequence alongside text tokens, distinguishing flexible soft tokens from fixed-vocabulary hard tokens. section 16.4.2
  • Use a special image marker to reserve positions that are replaced by the encoded modality. section 16.4.2
>>> gemma_lm.preprocessor.max_images_per_prompt = 1 #1
>>> gemma_lm.preprocessor.sequence_length = 512  #1
>>> prompt = "What is going on in this image? Be concise!<start_of_image>"
>>> gemma_lm.generate({
...     "prompts": PROMPT_TEMPLATE.format(prompt),
...     "images": [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({
...     "prompts": PROMPT_TEMPLATE.format(prompt),
...     "images": [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>

A working Gemma example supplies an image, places <start_of_image> in the prompt, and generates descriptions and answers about the visual input. section 16.4.2

How Deep Learning with Python, Third Edition covers it

How deep it goes
Traces image patches through encoding, dimensional alignment, placeholder replacement, and soft-token insertion before deferring broader training details.
Examples
Uses image patches, Gemma image prompts, and generated descriptions of a snake wearing glasses in a chair and reading.
What you can do after
Apply it by framing a modality as a compatible sequence and combining that representation with text for Transformer processing.
How it connects
Connects data modality with sequence data, Transformers, image encoders, token embeddings, soft tokens, and multimodal models.

Also explained in

Compare how 4 books explain data modality →

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.