Tensorflow How to convert NaNs to a number?
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
If you want to replace NaN values in a TensorFlow tensor with a real number such as 0.0, the usual solution is tf.where combined with tf.math.is_nan. That gives you explicit control over which values are replaced and what replacement value should be used.
This matters because NaN values propagate through arithmetic and can quickly poison training or inference results. Replacing them early in the input pipeline or right before sensitive computations is often the safest approach.
Replace NaN with tf.where
The direct pattern looks like this:
This returns a tensor where every NaN has been replaced by 0.0 while non-NaN values are left unchanged.
You can replace with any number you want:
That is often useful when a specific sentinel value or default fill is required.
Match the Tensor Shape and Type
For scalar replacement values such as 0.0, TensorFlow broadcasts automatically. If you need a tensor-shaped replacement, provide one with a compatible shape and dtype:
This is helpful when the replacement value is computed dynamically rather than hard-coded.
Clean Matrices and Higher-Dimensional Tensors
The same pattern works for matrices and batches:
Using tf.zeros_like is a convenient way to replace all NaN values with zeros while keeping the same shape and dtype.
Consider Why the NaN Values Exist
Replacing NaN values is useful, but it is also worth asking where they came from. Common causes include:
- division by zero
- invalid logarithms
- exploding gradients
- missing or badly parsed input data
If the NaN values indicate a modeling bug, replacement may only hide the symptom. In data-cleaning pipelines, replacement is often correct. In model training, it can be a sign that something upstream needs attention.
That is why many teams do both: they replace NaN values at the data boundary for robustness and they also alert or inspect metrics so the root cause is not forgotten. The replacement step should be deliberate, not just automatic cleanup with no follow-up.
Apply It in the Input Pipeline
If the problem comes from training data, cleaning it in the input pipeline keeps the model code simpler:
That ensures the model never sees the NaN values at all.
Common Pitfalls
- Replacing
NaNvalues without investigating whether they come from a deeper numerical bug. - Forgetting that the replacement value must have a compatible dtype with the tensor.
- Cleaning only one tensor in a pipeline while other features or labels still contain
NaNvalues. - Assuming replacement is always harmless during training when it may distort the data distribution.
Summary
- Use
tf.where(tf.math.is_nan(x), replacement, x)to replaceNaNvalues in TensorFlow. - Use scalars,
tf.zeros_like, or tensor-shaped replacements depending on the use case. - The same approach works for vectors, matrices, and batches.
- Cleaning input data early is often better than letting
NaNvalues propagate into the model. - Replacement is useful, but it is still worth understanding why the
NaNvalues appeared in the first place.
Related reading
- Tensorflow How to convert scalar tensor to scalar variable in python?
- TensorFlow How to ensure Tensors are in the same graph
- Tensorflow How to extract attention_scores for graphing?
- Tensorflow How to feed a placeholder variable with a tensor?
- Tensorflow How to find the size of a tf.data.Dataset API object
- Tensorflow How to get a tensor by name?
- Tensorflow How to modify the value in tensor
- TensorFlow how to safely terminate training manually KeyboardInterrupt
.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.