How to Convert a Tensor to a NumPy Array in TensorFlow?

There are two ways to convert a Tensor to a NumPy array:

  • TensorFlow version 2.x — use tensor.numpy()
  • TensorFlow version 1.x — use tensor.eval(session=tf.compat.v1.Session())

Let’s dive into these two methods in greater detail.

Method 1: Explicit Tensor to NumPy Array Conversion in TensorFlow 2.x

To convert a tensor t to a NumPy array in TensorFlow version 2.0 and above, use the t.numpy() built-in method. The resulting object is a NumPy array of type numpy.ndarray.

Here’s a code example that converts tensor t to array a.

import tensorflow as tf

t = tf.constant([[1, 2], [4, 8]])
a = t.numpy()

print(a)
print(type(a))

The output of this code snippet shows that the result is a NumPy array:

[[1 2]
 [4 8]]
<class 'numpy.ndarray'>

You can try this yourself in an interactive Jupyter notebook with Colab:

Tensor to NumPy Array

Method 2: Automatic Conversion using NumPy Operations on Tensors

If you apply a NumPy operation on Tensors, the result will automatically be converted to a NumPy ndarray.

In the following code, we first create a Tensor and store it in variable t by creating a Tensor constant and using TensorFlow’s multiplication routine to show that the result of a TensorFlow operation is a Tensor data type.

Then, we perform the np.add() NumPy operation on the Tensor obtained through the previous operation. Naturally, the result is a NumPy ndarray so the conversion has been performed automatically by NumPy.

import numpy as np
import tensorflow as tf

# Create Tensor
t = tf.constant([[1, 2], [4, 8]])
t = tf.multiply(t, 2)
print(t)

# NumPy operation results in ndarray
a = np.add(t, 1)
print(a)

Note that if the Tensor may be held in GPU memory. In this case, the conversion may not be possible because NumPy relies on the host machine’s RAM that may be more limited than the GPU memory.

Method 3: Explicit Conversion of Tensors to NumPy Arrays in TensorFlow 1.x

To convert a tensor t to a NumPy array in TensorFlow versions 1.x (such as 1.14 and 1.15), use the t.eval() built-in method and pass the session argument like so: t.eval(session=tf.compat.v1.Session()). The resulting object is a NumPy array of type numpy.ndarray.

Session objects in TensorFlow hold the execution state and encapsulate the execution environments of Operation objects.

Here’s a code example that converts tensor t to array a.

%tensorflow_version 1.x
import tensorflow as tf 

t = tf.constant([[1, 2], [4, 8]])
a = t.eval(session=tf.compat.v1.Session())

print(a)
print(type(a))

The output is the same NumPy array

[[1 2]
 [4 8]]
<class 'numpy.ndarray'>

While this method works, it’s usually not needed because you don’t still use TF v1, do you? ๐Ÿ˜‰

Where to Go From Here

Thanks for reading the blog tutorial anyways—to keep improving your Python skills, why not download a couple of my hand-crafted Python, machine learning, and NumPy cheat sheets? My subscribers love them!

Just sign up to the Finxter programming email academy and you’ll be able to instantly download the cheat sheets here:

Join us, it’s fun!