How to make an if statement using a boolean Tensor
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
A boolean tensor looks like something you should be able to drop directly into a normal Python if statement, but that is usually the wrong mental model. In TensorFlow, tensors are symbolic or deferred values in many execution paths, so Python control flow and tensor control flow are not always interchangeable. The right tool depends on whether you have one scalar condition or an element-wise mask.
The short answer is this: use tf.cond when one boolean tensor decides between two branches, and use tf.where when you want per-element selection across tensors.
Use tf.cond for a Single Tensor Condition
If your condition is a scalar boolean tensor, tf.cond is the direct equivalent of an if statement in graph-style execution.
This says: if flag is true, run the first branch; otherwise run the second branch. Each branch is wrapped in a zero-argument function so TensorFlow can build or execute the correct branch at the right time.
That is the main pattern to remember when you want one tensor to decide one code path.
Why a Plain Python if Usually Fails
A common first attempt looks like this:
That is fragile because Python wants an immediate boolean value, while a tensor is a TensorFlow object. In eager mode you may still run into type errors, and inside @tf.function the difference becomes even more important because TensorFlow is tracing code.
If you are writing TensorFlow logic, treat tensor conditions as tensor control flow, not as ordinary Python booleans.
Use tf.where for Element-Wise Selection
Sometimes you do not want a single branch for the whole program. You want a boolean tensor to choose values position by position. That is what tf.where does.
The output uses elements from a where the mask is true and elements from b where the mask is false.
This is not the same as tf.cond. tf.cond chooses one branch for the whole expression. tf.where chooses values element by element.
Python if Can Still Appear Inside @tf.function
TensorFlow can convert some Python control flow automatically when the code is wrapped in @tf.function. That can make this pattern work:
This works because TensorFlow transforms supported Python control flow into graph-compatible operations during tracing. Even so, it is still valuable to understand tf.cond, because it makes the intent explicit and behaves well when you need a direct tensor-branching primitive.
Build Clear Branches
When using tf.cond, both branches should return compatible tensor shapes and types. A small helper function often keeps the code readable.
The key idea is that both branches represent valid tensor computations. If one branch returns a scalar and the other returns a rank-two tensor, the result becomes hard to reason about and may fail.
Common Pitfalls
The most common mistake is trying to use a tensor as though it were an ordinary Python boolean. That usually leads to confusing control-flow errors.
Another frequent mistake is using tf.cond when the real goal is element-wise masking. In that case, tf.where is the better tool.
A third issue is returning incompatible shapes or types from the two branches of tf.cond. Even if the code looks symmetric, the outputs still need to line up.
Summary
- Use
tf.condwhen one boolean tensor chooses between two whole branches. - Use
tf.wherewhen a boolean tensor should select values element by element. - Do not rely on a plain Python
ifwith tensors unless you understand how TensorFlow tracing applies. - Keep
tf.condbranch outputs compatible in shape and dtype. - Think in tensor control flow, not ordinary Python boolean semantics.
Related reading
- How to make Keras use Tensorflow backend in Anaconda?
- How to make predictions with tf.estimator.Estimator from checkpoint?
- How to make TensorFlow use more available CPU
- How to make tf.data.Dataset return all of the elements in one call?
- How to make predictions on test image after training inception model from scratch on custom dataset?
- How to make predictions using a model that requires an input shape with more than two dimensions using MLflow?
- How to make the tensorflow hub embeddings servable using tensorflow serving?
- How to manually create a tf.Summary
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.