TensorFlow
Machine Learning
Deep Learning
Error Handling
Programming

Tensorflow Attempting to use uninitialized value beta1_power

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

The error about using uninitialized beta1_power usually comes from TensorFlow 1.x style graph execution when Adam optimizer variables were created but never initialized. beta1_power and beta2_power are internal state variables used by Adam to track momentum decay across training steps. If the graph contains them and you run training before initialization, TensorFlow stops with this error.

Why Adam Creates Extra Variables

Optimizers in TensorFlow do more than apply gradients. Adam maintains internal variables for moving averages and decay terms. In graph mode, those variables become part of the computation graph and must be initialized before use.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5x = tf.compat.v1.get_variable("x", shape=[], initializer=tf.compat.v1.zeros_initializer())
6loss = (x - 3.0) ** 2
7optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=0.1)
8train_op = optimizer.minimize(loss)

At this point, the graph contains optimizer state variables, including beta1_power, but nothing has initialized them yet.

Initialize After Building the Optimizer

The fix in TensorFlow 1.x style code is simple: build the whole graph first, including the optimizer and training op, then run the global variables initializer.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5x = tf.compat.v1.get_variable("x", shape=[], initializer=tf.compat.v1.zeros_initializer())
6loss = (x - 3.0) ** 2
7optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=0.1)
8train_op = optimizer.minimize(loss)
9
10init_op = tf.compat.v1.global_variables_initializer()
11
12with tf.compat.v1.Session() as sess:
13    sess.run(init_op)
14
15    for _ in range(20):
16        _, current_x, current_loss = sess.run([train_op, x, loss])
17
18    print(current_x, current_loss)

The important part is the order. If you create the initializer before creating the optimizer variables, those later variables will not be included in the initializer op.

A Common Ordering Bug

This mistake is easy to make:

python
init_op = tf.compat.v1.global_variables_initializer()
optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=0.1)
train_op = optimizer.minimize(loss)

Here init_op was created too early. The optimizer variables do not exist yet, so they remain uninitialized even though the session later runs the initializer.

The safe rule is:

  • build variables
  • build optimizer and train op
  • create the initializer
  • start the session and run the initializer

TensorFlow 2 Usually Avoids This Problem

In TensorFlow 2, eager execution and Keras training loops manage variable creation more automatically, so this exact error is much less common.

python
1import tensorflow as tf
2
3x = tf.Variable(0.0)
4optimizer = tf.keras.optimizers.Adam(learning_rate=0.1)
5
6for _ in range(20):
7    with tf.GradientTape() as tape:
8        loss = (x - 3.0) ** 2
9
10    gradients = tape.gradient(loss, [x])
11    optimizer.apply_gradients(zip(gradients, [x]))
12
13print(x.numpy())

If you are maintaining older graph-mode code, though, the initialization rules still matter.

Re-Creating Optimizers Mid-Session Can Also Trigger It

Another source of this error is rebuilding the optimizer or training op after initialization and then trying to run it in the same session. The new optimizer variables have never been initialized. If the graph changes, run the appropriate initializer again for the new variables or rebuild the initialization flow cleanly.

For example, if code conditionally swaps optimizers during experimentation, the session state can easily fall out of sync with the graph.

Diagnose by Listing Uninitialized Variables

If you are unsure which variables were missed, TensorFlow 1.x can report them.

python
with tf.compat.v1.Session() as sess:
    uninitialized = sess.run(tf.compat.v1.report_uninitialized_variables())
    print(uninitialized)

This is useful when the optimizer is not the only component creating late graph variables.

Common Pitfalls

  • Creating global_variables_initializer() before building the optimizer and train op.
  • Mixing TensorFlow 1.x graph-mode patterns with TensorFlow 2 assumptions.
  • Recreating optimizers after initialization and forgetting to initialize the new variables.
  • Running part of the graph in a session before all variables exist.
  • Assuming only model weights need initialization while optimizer state can be skipped.

Summary

  • 'beta1_power is an Adam optimizer state variable, not a model parameter.'
  • In TensorFlow 1.x graph mode, optimizer variables must be initialized before training.
  • Always create the optimizer and training op before creating the global initializer.
  • TensorFlow 2 largely avoids this problem through eager execution and Keras abstractions.
  • If the graph changes after initialization, initialize the newly created variables as well.

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.