tensorflow
debugging
trainable variables
print variables
machine learning

While debugging, how to print all variables which is in list format who are trainable in Tensorflow?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When debugging a TensorFlow model, the most direct way to inspect learnable parameters is to print model.trainable_variables. That gives you the actual trainable tf.Variable objects that the optimizer will update. If your variables are nested inside layers or Python lists, flatten them first and print names, shapes, and small previews instead of dumping huge tensors blindly.

The Simplest Debug Print

For a Keras model, start here:

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Dense(8, input_shape=(4,), activation="relu"),
5    tf.keras.layers.Dense(1)
6])
7
8for var in model.trainable_variables:
9    print(var.name, var.shape)

This is usually the best first debugging view because it tells you:

  • which variables are trainable
  • how many there are
  • what their shapes are

That is often enough to catch architecture mistakes immediately.

If you need the actual numbers too, print them selectively.

python
for var in model.trainable_variables:
    print(var.name)
    print(var.numpy())

That works for small models, but it becomes unreadable very quickly. For large models, summary-style output is more useful.

python
for var in model.trainable_variables:
    values = var.numpy()
    print(var.name, values.shape, values.min(), values.max())

That often gives better debugging signal than thousands of raw numbers.

Nested Variable Lists

If variables are grouped inside nested layer structures or lists, flatten them first.

python
1nested = [layer.trainable_variables for layer in model.layers]
2flat_vars = tf.nest.flatten(nested)
3
4for var in flat_vars:
5    print(var.name, var.shape)

This is useful when you want one clean list even though the model structure itself is nested.

Inspect Layer by Layer

Sometimes the best debugging view is grouped by layer instead of flattened globally.

python
1for layer in model.layers:
2    print("Layer:", layer.name)
3    for var in layer.trainable_variables:
4        print("  ", var.name, var.shape)

That is especially helpful when checking whether a specific layer is trainable or frozen.

Frozen Layers Change the Output

A common reason variables seem to be missing is that some layer has trainable = False.

python
model.layers[0].trainable = False
print(len(model.trainable_variables))

So if the list looks incomplete, check layer trainability before assuming TensorFlow hid something from you.

Common Pitfalls

The biggest mistake is printing every value from a large model and creating output that is too noisy to inspect.

Another mistake is looking at layer.variables when the real question is specifically about trainable parameters.

A third issue is forgetting that frozen layers disappear from trainable_variables, which can make the list look unexpectedly short.

Summary

  • Use model.trainable_variables as the primary debugging view for learnable TensorFlow parameters
  • Print names and shapes first, then print values only when the tensors are small enough to inspect
  • Use tf.nest.flatten if your variables are nested inside lists or structures
  • Inspect layer-by-layer when you need structural debugging
  • If variables seem missing, check whether the corresponding layer is frozen

Course illustration
Course illustration

All Rights Reserved.