ValueError Trying to share variable var, but specified dtype float32 and found dtype float64_ref when trying to use get_variable
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This TensorFlow error happens when code tries to reuse a variable name but asks for a different dtype than the one already stored in the graph. In practice, the fix is to make variable creation and reuse agree on one dtype, then cast inputs at the edges instead of letting mismatches leak into get_variable.
Why the Error Happens
tf.compat.v1.get_variable looks up variables by name inside the current variable scope. If a variable already exists and you reuse it, TensorFlow expects the shape and dtype to match the original definition. If you created var as float64 and later request var as float32, TensorFlow refuses because that would silently change the graph contract.
Here is a small example that reproduces the problem:
The first block creates the variable as float64. The second block tries to reuse the same name as float32, which triggers the error immediately while the graph is being built.
Fix the Variable Definition, Not Just the Symptom
The right fix is usually to standardize the variable dtype at creation time and make the rest of the graph conform to it. In most TensorFlow models, float32 is the best default because it is faster and more memory-efficient than float64.
Once the dtype is consistent, reuse works as intended.
Cast Incoming Data Before It Touches the Variable
Sometimes the variable is correct and the real problem is that upstream NumPy data defaults to float64. That is common because NumPy arrays created from plain Python floats usually become float64 unless you specify otherwise.
Casting inputs at the boundary is cleaner than letting mixed dtypes spread through your model code.
Watch Variable Scopes and Reuse Logic
This error often appears in old TensorFlow 1.x style code where helper functions create variables under reusable scopes. The problem is not always the visible dtype= argument. Sometimes one helper creates a variable with float64 because it inherits a tensor dtype, while another helper later tries to access the same name with an explicit float32.
A defensive pattern is to pass the dtype as a function argument and use it consistently:
This makes dtype decisions explicit instead of accidental.
Prefer Modern TensorFlow Patterns in New Code
If you are writing new TensorFlow code, avoid building around tf.compat.v1.get_variable unless you are maintaining a legacy graph-based model. Keras layers manage variable creation for you and reduce the chance of scope and dtype conflicts.
That does not remove dtype issues entirely, but it moves variable management into a more structured API.
Common Pitfalls
The most common cause is NumPy creating float64 data while the model expects float32. Another repeated issue is reusing a variable scope in helper functions without making dtype a deliberate parameter. Developers also copy old TensorFlow 1.x snippets that mix tf.float64 initializers with tf.float32 placeholders, which builds a graph that is inconsistent from the start. Finally, some fixes only change the second get_variable call and leave the original variable definition untouched, so the error returns as soon as that code path runs again.
Summary
- This error means TensorFlow found an existing variable name with a different dtype than the one you requested.
- Pick one dtype for the variable, usually
float32, and keep reuse calls consistent with it. - Cast NumPy arrays and other inputs before they flow into the graph.
- Make dtype an explicit part of helper APIs when variable scopes are reused.
- Prefer modern Keras layers for new code so TensorFlow manages variables more safely.

