Convert Tensor to NumPy array in Python
This tutorial will show you how to convert a Tensor to a NumPy array in Python.
Use the function in Python Tensor.numpy()
to convert a tensor to a NumPy array
Eager Execution of TensorFlow library can be used to convert tensor to NumPy array in Python. With eager execution Eager Execution
, the behavior of TensorFlow library operations changes and these operations are executed immediately. We can also use to Eager Execution
perform NumPy operations on Tensor objects. Tensor.numpy()
The function converts a Tensor to a NumPy array in Python. In TensorFlow 2.0, eager execution is enabled by default Eager Execution
. Therefore, this method works best with TensorFlow version 2.0. See the following code example.
import tensorflow as tf
tensor = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("Tensor = ", tensor)
array = tensor.numpy()
print("Array = ", array)
Output:
Tensor = tf.Tensor(
[[1 2 3]
[4 5 6]
[7 8 9]], shape=(3, 3), dtype=int32)
Array = [[1 2 3]
[4 5 6]
[7 8 9]]
tf.constant()
In the code above, we first created and initialized a Tensor object in Python using the tensor.numpy() function tensor
. We printed it in Python using the tensor.numpy() function tensor
and converted it to a NumPy array array
. Finally, we printed it 数组
.
Use the function in Python Tensor.eval()
to convert a tensor to a NumPy array
We can also Tensor.eval()
convert a Tensor to a NumPy array in Python using the function. This method is not supported in TensorFlow version 2.0. Therefore, we have to keep the previous version 1.0 of TensorFlow or disable all the behaviors of version 2.0 of the TensorFlow library. See the following code example.
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
tensor = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("Tensor = ", tensor)
array = tensor.eval(session=tf.Session())
print("Array = ", array)
Output:
Tensor = Tensor("Const_1:0", shape=(3, 3), dtype=int32)
Array = [[1 2 3]
[4 5 6]
[7 8 9]]
In the code above, we use tensor.eval()
the function in Python to convert the Tensor object tensor
to a NumPy array array
. We first imported version 1.0 of the TensorFlow library and disabled all behaviors of version 2.0. Then, we tf.constant()
created and initialized using the function tensor
and tensor
printed the value in . We then executed tensor.eval()
the function and saved the returned value array
in and printed the value in array
.
Use the function in Python TensorFlow.Session()
to convert a tensor to a NumPy array
TensorFlow.Session()
is another method that can be used to convert a Tensor to a NumPy array in Python. This method Tensor.eval()
is very similar to the previous method with the function. Version 2.0 of the TensorFlow library also does not support this method. We either have to install version 1.0 of the TensorFlow library or disable all the behaviors of version 2.0 of the TensorFlow library. We can pass a Tensor object to TensorFlow.Session().run()
the function to convert that Tensor object to a NumPy array in Python. See the following code example.
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
tensor = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("Tensor = ", tensor)
array = tf.Session().run(tensor)
print("Array = ", array)
Output:
Tensor = Tensor("Const_6:0", shape=(3, 3), dtype=int32)
Array = [[1 2 3]
[4 5 6]
[7 8 9]]
In the code above, we use tf.Session.run(tensor)
the function in Python to convert the Tensor object tensor
to a NumPy array array
. We first imported the 1.0 compatible TensorFlow library and disabled all the behaviors of version 2.0. Then, we created the tensor object tensor
and printed tensor
the value of . Then, we used tf.Session.run(tensor)
the function to tensor
convert the tensor to array
a NumPy array and printed the value in array
.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Converting a PIL image to a NumPy array
Publish Date:2025/05/03 Views:148 Category:Python
-
This tutorial will discuss methods to convert a PIL image to a 3-dimensional NumPy array in Python. numpy.array() Convert a PIL image to a NumPy array in Python using the PIL is used to perform various operations on images in Python. The Pi
How to Convert DataFrame Column to String in Pandas
Publish Date:2025/05/02 Views:162 Category:Python
-
We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a
How to count the frequency of values in a Pandas DataFrame
Publish Date:2025/05/02 Views:84 Category:Python
-
Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().
How to get value from Pandas DataFrame cell
Publish Date:2025/05/02 Views:148 Category:Python
-
We'll look at using to get values from cells in iloc Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .
How to Add a Row to a Pandas DataFrame
Publish Date:2025/05/02 Views:127 Category:Python
-
Pandas is designed to load a fully populated DataFrame . We can pandas.DataFrame add them one by one in . This can be done by using various methods, such as .loc , dictionary, pandas.concat() or DataFrame.append() . .loc [index] Add rows to
How to change the order of Panas DataFrame columns
Publish Date:2025/05/02 Views:185 Category:Python
-
We will show how to use insert and reindex to change the order of columns in different ways pandas.DataFrame , such as assigning column names in a desired order. pandas.DataFrame Sort the columns in the new order The easiest way is columns
How to pretty print an entire Pandas Series/DataFrame
Publish Date:2025/05/02 Views:168 Category:Python
-
We will introduce various methods to pretty print the entire Pandas Series/DataFrame, such as option_context, set_option, and options.display. option_context Pretty Printing Pandas DataFrame We can option_context use with one or more option
How to count the number of NaN occurrences in a Pandas Dataframe column
Publish Date:2025/05/02 Views:144 Category:Python
-
We will look at methods for counting the number of NaN occurrences in a column of a Pandas DataFrame. We have a number of options, including isna() the method for one or more columns, by NaN subtracting the total length from the number of o
How to Convert a Pandas Dataframe to a NumPy Array
Publish Date:2025/05/02 Views:153 Category:Python
-
We will introduce to_numpy() the method to pandas.Dataframe convert a to NumPy an array, which is introduced in pandas v0.24.0, replacing the old .values method. We can define it on Index , Series , and DataFrame objects to_numpy . The old