Convert List To TensorFlow Tensor

Convert a python list into a TensorFlow Tensor using the TensorFlow convert_to_tensor functionality

Type: FREE   By: Sebastian Gutierrez, AIWorkbox.com Instructor Sebastian Gutierrez   Duration: 2:21   Technologies: TensorFlow, Python

Page Sections: Video  |  Code  |  Transcript


< > Code:

You must be a Member to view code

Access all courses and lessons, gain confidence and expertise, and learn how things work and how to use them.

    or   Log In


Transcript:

This video will show you how to convert a Python list into a TensorFlow tensor using the tf.convert_to_tensor functionality.


First, we import TensorFlow as tf.

import tensorflow as tf


Then we print out the version of TensorFlow we are using.

print(tf.__version__)

We are using TensorFlow 1.8.0.


Next, let’s start out by defining a Python list that’s composed of interior lists and we assign this list to the Python variable initial_python_list.

initial_python_list = [
    [
        [1,2,3,4],
        [5,6,7,8]
    ],
    [
        [9,10,11,12],
        [13,14,15,16]
    ]
]


Let’s print this initial_python_list variable to see what it looks like.

print(initial_python_list)

We can see that it is one long set of nested lists.


Let’s also do a type check of the variable to make sure it is a Python list.

type(initial_python_list)

We can see that the class is list, so bingo, it worked!


What we want to do now is to convert this Python list to a TensorFlow tensor.


To do this, we’ll use the tf.convert_to_tensor operation.

tensor_from_list = tf.convert_to_tensor(initial_python_list)

So tf.convert_to_tensor, and we pass in our Python list, and the result of this operation will be assigned to the Python variable tensor_from_list.


So we didn’t get an error, so let’s see what happens when we print the tensor from the Python list variable.

print(tensor_from_list)

We see that it’s a tensor.

We see that it’s a constant.

We see that the shape is 2x2x4.


So let’s check it out.

So one, two, so that’s the first dimension.

Then two rows.

That’s the second dimension.

And one, two, three, four columns.

That’s the third dimension.

And the data type is int32.

Here, I used all integers, I didn’t use any floating point numbers, so that makes sense.


One interesting thing about this conversion is that when we print it like we just did, we can’t actually see the data.

So even though our initial Python list has been converted into a tensor, we can’t actually see the data.

This is because we’re still in the building the TensorFlow graph phase rather than the actual evaluating the graph.


So now that we’ve created our TensorFlow tensors, it’s time to run the computational graph.

sess = tf.Session()

We launch the graph in a session.


And then we initialize all the global variables in the graph.

sess.run(tf.global_variables_initializer())


So now that our variable has been initialized, let’s print our tensor_from_list Python variable again, only this time in a TensorFlow session.

print(sess.run(tensor_from_list))

So we do print, and then sess.run, and pass in our tensor_from_list.

We can now see all the data that was in the original Python list.


Perfect! We were able to convert a Python list into a TensorFlow tensor using the tf.convert_to_tensor functionality.



Back to deep learning tutorial lesson list