TensorFlow getting variable by name
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
In TensorFlow 2.x, variables are standard Python objects — you access them by keeping a reference to the tf.Variable object. The TF 1.x approach of getting variables by string name (tf.get_variable, tf.trainable_variables()) is largely deprecated. In TF 2.x, Keras layers and models expose their variables through .trainable_variables, .non_trainable_variables, and .variables properties. For legacy TF 1.x code, variables were retrieved from the default graph using tf.compat.v1.get_variable() with variable scopes, or filtered from tf.compat.v1.global_variables() by name.
TensorFlow 2.x: Accessing Variables by Reference
Finding Variables by Name in TF 2.x
Keras Model Variable Access Patterns
TF 1.x Legacy: Variable Scopes and get_variable
Checkpoint Inspection and Variable Loading
Transfer Learning: Accessing Pretrained Variables
Common Pitfalls
- Using
tf.compat.v1.get_variablein TF 2.x eager mode:get_variablerequires variable scopes and graph mode. In TF 2.x with eager execution (default), usetf.Variabledirectly and keep Python references. Mixing TF 1.x and TF 2.x variable APIs causes confusing behavior. - Variable name collisions: Creating two
tf.Variableobjects with the samenameparameter does not merge them — TF appends suffixes (weights:0,weights_1:0). Each is a separate variable. In TF 1.x,get_variablewithreuse=Truereturned the same variable; TF 2.x has no equivalent. - Forgetting to build the model before accessing variables:
model.trainable_variablesis empty until the model processes its first input. Callmodel(sample_input)ormodel.build(input_shape)before listing variables. - Checkpoint variable name mismatch after refactoring: Renaming layers or restructuring a model changes variable names in checkpoints.
tf.train.Checkpointuses Python attribute names for matching, so renamingself.dense1toself.encoderbreaks checkpoint restoration. - Accessing
:0suffix in variable names: TF variable names include a:0suffix (e.g.,weights:0). When filtering by name, usestartswithorinrather than exact equality. The:0indicates the output index and is always present.
Summary
- In TF 2.x, access variables through Python references or
model.trainable_variables - Use
model.get_layer("name")to access specific layer variables in Keras - Filter variables by name with list comprehensions on
model.variables - For TF 1.x legacy code, use
tf.compat.v1.get_variablewithvariable_scopeandreuse=True - Inspect checkpoint variables with
tf.train.load_checkpoint()andget_variable_to_shape_map() - Always build the model before accessing variables — call
model(sample_input)first
Related reading
- Tensorflow GPU Could not load dynamic library 'cusolver64_10.dll'; dlerror cusolver64_10.dll not found
- TensorFlow GPU is cudnn optional? Couldn't open CUDA library libcudnn.so
- Tensorflow GPU utilization only 60 GTX 1070
- Tensorflow GradientTape Gradients does not exist for variables intermittently
- Tensorflow Graph is finalized and cannot be modified
- ''tensorflow'' has no attribute ''config''
- ''tensorflow'' has no attribute ''config''
- ''tensorflow'' has no attribute ''to_int32''
.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.