TensorFlow 2.x

Eager Execution is enabled by default, so just call .numpy() on the Tensor object.

import tensorflow as tf

a = tf.constant([[1, 2], [3, 4]])                 
b = tf.add(a, 1)

a.numpy()
# array([[1, 2],
#        [3, 4]], dtype=int32)

b.numpy()
# array([[2, 3],
#        [4, 5]], dtype=int32)

tf.multiply(a, b).numpy()
# array([[ 2,  6],
#        [12, 20]], dtype=int32)

See NumPy Compatibility for more. It is worth noting (from the docs),

Numpy array may share a memory with the Tensor object. Any changes to one may be reflected in the other.

Bold emphasis mine. A copy may or may not be returned, and this is an implementation detail based on whether the data is in CPU or GPU (in the latter case, a copy has to be made from GPU to host memory).

But why am I getting the AttributeError: 'Tensor' object has no attribute 'numpy'?.
A lot of folks have commented about this issue, there are a couple of possible reasons:

  • TF 2.0 is not correctly installed (in which case, try re-installing), or
  • TF 2.0 is installed, but eager execution is disabled for some reason. In such cases, call tf.compat.v1.enable_eager_execution() to enable it, or see below.

If Eager Execution is disabled, you can build a graph and then run it through tf.compat.v1.Session:

a = tf.constant([[1, 2], [3, 4]])                 
b = tf.add(a, 1)
out = tf.multiply(a, b)

out.eval(session=tf.compat.v1.Session())    
# array([[ 2,  6],
#        [12, 20]], dtype=int32)

See also TF 2.0 Symbols Map for a mapping of the old API to the new one.

Answer from coldspeed95 on Stack Overflow
🌐
TensorFlow
tensorflow.org › tensorflow core › numpy api on tensorflow
NumPy API on TensorFlow | TensorFlow Core
August 15, 2024 - TensorFlow APIs leave tf.Tensor inputs unchanged and do not perform type promotion on them, while TensorFlow NumPy APIs promote all inputs according to NumPy type promotion rules. In the next example, you will perform type promotion. First, run addition on ND array inputs of different types and note the output types.
🌐
TensorFlow
tensorflow.org › tensorflow core › customization basics: tensors and operations
Customization basics: tensors and operations | TensorFlow Core
August 16, 2024 - Tensors are explicitly converted to NumPy ndarrays using their .numpy() method. These conversions are typically cheap since the array and tf.Tensor share the underlying memory representation, if possible.
Top answer
1 of 13
251

TensorFlow 2.x

Eager Execution is enabled by default, so just call .numpy() on the Tensor object.

import tensorflow as tf

a = tf.constant([[1, 2], [3, 4]])                 
b = tf.add(a, 1)

a.numpy()
# array([[1, 2],
#        [3, 4]], dtype=int32)

b.numpy()
# array([[2, 3],
#        [4, 5]], dtype=int32)

tf.multiply(a, b).numpy()
# array([[ 2,  6],
#        [12, 20]], dtype=int32)

See NumPy Compatibility for more. It is worth noting (from the docs),

Numpy array may share a memory with the Tensor object. Any changes to one may be reflected in the other.

Bold emphasis mine. A copy may or may not be returned, and this is an implementation detail based on whether the data is in CPU or GPU (in the latter case, a copy has to be made from GPU to host memory).

But why am I getting the AttributeError: 'Tensor' object has no attribute 'numpy'?.
A lot of folks have commented about this issue, there are a couple of possible reasons:

  • TF 2.0 is not correctly installed (in which case, try re-installing), or
  • TF 2.0 is installed, but eager execution is disabled for some reason. In such cases, call tf.compat.v1.enable_eager_execution() to enable it, or see below.

If Eager Execution is disabled, you can build a graph and then run it through tf.compat.v1.Session:

a = tf.constant([[1, 2], [3, 4]])                 
b = tf.add(a, 1)
out = tf.multiply(a, b)

out.eval(session=tf.compat.v1.Session())    
# array([[ 2,  6],
#        [12, 20]], dtype=int32)

See also TF 2.0 Symbols Map for a mapping of the old API to the new one.

2 of 13
146

Any tensor returned by Session.run or eval is a NumPy array.

>>> print(type(tf.Session().run(tf.constant([1,2,3]))))
<class 'numpy.ndarray'>

Or:

>>> sess = tf.InteractiveSession()
>>> print(type(tf.constant([1,2,3]).eval()))
<class 'numpy.ndarray'>

Or, equivalently:

>>> sess = tf.Session()
>>> with sess.as_default():
>>>    print(type(tf.constant([1,2,3]).eval()))
<class 'numpy.ndarray'>

EDIT: Not any tensor returned by Session.run or eval() is a NumPy array. Sparse Tensors for example are returned as SparseTensorValue:

>>> print(type(tf.Session().run(tf.SparseTensor([[0, 0]],[1],[1,2]))))
<class 'tensorflow.python.framework.sparse_tensor.SparseTensorValue'>
🌐
TensorFlow
tensorflow.org › tensorflow core › introduction to tensors
Introduction to Tensors | TensorFlow Core
You can convert a tensor to a NumPy array either using np.array or the tensor.numpy method:
🌐
MachineLearningMastery
machinelearningmastery.com › home › blog › integrating tensorflow and numpy for custom operations
Integrating TensorFlow and NumPy for Custom Operations - MachineLearningMastery.com
February 17, 2025 - Scientific Computing: Conduct scientific simulations in NumPy, using TensorFlow to optimize parameters or run simulations on GPUs. This combination bridges scientific computing and machine learning. Automated Differentiation: Using tf.experimental.numpy, operations performed on tensors automatically gain gradient support, enabling machine learning tasks with NumPy-like syntax while utilizing TensorFlow’s optimization capabilities.
🌐
Python Guides
pythonguides.com › tensorflow-tensor-to-numpy
Convert Tensor To NumPy In TensorFlow - Python Guides
June 9, 2025 - Converting tensors to NumPy arrays allows you to bridge these two worlds effortlessly. The simplest and most direct way to convert a TensorFlow tensor to a NumPy array is by using the .numpy() method in Python.
🌐
Saturn Cloud
saturncloud.io › blog › convert-a-tensor-to-a-numpy-array-in-tensorflow
Convert a Tensor to a Numpy Array in Tensorflow | Saturn Cloud Blog
June 13, 2023 - Tensors are used to represent the inputs and outputs of your TensorFlow models, as well as the intermediate values computed during training or inference. Tensors can have any number of dimensions, from 0 (a scalar) to n (a tensor with n dimensions).
Find elsewhere
Top answer
1 of 2
23

I think it may be worth adding a bit more of information, although it is easy to find about it just searching around a bit.

NumPy and TensorFlow are actually very similar in many respects. Both are, essentially, array manipulation libraries, built around the concept of tensors (or nd-arrays, in NumPy terms). Originally, in TensorFlow 0.x and 1.x, there was only "graph mode", with all values being "symbolic tensors" that did not have a specific value until one was fed at a later point... It was a bit confusing and quite different from NumPy. Nowadays "graph mode" still exists but, for the most part, TensorFlow 2.x works in "eager mode", where each tensor has a specific value. This makes it more similar to NumPy, so the differences may seem subtle. So maybe we can draft a list with some of the most significant points.

  • NumPy was developed as a full-fledged open source tensor algebra package for Python that could rival MATLAB and the likes. It is a Python library with a long history and plenty of functionality, either directly in it or built around it (see SciPy and different scikits). TensorFlow was developed by Google much more recently specifically for the purpose of building machine learning models (although you could use it for many other tasks), continuing the ideas from the (now discontinued) Theano library. Although TensorFlow is most commonly used with Python, it can be used in C/C++ and other languages too, which is important because it allows you to train a model in Python and then integrate it in an existing application written in another language.
  • A main selling point of TensorFlow is that it can automatically differentiate computations. This is an essential feature for deep learning, that uses gradient-based optimization (backpropagation), and it means that you can pretty much just write whatever you want to compute and TensorFlow will figure out the gradients by itself. There are things like Autograd or JAX for NumPy, but they are not as powerful as TensorFlow automatic differentiation, which actually maintains a computation graph structure under the hood (the name "TensorFlow" refers to the tensors and their gradients "flowing" through the computation graph).
  • TensorFlow offers GPU computation with CUDA out of the box. Again there are things like CuPy for NumPy, but it is not part of the library itself.
  • TensorFlow integrates a lot more functionality that is not strictly array manipulation into the library itself, like image manipulation and common neural network utilities. NumPy tends to defer that kind of things to additional libraries like SciPy, making it more of an ecosystem and less monolithic. TensorFlow has some of that too, like TensorFlow Probability or TensorFlow Graphics, but it is not too developed yet.
  • TensorFlow offers a bunch of useful stuff if you are doing machine learning, like training checkpoints, distributed training, TensorBoard, TensorFlow Serving, etc. It also integrates better (or at all) with inference platforms and standards like TensorRT, Google Coral, ONNX and that kind of stuff.
  • NumPy generally integrates better with the "traditional" Python scientific stack, like Jupyter, Matplotlib, Pandas, dask, xarray, etc. There are pretty good libraries to do machine learning with NumPy too, like scikit-learn or Chainer, which are perfectly good if you only need to work in Python.
  • TensorFlow and NumPy also work reasonably well together, specially in eager mode, where any TensorFlow tensor can be directly converted to a NumPy array.

In general, if you are not going to work on machine learning, and specifically neural networks / deep learning, NumPy is probably the best choice, as it is easier to pick up, at least for general purposes, and has a larger community and corpus of documentation and resources. However, if you are going to be doing a significant amount work on that area, it may be worth to give TensorFlow a shot

2 of 2
2

Although the method names and parameters look identical, they are not the same thing. This becomes clear in the debugger. Just assign the results to variables and inspect them:

As you can see, Tensorflow gives you an EagerTensor and NumPy gives you an NDArray.

Tensorflow is a library for artificial intelligence, especially machine learning. Numpy is a library for doing numerical calculations.

They are often used in combination, because it's often required to pre-process data, which can be done with NumPy, and then do the machine learning on the processed data with Tensorflow.

🌐
TensorFlow
tensorflow.org › tensorflow core › load numpy data
Load NumPy data | TensorFlow Core
August 16, 2024 - TensorFlow Core · This tutorial provides an example of loading data from NumPy arrays into a tf.data.Dataset. This example loads the MNIST dataset from a .npz file. However, the source of the NumPy arrays is not important. import numpy as np import tensorflow as tf ·
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-numpy-array-to-tensor
How To Convert Numpy Array To Tensor - GeeksforGeeks
June 12, 2025 - name : by default None. If a new Tensor is produced, this is an optional name to use. This method directly converts a NumPy array into a TensorFlow tensor while inferring the data type.
🌐
EDUCBA
educba.com › home › data science › data science tutorials › tensorflow tutorial › tensorflow tensor to numpy
tensorflow tensor to numpy | How to use TensorFlow tensor to numpy?
March 15, 2023 - Then we can utilize the _tensor.numpy() function directly. ... Ex: We’ll start by creating a NumPy array using NumPy’s random.rand method to produce a four-by-three random matrix. ... We can see that it’s just a typical NumPy array if we run the code. The command line might look like this: python numpy-arrays-to-tensorflow-tensors-and-back.py
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
AskPython
askpython.com › home › convert a tensor to numpy array in tensorflow-implementation
Convert A Tensor To Numpy Array In Tensorflow-Implementation - AskPython
March 26, 2023 - Tensors are supported by accelerated GPU which is not supported by NumPy. Tensorflow is an open source API that can perform gradient computations unlike numpy arrays.
🌐
ProjectPro
projectpro.io › recipes › convert-numpy-array-tensor
Convert numpy array to tensor - ProjectPro
January 19, 2023 - This recipe helps you convert a ... ... To achieve this we have a function in tensorflow called "convert_to_tensor", this will convert the given value into a tensor....
🌐
Medium
zorahirbodvash.medium.com › numpy-vs-tensorflow-adf7ba0634a6
Numpy Vs. Tensorflow. Here I am writing about the most… | by Zora Hirbodvash | Medium
February 11, 2021 - We can obtain the NumPy version from TensorFlow objects. Also, the TensorFlow tensor can be directly converted to a NumPy array in eager mode. TensorFlow reimplements a large portion of NumPy API.
🌐
Real Python
realpython.com › numpy-tensorflow-performance
Pure Python vs NumPy vs TensorFlow Performance Comparison – Real Python
September 7, 2023 - A performance comparison between pure Python, NumPy, and TensorFlow using a simple linear regression algorithm.