TensorFlow
machine learning
model weights
neural networks
Python

Get the value of some weights in a model trained by TensorFlow

Master System Design with Codemia

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

Introduction

Reading trained weights from a TensorFlow model is a common debugging and inspection task. The exact API depends on whether you are using Keras layers, a subclassed model, or low-level tf.Variable objects, but the core idea is the same: find the variable you want, then read its current tensor value.

Inspect Weights in a Keras Model

For Keras models, the quickest path is to inspect model.weights, model.trainable_weights, or layer.get_weights().

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(4,)),
5    tf.keras.layers.Dense(3, activation="relu", name="hidden"),
6    tf.keras.layers.Dense(1, name="output"),
7])
8
9model.compile(optimizer="adam", loss="mse")
10
11# Build the model by calling it once
12_ = model(tf.zeros((1, 4)))
13
14for var in model.weights:
15    print(var.name, var.shape)

Each entry is a tf.Variable. You can read its numeric value with .numpy() when eager execution is enabled, which is the default in modern TensorFlow.

python
1hidden_kernel = model.get_layer("hidden").weights[0]
2hidden_bias = model.get_layer("hidden").weights[1]
3
4print(hidden_kernel.numpy())
5print(hidden_bias.numpy())

That gives you the raw NumPy arrays for the selected layer.

Read a Specific Layer or Weight Matrix

If you only care about one part of the network, naming layers helps a lot. It lets you avoid fragile index-based access across the full model.

python
1layer = model.get_layer("output")
2kernel, bias = layer.get_weights()
3
4print("kernel shape:", kernel.shape)
5print("bias shape:", bias.shape)
6print("kernel first row:", kernel[0])

get_weights() returns plain NumPy arrays, which is convenient for logging, exporting, or small numerical checks. If you need TensorFlow ops afterward, keep the original variable and avoid converting too early.

A useful pattern for larger models is to print names and filter.

python
for var in model.trainable_weights:
    if "hidden" in var.name:
        print(var.name, var.numpy().mean())

This is often enough to confirm whether a layer is learning at all.

Access Weights in a Subclassed or Low-Level Model

If you built the model from tf.Module or custom tf.Variable members, you usually read from those variables directly.

python
1import tensorflow as tf
2
3class LinearModel(tf.Module):
4    def __init__(self):
5        super().__init__()
6        self.w = tf.Variable([[0.5], [1.5]], dtype=tf.float32, name="w")
7        self.b = tf.Variable([0.2], dtype=tf.float32, name="b")
8
9    def __call__(self, x):
10        return tf.matmul(x, self.w) + self.b
11
12model = LinearModel()
13print(model.w.numpy())
14print(model.b.numpy())

For subclassed tf.keras.Model, the same idea applies after the variables are created by calling the model at least once.

python
1class MyModel(tf.keras.Model):
2    def __init__(self):
3        super().__init__()
4        self.dense = tf.keras.layers.Dense(2)
5
6    def call(self, inputs):
7        return self.dense(inputs)
8
9model = MyModel()
10_ = model(tf.zeros((1, 3)))
11print(model.trainable_variables[0].numpy())

If you skip the initial call, the layer may not have created its weights yet.

Inspect Saved Weights Safely

When weights were loaded from a checkpoint or SavedModel, first restore them, then inspect the variables.

python
1model = tf.keras.Sequential([
2    tf.keras.layers.Input(shape=(4,)),
3    tf.keras.layers.Dense(3, name="dense_a"),
4])
5
6_ = model(tf.zeros((1, 4)))
7model.load_weights("weights.ckpt")
8
9for var in model.weights:
10    print(var.name, var.numpy())

Be careful when matching layer names and shapes. TensorFlow restores by structure and name expectations, so a silent architectural mismatch usually turns into a loading error rather than a trustworthy value.

Common Pitfalls

  • Trying to read weights before the model or layer has been built leaves you with missing variables.
  • Relying on raw numeric indexes across model.get_weights() becomes brittle when the architecture changes.
  • Confusing get_weights() output, which is NumPy arrays, with TensorFlow variables can break downstream code that expects tensors.
  • Inspecting a checkpoint before restoring it into a matching model gives misleading results.
  • Forgetting whether you want trainable variables only or all variables, including non-trainable state such as batch-normalization statistics, leads to incomplete inspection.

Summary

  • Use layer.weights, model.weights, or get_weights() to inspect Keras-trained parameters.
  • Use .numpy() to read variable values under eager execution.
  • Name layers so specific weight lookup is stable and readable.
  • For subclassed and low-level models, read the underlying tf.Variable objects directly.
  • Build or restore the model before inspecting weights so the variables actually exist.

Course illustration
Course illustration

All Rights Reserved.