TensorFlow
tf.get_variable
numpy
variable initialization
machine learning

How does one initialize a variable with tf.get_variable and a numpy value 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

If you are using TensorFlow 1 style code, you can initialize a variable from a NumPy array by passing that array as the initializer to tf.get_variable. The main caveat is that tf.get_variable belongs to the TensorFlow 1 graph-style API, so in modern TensorFlow you usually reach it through tf.compat.v1.get_variable, while TensorFlow 2 code usually prefers plain tf.Variable.

Initialize From a NumPy Array in TensorFlow 1 Style

When you already have a NumPy value, the simplest pattern is to pass it directly as the initializer.

python
1import numpy as np
2import tensorflow as tf
3
4tf.compat.v1.disable_eager_execution()
5
6initial_value = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32)
7
8weights = tf.compat.v1.get_variable(
9    "weights",
10    initializer=initial_value
11)
12
13with tf.compat.v1.Session() as sess:
14    sess.run(tf.compat.v1.global_variables_initializer())
15    print(sess.run(weights))

TensorFlow can infer the shape from the NumPy array in this case, so you usually do not need to specify shape separately unless your code structure requires it.

Use a Constant Initializer When You Want the Intent to Be Explicit

Another common style is to wrap the NumPy value in a constant initializer. This makes it clear that the variable starts from a fixed tensor rather than a random initializer.

python
1import numpy as np
2import tensorflow as tf
3
4tf.compat.v1.disable_eager_execution()
5
6initial_value = np.array([0.5, 0.25, 0.125], dtype=np.float32)
7
8vector = tf.compat.v1.get_variable(
9    "vector",
10    shape=initial_value.shape,
11    dtype=tf.float32,
12    initializer=tf.compat.v1.constant_initializer(initial_value)
13)

This form is useful when you want the dtype and shape written explicitly or when the initializer code should look consistent with other variable definitions.

Know the Reuse Rules

tf.get_variable is different from tf.Variable because it participates in variable sharing and reuse. If your code runs inside a variable scope, TensorFlow expects repeated requests for the same variable name to follow the reuse rules.

python
1import numpy as np
2import tensorflow as tf
3
4tf.compat.v1.disable_eager_execution()
5
6initial_value = np.array([1.0, 2.0], dtype=np.float32)
7
8with tf.compat.v1.variable_scope("layer"):
9    w1 = tf.compat.v1.get_variable("w", initializer=initial_value)
10
11with tf.compat.v1.variable_scope("layer", reuse=True):
12    w2 = tf.compat.v1.get_variable("w")

That matters because many older TensorFlow examples use get_variable specifically for reusable weights in layered model code.

TensorFlow 2 Usually Uses tf.Variable

If you are not tied to legacy graph code, the modern equivalent is simpler:

python
1import numpy as np
2import tensorflow as tf
3
4initial_value = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32)
5weights = tf.Variable(initial_value, name="weights")
6
7print(weights.numpy())

This is often the better answer in current TensorFlow projects because eager execution is on by default and tf.Variable fits naturally with tf.keras layers and model code.

Shape and Dtype Must Still Match

Even though the NumPy array can define the starting value, TensorFlow still expects the declared dtype and shape to agree with that array. If the array is float64 and the variable expects float32, TensorFlow may raise an error or force you to cast explicitly.

A safe pattern is:

python
initial_value = np.asarray(initial_value, dtype=np.float32)

That avoids many silent mismatches before the graph is built.

Common Pitfalls

The first mistake is mixing TensorFlow 1 and TensorFlow 2 styles without realizing it. tf.get_variable is a graph-era API, so if you are in a modern eager-execution environment, tf.Variable is often the simpler option.

Another common issue is specifying a shape that does not match the NumPy initializer. If you provide both, they must agree exactly.

Developers also sometimes forget to run global_variables_initializer() in TensorFlow 1 session-based code. The variable definition alone does not create a usable runtime value until the graph initializer runs.

Finally, reuse behavior can surprise people. If you call get_variable twice with the same name in the same scope without the correct reuse setting, TensorFlow raises an error instead of silently creating a second variable.

Summary

  • In TensorFlow 1 style code, pass the NumPy array as the initializer to tf.compat.v1.get_variable.
  • Use constant_initializer if you want the fixed starting value to be explicit.
  • Make sure dtype and shape match the NumPy array.
  • Run variable initialization before reading the value in session-based code.
  • In modern TensorFlow 2 code, prefer tf.Variable unless you specifically need legacy variable-scope behavior.

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
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
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