Skip to main content

How to Convert a TensorFlow Tensor to a NumPy Array with numpy()

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Plotting a result, saving it with another library or just printing the numbers all start the same way: get the values out of the tensor. .numpy() does that, and returns a NumPy array. This guide converts two tensors in TensorFlow 2.22.0, and shows where .numpy() doesn't work.

Syntax​

Syntax
<tensor>.numpy()
PartWhat it means
<tensor>A tensor that already has its values, as every tensor does in ordinary Python code
.numpy()Returns the values as a NumPy array, or as a NumPy number for a tensor with one value

The tensor guide: "You can convert a tensor to a NumPy array either using np.array or the tensor.numpy method". The tf.Tensor API page explains where the method comes from: "during eager execution, you may discover your Tensors are actually of type EagerTensor. This is an internal detail, but it does give you access to a useful function, numpy". Eager execution is TensorFlow's normal mode, where every line runs straight away.

The Example​

Three prices in a tensor, converted to a NumPy array, and a tensor holding the single number 7, converted to a number.

Prerequisites​

  • A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10. TensorFlow installs NumPy with it.

Step 1: Write the Script​

to_numpy.py
import tensorflow as tf

prices = tf.constant([2.5, 4.0, 1.25])
array = prices.numpy()

print(type(prices))
print(type(array))
print(array)
print(array.dtype)
print(tf.constant(7).numpy())

The to_numpy.py script in a macOS-style code window: a prices tensor of 2.5, 4.0 and 1.25 converted with prices.numpy(), then prints of both types, the array, its dtype, and tf.constant(7).numpy().

prices.numpy() copies the three prices into a NumPy array. The prints compare the two objects, then show the array and its type of number.

Step 2: Run It​

Terminal
uv run python to_numpy.py
Output
<class 'tensorflow.python.framework.ops.EagerTensor'>
<class 'numpy.ndarray'>
[2.5 4. 1.25]
float32
7

The terminal after uv run python to_numpy.py: the tensor's class EagerTensor, the array's class numpy.ndarray, the array 2.5 4. 1.25, its dtype float32, and 7.

The tensor is an EagerTensor, and .numpy() gave a numpy.ndarray with the same three prices. The array kept the tensor's type, float32. For a tensor with one value, .numpy() gave just the number, 7. The command works unchanged in PowerShell.

The first run in a new environment is slow, while TensorFlow loads for the first time.

Common Mistakes​

MistakeWhat happensDo this instead
Calling .numpy() inside a function marked @tf.functionAttributeError: 'SymbolicTensor' object has no attribute 'numpy'. tf.function turns the function into a TensorFlow graph, built before any values existUse TensorFlow operations inside it: tf.reduce_sum(prices) returned tf.Tensor(7.75, shape=(), dtype=float32). Call .numpy() on the result, outside the function

The tf.function API page says what the decorator does: it "Compiles a function into a callable TensorFlow graph." While the graph is being built, prices is a placeholder, a SymbolicTensor, so there are no values to copy.

FAQ​

Does changing the array change the tensor?​

No. .numpy() returns a copy, and its own documentation in TensorFlow's source says why: "Unlike NumPy arrays, Tensors are immutable, so this method has to copy the contents to ensure safety." The array is yours to change; the tensor stays as it was.

Can I turn the array back into a tensor?​

Yes, pass it to tf.constant, the same way you'd pass a list: tf.constant(array) gave back tf.Tensor([2.5 4. 1.25], shape=(3,), dtype=float32).

Conclusion​

.numpy() turned a tensor of three prices into a NumPy array of type float32, and a tensor holding one value into a plain number. It works wherever tensors hold their values; inside a @tf.function, use TensorFlow operations and convert the result afterwards.

Additional Resources​