Have you ever been working with TensorFlow and needed to use your tensor data in a non-TensorFlow environment? I’ve been there many times during my decade-plus career as a Python developer.
One of the most common operations I perform is converting TensorFlow tensors to NumPy arrays. This conversion is essential when you need to use your tensor data with libraries that expect NumPy arrays.
In this tutorial, I will walk you through different methods to convert tensors to NumPy arrays in TensorFlow, with practical examples that you can apply to your projects.
Methods to Convert Tensors to NumPy in TensorFlow
Before getting into the conversion methods, let’s understand why we need this conversion in the first place.
TensorFlow tensors are optimized for GPU acceleration and automatic differentiation. However, many Python libraries, such as Matplotlib, Pandas, and scikit-learn, expect NumPy arrays as input.
Converting tensors to NumPy arrays allows you to bridge these two worlds effortlessly.
1: Use the .numpy() Method
The simplest and most direct way to convert a TensorFlow tensor to a NumPy array is by using the .numpy() method in Python. This method is available for all TensorFlow EagerTensors.
Here’s how I use it in my day-to-day work:
import tensorflow as tf
import numpy as np
# Create a simple tensor
tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
# Convert to NumPy array
numpy_array = tensor.numpy()
print(f"Tensor: {tensor}")
print(f"NumPy array: {numpy_array}")
print(f"Type of numpy_array: {type(numpy_array)}")Output:
Tensor: [[1 2 3]
[4 5 6]]
NumPy array: [[1 2 3]
[4 5 6]]
Type of numpy_array: <class 'numpy.ndarray'>I executed the above example code and added the screenshot below.

This method is straightforward and works in most scenarios when you’re using TensorFlow 2.x with eager execution enabled (which is the default).
2: Use tf.make_ndarray()
Another approach I sometimes use, especially when dealing with protocol buffers, is the tf.make_ndarray() function in Python. This is particularly useful when working with models that are saved or serialized tensors.
import tensorflow as tf
# Create a TensorProto
tensor_proto = tf.make_tensor_proto([[1, 2, 3], [4, 5, 6]])
# Convert to NumPy array
numpy_array = tf.make_ndarray(tensor_proto)
print(f"NumPy array: {numpy_array}")
print(f"Type of numpy_array: {type(numpy_array)}")Output:
NumPy array: [[1 2 3]
[4 5 6]]
Type of numpy_array: <class 'numpy.ndarray'>I executed the above example code and added the screenshot below.

While this method is less common for everyday use, it’s valuable when working with TensorFlow’s serialization formats.
Read TensorFlow One_Hot Encoding
3: Use np.array() Function
If you’re working with TensorFlow 1.x or in graph mode, you might need to use the Python NumPy array constructor directly. Here’s how I do it:
import tensorflow as tf
import numpy as np
# Create a tensor
tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
# Convert to NumPy array
numpy_array = np.array(tensor)
print(f"Tensor: {tensor}")
print(f"NumPy array: {numpy_array}")
print(f"Type of numpy_array: {type(numpy_array)}")Output:
Tensor: [[1 2 3]
[4 5 6]]
NumPy array: [[1 2 3]
[4 5 6]]
Type of numpy_array: <class 'numpy.ndarray'>I executed the above example code and added the screenshot below.

This method works in many cases but might not be as efficient as the .numpy() method in TensorFlow 2.x.
Check out Iterate Over Tensor In TensorFlow
Convert Complex Tensors
When working on deep learning projects, I often need to convert more complex tensors, such as those resulting from model predictions. Here’s a real-world example from a project where I was classifying U.S. zip codes from images:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# Load a pre-trained model (simplified example)
model = tf.keras.models.load_model('zip_code_model.h5')
# Make a prediction (returns a tensor)
image = tf.io.read_file('sample_zip.jpg')
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize(image, [224, 224])
image = tf.expand_dims(image, 0) # Add batch dimension
predictions_tensor = model(image)
# Convert the predictions tensor to NumPy
predictions_numpy = predictions_tensor.numpy()
# Now we can use matplotlib to visualize the predictions
plt.figure(figsize=(10, 4))
plt.bar(range(10), predictions_numpy[0])
plt.xticks(range(10))
plt.xlabel('Digit')
plt.ylabel('Probability')
plt.title('U.S. ZIP Code Digit Recognition Probabilities')
plt.show()This example demonstrates how converting tensors to NumPy arrays enables us to use visualization libraries like Matplotlib, which don’t directly support TensorFlow tensors.
Convert Tensors with Gradients
One aspect that often confuses developers is how to handle tensors with gradients. When working with tensors that require gradient tracking, I use this approach:
import tensorflow as tf
# Create a model
x = tf.Variable([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
with tf.GradientTape() as tape:
y = tf.reduce_sum(x**2)
# Compute gradients
grad = tape.gradient(y, x)
# Convert gradient tensor to NumPy
grad_numpy = grad.numpy()
print(f"Gradient tensor: {grad}")
print(f"NumPy gradient: {grad_numpy}")This conversion works seamlessly even with tensors that have gradient information attached to them.
Read Use TensorFlow’s get_shape Function
Convert Tensors on GPU/TPU
When working with tensors on a GPU or TPU, I’ve found it’s essential to be cautious with conversions. Here’s how I handle this:
import tensorflow as tf
# Create a tensor on GPU (if available)
with tf.device('/GPU:0'):
tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
# For GPU tensors, we need to ensure the tensor is copied to CPU before conversion
cpu_tensor = tf.identity(tensor)
with tf.device('/CPU:0'):
numpy_array = cpu_tensor.numpy()
print(f"NumPy array: {numpy_array}")This approach ensures that the tensor is properly transferred from GPU memory to CPU memory before the conversion, preventing potential errors.
Check out Tensorflow Activation Functions
Convert Tensors in a TensorFlow Dataset
When working with TensorFlow Datasets, I often need to convert batches of tensors to NumPy arrays:
import tensorflow as tf
import numpy as np
# Create a simple dataset
dataset = tf.data.Dataset.from_tensor_slices([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
dataset = dataset.batch(2)
# Iterate and convert each batch
for batch in dataset:
numpy_batch = batch.numpy()
print(f"Tensor batch: {batch}")
print(f"NumPy batch: {numpy_batch}")
print(f"Type of numpy_batch: {type(numpy_batch)}")
print("---")This pattern is particularly useful when processing mini-batches of data during model training or evaluation.
Check out Tensorflow Gradient Descent in Neural Network
Best Practices for Tensor to NumPy Conversion
Over my years of experience, I’ve developed these best practices for converting tensors to NumPy arrays:
- Use
.numpy()whenever possible – It’s the most straightforward and efficient method in TensorFlow 2.x. - Be mindful of memory usage – Converting large tensors can consume significant memory as the data is copied from TensorFlow’s memory management to NumPy’s.
- Handle device placement carefully – Always ensure tensors are on the CPU before conversion if they were created on a GPU or TPU.
- Batch conversions when possible – When processing large datasets, convert tensors in batches rather than all at once to manage memory efficiently.
- Check for eager execution – Remember that
.numpy()only works in eager execution mode, which is the default in TensorFlow 2.x.
Converting tensors to NumPy arrays is a fundamental skill when working with TensorFlow. These methods have helped me seamlessly integrate TensorFlow with other Python libraries in countless projects.
Related tutorials you may like to read:
- Compile Neural Network in Tensorflow
- Build an Artificial Neural Network in Tensorflow
- Training a Neural Network in TensorFlow

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.