. Advertisement .
..3..
. Advertisement .
..4..
There is more than one way to convert Tensor to NumPy array in Python. You can either use NumPy or TensorFlow to carry out this conversion. Check out all the possible solutions below.
Convert Tensor To NumPy Array In Python
With numpy.array()
In TensorFlow, Tensors are similar to arrays in NumPy (they all have a shape and a data type). Conversions between them are easy to carry out and well supported in Python. In fact, they happen without your intervention in many situations.
For instance, operations in TensorFlow that involve NumPy arrays will automatically convert them to Tensors. Likewise, NumPy operations will also do the same and convert Tensors to arrays.
If you don’t want to rely on those automatic conversions, you can manually invoke the array() function in NumPy. This is one of the main ways to create arrays in this library.
Syntax:
numpy.array(object, dtype, copy, order, subok, ndmin, like)
Parameters:
- object: this parameter should be an array-like object that exposes the array interface, meaning its __array__ method should return a nested sequence like an array. If you provide a scalar object, NumPy will create a zero-dimensional array that contains that value.
- dtype: the data type you want the array to have. When this optional parameter is omitted, NumPy will determine it by using the minimum data type needed to store the values in sequence.
- copy: when this optional parameter is set True, NumPy copies the object. If False, it only makes a copy when the __array__ method returns a copy, when a copy is required to meet other requirements, or when the object is a nested sequence.
- order: this parameter is optional. It specifies the array’s memory layout. When the object isn’t an array, NumPy will create a new array in C order (which is row major), or in Fortran order (column major) when F is specified.
- subok: if this boolean parameter is set to True, NumPy will pass its subclasses through. Otherwise (which is also the default case), NumPy will make the returned array a base-class array.
- ndmin: you can use this integer parameter to set the minimum number of dimensions of the resulting array.
Example:
>>> import tensorflow as tf
>>> import numpy as np
>>> t = tf.constant([[1, 2], [4, 8]])
>>> a = np.array(t)
>>> a
array([[1, 2],
[4, 8]], dtype=int32)
>>> type(a)
<class 'numpy.ndarray'>
In the example above, we have created a (2, 2) Tensor before converting it to a 2×2 NumPy array. You can verify the type and content of this array with the function type().
Make sure you use the correct syntax or errors like “’numpy.ndarray’ object is not callable” may occur. If you run into this error, consult this guide for solutions.
With tf.numpy()
As the numpy module offers us the array()
function to create an array from an array-like object, you can do this from a TensorFlow method numpy()
.
This is an explicit way to do this conversion, which typically isn’t resource-consuming since Tensors and Numpy arrays share memory representation under the hood when possible. However, this isn’t always the case. NumPy always stores its arrays in host memory, while sometimes Tensors are hosted in GPU memory. A conversion from a Tensor to a NumPy array, therefore, may involve a memory copy from GPU to host.
This is how you can use the numpy()
method of a Tensor object to convert it to a NumPy array:
>>> import tensorflow as tf
>>> import numpy as np
>>> t = tf.constant([[1, 2], [4, 8]])
>>> a = t.numpy()
>>> a
array([[1, 2],
[4, 8]], dtype=int32)
>>> type(a)
<class 'numpy.ndarray'>
With tf.make_ndarray()
The make_ndarray()
converts a TensorProto to a NumPy array with the same data and shape as the Tensor. This function is also easy to use and can replace the numpy()
method. However, you will need to convert the Tensor to a TensorProto first:
>>> import tensorflow as tf
>>> import numpy as np
>>> t = tf.constant([[1, 2], [4, 8]])
>>> proto_tensor = tf.make_tensor_proto(t)
>>> tf.make_ndarray(proto_tensor)
array([[1, 2],
[4, 8]], dtype=int32)
Summary
You can use the array()
function (of the numpy module) or the numpy()
method or make_ndarray()
function (of the tensorflow module) to convert Tensor to NumPy array in Python. This conversion is fairly straightforward since these objects share many similarities.
Leave a comment