Keras
TensorFlow
MultiHeadAttention
attention_mask
machine learning

MultiHeadAttention attention_mask Keras, Tensorflow example

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

The MultiHeadAttention layer in Keras, part of TensorFlow, is a powerful construct derived from the Transformer architecture. It allows a model to focus on different parts of the input sequence, enabling it to capture dependencies within the sequence more effectively. One of the essential features of the MultiHeadAttention layer is the attention_mask, which plays a crucial role in controlling which input elements should be ignored during the attention calculation. Let's delve into the concepts, usage, and details of the attention_mask with examples.

MultiHeadAttention Overview

In the context of a Transformer, the MultiHeadAttention layer applies multiple attention mechanisms (or "heads") to the input and concatenates the results. This operation allows the model to combine information from different representation subspaces. The key parameters for this layer include:

  • num_heads: Number of attention heads.
  • key_dim: Dimensionality of the query and key tensors.
  • value_dim: Dimensionality of the value tensor. If not specified, it defaults to key_dim.

Attention Mask

The attention_mask parameter is critical for selectively focusing the attention mechanism. It allows you to specify which tokens or elements should be omitted from the attention calculation. This is particularly crucial in language models, where you need to ensure the model doesn’t look ahead during training and only attends to previous tokens.

Types of Attention Masks

  1. Causal Mask: Prevents attending to future tokens. Essential in autoregressive models.
  2. Padding Mask: Ignores padded elements that do not represent actual data, thereby preventing the model from considering them.

Technical Explanation

The attention_mask can be a 2D or 3D tensor:

  • 2D Mask: Shape [batch_size, seq_len], used to mask specific time steps in a sequence.
  • 3D Mask: Shape [batch_size, num_heads, seq_len, seq_len], used for more fine-grained control over specific element pairs in the sequence.

Example Usage with attention_mask

Let's create an example to demonstrate how to use an attention mask in a MultiHeadAttention layer.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.