Tensorflow get all variables in scope
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, variable scopes organize variables into named groups, making it possible to share variables and retrieve them by prefix. Getting all variables within a specific scope is essential for tasks like saving/restoring model subsets, freezing layers during transfer learning, computing per-scope statistics, and debugging model architecture. TensorFlow 1.x uses tf.get_collection() and tf.trainable_variables() with scope filtering, while TensorFlow 2.x uses tf.Module and Keras layer properties.
TensorFlow 1.x: Variable Scopes
tf.get_collection() filters variables by scope name prefix. TRAINABLE_VARIABLES includes only variables that participate in gradient computation, while GLOBAL_VARIABLES includes all variables.
Using tf.trainable_variables with Scope Filter
The scope parameter in tf.get_collection() is actually a regex pattern, so scope="model/" matches all variables whose names start with model/.
TensorFlow 2.x: Keras Layer Variables
Using tf.Module in TF2
tf.Module automatically tracks variables in submodules. Access .trainable_variables on any submodule to get its scoped variables.
Practical Use Cases
Migrating from TF1 to TF2
Common Pitfalls
- Using TF1 APIs in TF2 without compat mode:
tf.get_collection()andtf.variable_scope()do not work in TF2 eager mode. Usetf.compat.v1for migration or switch totf.Module/ Keras layer properties. - Forgetting to build the model before accessing variables: Variables are created lazily in TF2. Calling
model.trainable_variablesbefore passing data through the model returns an empty list. Callmodel.build(input_shape)or pass a dummy input first. - Scope regex matching unintended variables: The
scopeparameter intf.get_collection()is a regex.scope="model"matches bothmodel/layer1andmy_model/layer1. Usescope="model/"with a trailing slash for exact prefix matching. - Confusing
trainable_variableswithvariables:trainable_variablesexcludes batch normalization running means/variances and other non-trainable state. Usevariables(ornon_trainable_variables) when you need the complete set for saving or inspection. - Variable name collisions across scopes: In TF1, creating variables with the same name in different scopes appends
_1,_2suffixes. In TF2 with Keras, each layer has a unique name. Always verify variable names with[v.name for v in model.trainable_variables]to confirm scope structure.
Summary
- TF1: Use
tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="name")to get scoped variables - TF2 Keras: Use
model.get_layer("name").trainable_variablesper layer - TF2 Module: Use
module.submodule.trainable_variablesfor hierarchical access - Build the model before accessing variables — TF2 creates them lazily
- Use scoped variable access for transfer learning, per-layer learning rates, and selective checkpointing
- The
scopeparameter is regex-based — use trailing slashes for exact prefix matching
Related reading
- Tensorflow Get difference between each row/columns in Tensor
- Tensorflow Get indices of array rows which are zero
- Tensorflow get_single_element not working with tf.data.TFRecordDataset.batch
- Tensorflow GetNext failed because the iterator has not been initialized
- TensorFlow getting all states from a `RNN`
- TensorFlow getting all states from a \`RNN\`
- TensorFlow getting elements of every row for specific columns
- Tensorflow Getting scalar tensor value as int for pass to set_shape
.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.