TensorFlow
machine learning
variable management
programming
tutorial

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.

Practice ML system design

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

python
1import tensorflow as tf
2
3# Create named variables
4weights = tf.Variable(tf.random.normal([784, 128]), name="weights")
5bias = tf.Variable(tf.zeros([128]), name="bias")
6
7# Access directly — they're just Python objects
8print(weights.name)       # "weights:0"
9print(weights.shape)      # (784, 128)
10print(weights.trainable)  # True
11
12# In a Keras model — variables are managed by layers
13model = tf.keras.Sequential([
14    tf.keras.layers.Dense(128, name="hidden", input_shape=(784,)),
15    tf.keras.layers.Dense(10, name="output")
16])
17
18# Access all variables
19for var in model.trainable_variables:
20    print(f"{var.name}: {var.shape}")
21# hidden/kernel:0: (784, 128)
22# hidden/bias:0: (128)
23# output/kernel:0: (128, 10)
24# output/bias:0: (10)

Finding Variables by Name in TF 2.x

python
1# Filter model variables by name
2def get_variable_by_name(model, name):
3    for var in model.variables:
4        if name in var.name:
5            return var
6    return None
7
8kernel = get_variable_by_name(model, "hidden/kernel")
9print(kernel.shape)  # (784, 128)
10
11# Get variables for a specific layer
12hidden_layer = model.get_layer("hidden")
13print(hidden_layer.kernel.name)  # "hidden/kernel:0"
14print(hidden_layer.bias.name)    # "hidden/bias:0"
15
16# Access by layer name
17for layer in model.layers:
18    print(f"Layer: {layer.name}")
19    for var in layer.trainable_variables:
20        print(f"  {var.name}: {var.shape}")

Keras Model Variable Access Patterns

python
1# Custom model with named variables
2class MyModel(tf.keras.Model):
3    def __init__(self):
4        super().__init__()
5        self.dense1 = tf.keras.layers.Dense(128, name="encoder")
6        self.dense2 = tf.keras.layers.Dense(10, name="classifier")
7        self.custom_var = tf.Variable(1.0, name="temperature", trainable=True)
8
9    def call(self, x):
10        x = tf.nn.relu(self.dense1(x))
11        return self.dense2(x) / self.custom_var
12
13model = MyModel()
14model(tf.zeros([1, 784]))  # Build the model
15
16# All trainable variables (includes custom_var)
17for var in model.trainable_variables:
18    print(var.name)
19# encoder/kernel:0
20# encoder/bias:0
21# classifier/kernel:0
22# classifier/bias:0
23# temperature:0
24
25# Non-trainable variables (e.g., BatchNorm moving averages)
26for var in model.non_trainable_variables:
27    print(var.name)

TF 1.x Legacy: Variable Scopes and get_variable

python
1# TF 1.x style (use tf.compat.v1 in TF 2.x)
2import tensorflow as tf
3
4# Disable eager execution for TF 1.x compatibility
5tf.compat.v1.disable_eager_execution()
6
7with tf.compat.v1.variable_scope("encoder"):
8    weights = tf.compat.v1.get_variable(
9        "weights", shape=[784, 128],
10        initializer=tf.compat.v1.glorot_uniform_initializer()
11    )
12    bias = tf.compat.v1.get_variable(
13        "bias", shape=[128],
14        initializer=tf.compat.v1.zeros_initializer()
15    )
16
17# Retrieve by name
18all_vars = tf.compat.v1.global_variables()
19for v in all_vars:
20    print(v.name)
21# encoder/weights:0
22# encoder/bias:0
23
24# Filter by scope name
25encoder_vars = [v for v in all_vars if v.name.startswith("encoder/")]
26
27# Reuse an existing variable
28with tf.compat.v1.variable_scope("encoder", reuse=True):
29    same_weights = tf.compat.v1.get_variable("weights")
30    # Returns the SAME variable object, not a new one
31    assert same_weights is weights

Checkpoint Inspection and Variable Loading

python
1# Save a checkpoint
2checkpoint = tf.train.Checkpoint(model=model)
3checkpoint.save("./checkpoints/my_model")
4
5# Inspect variable names in a checkpoint
6reader = tf.train.load_checkpoint("./checkpoints/my_model-1")
7var_to_shape = reader.get_variable_to_shape_map()
8for name, shape in sorted(var_to_shape.items()):
9    print(f"{name}: {shape}")
10# model/encoder/kernel/.ATTRIBUTES/VARIABLE_VALUE: [784, 128]
11# model/encoder/bias/.ATTRIBUTES/VARIABLE_VALUE: [128]
12
13# Load specific variable from checkpoint
14tensor = reader.get_tensor("model/encoder/kernel/.ATTRIBUTES/VARIABLE_VALUE")
15print(tensor.shape)  # (784, 128)
python
1# SavedModel inspection
2loaded = tf.saved_model.load("./saved_model")
3for var in loaded.variables:
4    print(f"{var.name}: {var.shape}")
5
6# Restore specific variables
7checkpoint = tf.train.Checkpoint(encoder=model.dense1)
8checkpoint.restore("./checkpoints/my_model-1")
9# Only restores variables matching the encoder layer

Transfer Learning: Accessing Pretrained Variables

python
1# Load a pretrained model and access specific layers
2base_model = tf.keras.applications.ResNet50(weights="imagenet", include_top=False)
3
4# Find a specific layer's variables
5for layer in base_model.layers:
6    if "conv2" in layer.name:
7        print(f"{layer.name}: {[v.name for v in layer.trainable_variables]}")
8
9# Freeze specific layers by name
10for layer in base_model.layers:
11    if "conv1" in layer.name or "conv2" in layer.name:
12        layer.trainable = False
13
14# Access specific layer weights
15conv1_kernel = base_model.get_layer("conv1_conv").kernel
16print(conv1_kernel.shape)  # (7, 7, 3, 64)

Common Pitfalls

  • Using tf.compat.v1.get_variable in TF 2.x eager mode: get_variable requires variable scopes and graph mode. In TF 2.x with eager execution (default), use tf.Variable directly and keep Python references. Mixing TF 1.x and TF 2.x variable APIs causes confusing behavior.
  • Variable name collisions: Creating two tf.Variable objects with the same name parameter does not merge them — TF appends suffixes (weights:0, weights_1:0). Each is a separate variable. In TF 1.x, get_variable with reuse=True returned the same variable; TF 2.x has no equivalent.
  • Forgetting to build the model before accessing variables: model.trainable_variables is empty until the model processes its first input. Call model(sample_input) or model.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.Checkpoint uses Python attribute names for matching, so renaming self.dense1 to self.encoder breaks checkpoint restoration.
  • Accessing :0 suffix in variable names: TF variable names include a :0 suffix (e.g., weights:0). When filtering by name, use startswith or in rather than exact equality. The :0 indicates 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_variable with variable_scope and reuse=True
  • Inspect checkpoint variables with tf.train.load_checkpoint() and get_variable_to_shape_map()
  • Always build the model before accessing variables — call model(sample_input) first

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.