Keras
boolean mask
tensor manipulation
deep learning
Python

How do you create a boolean mask for a tensor in Keras?

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

Introduction

In Keras, boolean masks are really TensorFlow masks. You create a boolean tensor from a condition, then use that mask to filter values, ignore padding, or decide which positions should participate in later computation.

Two related ideas often get mixed together. One is manually masking tensor values with tf.boolean_mask. The other is Keras sequence masking, where layers such as Embedding or Masking propagate a mask internally.

Create a Mask from a Condition

The most direct approach is to compare a tensor against a rule:

python
1import tensorflow as tf
2
3x = tf.constant([
4    [1.0, 7.0, 2.0],
5    [9.0, 3.0, 8.0],
6])
7
8mask = x > 5.0
9print(mask)

mask has the same shape as x, but each entry is boolean. Any expression that returns True or False can be used, including >, ==, !=, or TensorFlow helpers such as tf.math.greater.

Apply the Mask with tf.boolean_mask

Once you have a boolean tensor, apply it with tf.boolean_mask:

python
filtered = tf.boolean_mask(x, mask)
print(filtered)

This returns only the elements where the mask is True.

One detail matters a lot: tf.boolean_mask removes masked-out entries and collapses the masked dimensions. That is often exactly what you want when filtering values, but it can be surprising if you expected the original shape to remain unchanged.

You can also apply a mask along a specific axis:

python
1rows = tf.constant([
2    [1, 2],
3    [3, 4],
4    [5, 6],
5])
6
7row_mask = tf.constant([True, False, True])
8selected_rows = tf.boolean_mask(rows, row_mask, axis=0)
9print(selected_rows)

Here the mask is applied to rows instead of to individual scalar elements.

Keras Sequence Masks

Keras also has a higher-level masking system for sequence models. This is common when 0 means padding and later layers should ignore padded timesteps.

python
1from tensorflow import keras
2
3inputs = keras.Input(shape=(None,), dtype="int32")
4x = keras.layers.Embedding(
5    input_dim=1000,
6    output_dim=16,
7    mask_zero=True,
8)(inputs)
9x = keras.layers.LSTM(32)(x)
10outputs = keras.layers.Dense(1)(x)
11
12model = keras.Model(inputs, outputs)

In this pattern, you are not manually calling tf.boolean_mask. Instead, Keras computes and propagates a mask so sequence-aware layers can ignore padding.

That difference is important:

  • use tf.boolean_mask when you want to filter tensor values directly
  • use Keras masking when you want model layers to ignore certain sequence positions

Building a Richer Boolean Mask

Real models often need more than one condition. You can combine masks with logical operators:

python
1x = tf.constant([0.2, -1.0, 0.7, 1.5, 0.0])
2mask = tf.logical_and(x >= 0.0, x < 1.0)
3filtered = tf.boolean_mask(x, mask)
4print(filtered)

This keeps only values in the interval from 0.0 up to but not including 1.0.

Masks like this are common in preprocessing, selective loss computation, metric filtering, and attention-related code.

Common Pitfalls

The biggest mistake is expecting tf.boolean_mask to preserve the original tensor shape. It returns only the selected values, so masked dimensions collapse.

Another common problem is confusing manual boolean masking with propagated Keras sequence masking. They are related, but they solve different problems.

A third issue is shape mismatch. The mask shape must match the dimensions you are masking.

Finally, do not forget the dtype. The mask must be boolean, not integer 0 and 1 values unless you explicitly convert them.

Summary

  • Create a boolean mask by applying a condition to a tensor.
  • Use tf.boolean_mask to filter values or slices.
  • Remember that tf.boolean_mask collapses the masked dimensions.
  • Use Keras sequence masking when padded timesteps should be ignored by model layers.
  • Most masking bugs come from shape mismatch or from confusing these two masking styles.

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
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my 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