How To Convert Numpy Array To Tensor
Last Updated :
12 Jun, 2025
We are given a NumPy array, and our task is to convert it into a TensorFlow tensor. This is useful when integrating NumPy-based data with TensorFlow pipelines, which support acceleration using GPU and TPU. For example, converting [[1, 2], [3, 4]] results in a TensorFlow object that looks like:
Python
<tf.Tensor: shape=(2, 2), dtype=int64, numpy=
array([[1, 2],
[3, 4]])>
TensorFlow provides multiple ways to convert a NumPy array to a tensor, each with its own flexibility and use cases.
Syntax of tf.convert_to_tensor()
tf.convert_to_tensor( value, dtype=None, dtype_hint=None, name=None)
Parameters:
- value : The type of an object with a registered Tensor conversion function.
- dtype: by default it is None. The returned tensor's element type is optional. If the type isn't specified, the type is inferred from the value type.
- dtype_hint: by default None. When dtype is None, this is an optional component type for the returned tensor. When converting to a tensor, a caller may not have a datatype in mind, hence dtype hint can be used as a preference. This parameter has no effect if the conversion to dtype hint is not possible.
- name : by default None. If a new Tensor is produced, this is an optional name to use.
Using tf.convert_to_tensor()
This method directly converts a NumPy array into a TensorFlow tensor while inferring the data type.
Python
import tensorflow as tf
import numpy as np
#create numpy_array
arr = np.array([[1, 2], [3, 4]])
# convert it to tensorflow
t1 = tf.convert_to_tensor(arr)
print(t1)
Output:
tf.Tensor(
[[1 2]
[3 4]], shape=(2, 2), dtype=int64)
Using tf.convert_to_tensor() with dtype and name
You can manually specify the data type and assign a name to the tensor for easier debugging.
Python
import tensorflow as tf
import numpy as np
#create numpy_array
arr = np.array([[1, 2], [3, 4]])
# convert it to tensorflow
t2 = tf.convert_to_tensor(arr, dtype=tf.float32, name='tensor1')
print(t2)
Output:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1., 2.],
[3., 4.]], dtype=float32)>
Using tf.Variable()
This method wraps the NumPy array as a Variable, which is mutable and trainable—useful during model training.
Python
import tensorflow as tf
import numpy as np
# create numpy_array
arr = np.array([[1, 2], [3, 4]])
# convert it to tensorflow
t3 = tf.Variable(arr, dtype=tf.float32, name='tensor1')
print(t3)
Output:
<tf.Variable 'tensor1:0' shape=(2, 2) dtype=float32, numpy=
array([[1., 2.],
[3., 4.]], dtype=float32)>
Using tf.constant()
This is a convenient method to create immutable tensors from NumPy arrays.
Python
import tensorflow as tf
import numpy as np
# create numpy_array
arr = np.array([[10, 20], [30, 40]])
# convert it to tensorflow
t4 = tf.constant(arr, dtype=tf.int32)
print(t4)
Output:
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[10, 20],
[30, 40]])>
Similar Reads
How To Convert Numpy Array To Tensor? We are given a NumPy array, and our task is to convert it into a TensorFlow tensor. This is useful when integrating NumPy-based data with TensorFlow pipelines, which support acceleration using GPU and TPU. For example, converting [[1, 2], [3, 4]] results in a TensorFlow object that looks like: Pytho
2 min read
How to Convert NumPy Matrix to Array In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array. Convert Python NumPy Matrix to an ArrayBelow are the ways by which we can
3 min read
How to convert NumPy array to dictionary in Python? The following article explains how to convert numpy array to dictionary in Python. Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array. A tuple of integers givi
3 min read
Convert Python List to numpy Arrays NumPy arrays are more efficient than Python lists, especially for numerical operations on large datasets. NumPy provides two methods for converting a list into an array using numpy.array() and numpy.asarray(). In this article, we'll explore these two methods with examples for converting a list into
4 min read
Convert Numpy Array To Xarray Xarray is a powerful Python library for working with labeled multi-dimensional arrays. In Python, NumPy provides basic data structures and APIs for working with raw ND arrays, but, in the real world, the data is more complex, in some cases, which are encoded. The data array maps to positions in spac
3 min read
How to convert 1D array of tuples to 2D Numpy array? In this article, we will discuss how to convert a 1D array of tuples into a numpy array. Example: Input: [(1,2,3),('Hi','Hello','Hey')] Output: [['1' '2' '3'] ['Hi' 'Hello' 'Hey']] #NDArray Method 1: Using Map The map is a function used to execute a function for each item in an Iterable i.e array.
2 min read