Running Keras with double precision fails
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
Keras can run with float64, but the whole pipeline has to agree on that choice. When double-precision training fails, the usual causes are mixed dtypes, unsupported GPU kernels, or custom code that silently falls back to float32.
Make the Dtype Choice Explicit
If you want double precision, configure it deliberately:
This is the simplest stable pattern:
- global default set to
float64 - input arrays are
float64 - layers are explicitly
float64
If one part stays in float32, TensorFlow may insert casts or throw dtype mismatch errors.
Why GPU Runs Fail More Often
Many GPUs and kernels are optimized for float32 and lower-precision training. Double precision may be:
- slower
- unsupported for some operations
- poorly supported in a particular driver or package combination
That means a model can work on CPU and fail on GPU for the same code path.
A useful diagnostic step is to force CPU execution:
If the model works on CPU but fails on GPU, the issue is usually kernel or hardware support rather than your model definition.
Watch for Accidental Mixed Precision
A very common bug is mixing float64 inputs with float32 constants or layers:
This may not fail immediately, but it creates instability and confusing casts deeper in the graph.
A few quick checks help:
When debugging precision issues, inspect dtypes aggressively instead of assuming TensorFlow guessed correctly.
Custom Layers Need Extra Care
Even if the main model is configured for float64, custom code can break the guarantee:
- hard-coded
float32constants - '
tf.cast(..., tf.float32)hidden in helper functions' - third-party ops compiled only for common precisions
- preprocessing code that converts back to
float32
So if the obvious layers look correct, inspect the custom or less-traveled parts of the pipeline next.
Decide Whether You Really Need float64
Double precision is not automatically better for deep learning. It uses more memory and often runs slower. Many models train perfectly well in float32, and many modern accelerators are tuned for that path.
Use float64 when you actually need the precision, such as:
- scientific modeling
- numerically sensitive custom losses
- long chains of calculations where accumulated error matters
If there is no strong numerical reason, float32 is usually the better engineering default.
Memory Cost Matters Too
Moving from float32 to float64 doubles tensor storage. So even if dtype support is fine, you can trigger memory errors more easily:
- larger activations
- larger optimizer state
- larger gradients
That means some "double precision fails" cases are really memory-capacity failures in disguise.
Debugging Checklist
When float64 runs fail, check in this order:
- are the NumPy inputs really
float64 - were layers created after setting the global float type
- does the failure happen only on GPU
- do custom layers or helper functions hard-code
float32 - are you actually hitting a memory limit rather than a dtype-support problem
That sequence usually gets to the root cause quickly.
Common Pitfalls
- Setting
float64globally but feedingfloat32inputs. - Assuming every GPU path supports double precision as smoothly as
float32. - Hard-coding
float32constants inside custom layers. - Forgetting that
float64doubles tensor memory usage. - Using double precision by habit instead of because the workload truly needs it.
Summary
- Keras can run in double precision, but the full pipeline must agree on dtype.
- Most failures come from mixed dtypes, limited GPU support, or hidden
float32casts. - Make layer and input dtypes explicit when debugging.
- If GPU execution fails, test the same code on CPU to isolate the problem.
- Use
float64intentionally, not automatically.
Related reading
- Running MSIL on GPU
- Running multiple tensorflow sessions concurrently
- running nvidia-docker on Windows 10 WSL2
- Running tf.mod and tf.floor_div in tensorflow in GPU
- Running session using tensorflow c api is significantly slower than using python
- Running Tensorflow graph multiple times over different input parameters what kind of loop is efficient?
- Running Tensorflow in Jupyter Notebook
- Running TensorFlow on a Slurm Cluster?
.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.