How to understand tf.get_collection in TensorFlow
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
tf.get_collection() is a TensorFlow 1.x graph-management tool. It lets you retrieve tensors, variables, or operations that were grouped into a named collection, which was useful when TensorFlow code was built around symbolic graphs and sessions rather than eager execution.
What a Collection Actually Is
In TensorFlow 1, a collection is just a named list attached to the current graph. TensorFlow itself uses built-in collections for common graph objects, and you can create your own collections to organize related nodes.
Examples of built-in collection names include:
- '
GLOBAL_VARIABLES' - '
TRAINABLE_VARIABLES' - '
SUMMARIES' - '
LOSSES'
The idea is simple: instead of keeping every important graph object in your own Python lists, you can register it with the graph and look it up later.
Adding and Reading Values
The basic pattern is:
- add something with
tf.add_to_collection - retrieve it later with
tf.get_collection
Here is a minimal example using the TensorFlow 1 compatibility API:
items is a Python list containing the graph objects stored under that collection name.
This mattered in larger graphs where different parts of the program needed to find losses, summaries, or outputs without passing them through many function layers manually.
Default Collections Are the Most Common Use
Many TensorFlow 1 APIs automatically populate standard collections. For example, variables created with tf.Variable are added to the global-variable collection, and trainable variables are also tracked separately.
That is why code such as optimizer setup, checkpoint logic, or summary export often relied on collections instead of custom bookkeeping.
The scope Argument Filters Results
tf.get_collection(name, scope=...) can filter the collection contents by graph scope. This is useful when the same kind of object exists in several subgraphs or model blocks.
This is especially helpful in multi-tower or multi-module TensorFlow 1 graphs.
get_collection() Versus get_collection_ref()
tf.get_collection() returns a list copy of the collection contents. If you want the actual underlying list object stored in the graph, TensorFlow also has tf.get_collection_ref().
That distinction matters because mutating the list returned by get_collection() does not update the graph collection itself. Most code should use get_collection() unless it intentionally wants to modify the underlying collection structure.
Why You See It Less in TensorFlow 2
TensorFlow 2 uses eager execution by default, so graph collections are far less central to everyday code. Modern Keras and eager workflows usually keep references directly in Python objects instead of relying on graph-level registries.
So when you see tf.get_collection(), you are almost always reading TensorFlow 1 style code or tf.compat.v1 compatibility code.
Common Pitfalls
- Treating collections as magical runtime values rather than named lists attached to a graph.
- Forgetting that
tf.get_collection()is primarily a TensorFlow 1 graph-management feature. - Expecting changes to the returned list to mutate the graph collection automatically.
- Overusing custom collections when ordinary Python references would be simpler in modern TensorFlow code.
- Confusing collection lookup with actual computation, because retrieving a tensor from a collection does not execute it.
Summary
- '
tf.get_collection()retrieves graph objects stored in a named TensorFlow 1 collection.' - Collections are useful for organizing variables, losses, summaries, and custom graph objects.
- TensorFlow provides several built-in collection names through
GraphKeys. - The optional
scopefilter helps limit results to one part of the graph. - In TensorFlow 2, collections are mostly legacy concepts used when reading or maintaining older code.
Related reading
- How to understand the term tensor in TensorFlow?
- How to understand the term tensor in TensorFlow?
- How to understand this LSTM example?
- How to update model parameters with accumulated gradients?
- How to understand the Densely Connected Layer section in tensorflow tutorial
- How to update Tensorflow on mac?
- How to understand the functional margin in SVM ?
- How to understand the output of Topic Model class in Mallet?
.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.