9951 explained code solutions for 126 technologies

python-tensorflowHow can I convert a Tensor object to a list in Python using TensorFlow?

To convert a Tensor object to a list in Python using TensorFlow, you can use the tf.compat.v1.Session() and eval() functions. The example code below shows how to do this:

import tensorflow as tf

# Create a Tensor object
tensor = tf.constant([[1,2,3], [4,5,6], [7,8,9]])

# Create a session
sess = tf.compat.v1.Session()

# Convert Tensor to a list
list_tensor = sess.run(tensor).tolist()

# Print the list

Output example

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

The code above consists of the following parts:

  1. import tensorflow as tf: This imports the TensorFlow library into the current Python session.
  2. tensor = tf.constant([[1,2,3], [4,5,6], [7,8,9]]): This creates a Tensor object.
  3. sess = tf.compat.v1.Session(): This creates a session in which the Tensor object can be evaluated.
  4. list_tensor = sess.run(tensor).tolist(): This evaluates the Tensor object and converts it to a list.
  5. print(list_tensor): This prints the list.

For more information about converting Tensor objects to lists in TensorFlow, please see the TensorFlow documentation.

Edit this code on GitHub