tensorflow
tf.get_collection
machine learning
deep learning
python

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.

Practice ML system design

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:

  1. add something with tf.add_to_collection
  2. retrieve it later with tf.get_collection

Here is a minimal example using the TensorFlow 1 compatibility API:

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5x = tf.compat.v1.placeholder(tf.float32, name="x")
6y = tf.compat.v1.placeholder(tf.float32, name="y")
7z = x + y
8
9tf.compat.v1.add_to_collection("my_outputs", z)
10
11items = tf.compat.v1.get_collection("my_outputs")
12print(items)

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.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5w = tf.Variable(1.0, name="weight")
6b = tf.Variable(0.0, name="bias", trainable=False)
7
8global_vars = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.GLOBAL_VARIABLES)
9trainable_vars = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.TRAINABLE_VARIABLES)
10
11print("global:", [v.name for v in global_vars])
12print("trainable:", [v.name for v in trainable_vars])

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.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5with tf.compat.v1.variable_scope("encoder"):
6    e = tf.Variable(1.0, name="w")
7
8with tf.compat.v1.variable_scope("decoder"):
9    d = tf.Variable(2.0, name="w")
10
11encoder_vars = tf.compat.v1.get_collection(
12    tf.compat.v1.GraphKeys.GLOBAL_VARIABLES,
13    scope="encoder"
14)
15
16print([v.name for v in encoder_vars])

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 scope filter 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
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.