Master TensorFlow: Converting Lists to Tensors Easily

In this article, you will learn how to convert list to tensor tensorflow.

In my ML project, I loaded the data in a Python list from the CSV file and needed to feed that dataset to a machine learning model for training or prediction in Tensorflow.

So here, machine learning models were built using TensorFlow, which required data in tensor format. so to convert that Python list into a tensor, I used tf.convert_to_tensor() function.

I successfully converted the data into a tensor and fed it to the model, so in this tutorial, I will show you how I have used tf.convert_to_tensor() with a basic example.

Convert List to Tensor TensorFlow

First, what is a tensor? A tensor represents an N-dimensional array of data. To learn more about Tensors, follow this tutorial on Tensor in TensorFlow.

If you are familiar with tensors, let’s see how to convert a list to a tensor. TensorFlow has a function called tf.convert_to_tensor() that allows you to convert Python lists into tensors. Not only Python lists but also it can convert other objects or numpy arrays into tensors.

The syntax for using tf.convert_to_tensor() function is given below.

tf.convert_to_tensor(
    value, dtype=None, dtype_hint=None, name=None
)

The parameters that it accepts are:

  • value: This parameter indicates the object (lists, strings, or numpy arrays) you want to convert into tensors.
  • dtype: It is an optional parameter representing the data type of the returned tensor; if not specified, TensorFlow automatically infers the type from the value.
  • name: This parameter indicates the name of the tensor and is optional, too.

Now, consider a simple example in which you create a list and then convert that list into a tensor.

First, import the TensorFlow library using the command below.

import tensorflow as tf

Create a Python list [ 4, 7, 8, 9] as shown in the below code.

list_data = [ 4, 7, 8, 9]

Pass the list_data to tf.convert_to_tensor as a value to convert this value into a tensor, as shown in the code below.

tensor_data = tf.convert_to_tensor(list_data)

Prints the tensor_data.

print(tensor_data)

Convert List to Tensor TensorFlow

It outputs the tensor, which is tf.Tensor([4 7 8 9], shape=(4,), dtype=int32). The output shows that it converted the given list_data into a tensor. Also, converting list_data to a tensor automatically infers the type of values in a list, which is int32.

Converting List of Strings to Tensor

Next, convert the list of strings into tensors, so first create str_list as shown in the below code.

str_list = ['Chicago', 'Houston', 'Dallas']

Pass that list of strings ‘str_list’ to tf.convert_to_tensor as shown below.

tensor_str = tf.convert_to_tensor(str_list)
print(tensor_str)

Convert List of Strings to Tensor TensorFlow

Look in the output; it converted the given list of strings into a tensor, which is tf.Tensor([b’Chicago’ b’Houston’ b’Dallas’], shape=(3,), dtype=string).

Similarly, you can convert anything in the list to a tensor using the tf.convert_to_list() function.

This is how you can use tf.convert_to_tensor() to convert the given list into tensors.

Next, l want to show you how to convert a list of lists into tensors.

Convert List of List to Tensor TensorFlow

In the preceding section, you converted the list of values into a tensor. Here, you will understand how to convert a nested list into a tensor.

First, creating a nested list means a list of lists, as shown below.

nested_list = [[1, 4, 6], [9, 3, 6]]

tensor_values = tf.convert_to_tensor(nested_list)
print(tensor_values)

After that, pass the nested_list to tf.convert_to_tensor() as shown in the below code.

tensor_values = tf.convert_to_tensor(nested_list)
print(tensor_values)

Convert List of List to Tensor TensorFlow

Look again in the output; it shows the tensor, which is tf.Tensor( [[1 4 6] [9 3 6]], shape=(2, 3), dtype=int32).

This is how you can convert the nested list into tensors using the tf.convert_to_tensor.

But it is not limited to converting a list of values; you can also convert a single scalar value; for example, suppose you have the single string ‘United States’.

Pass that string to tf.convert_to_tensor as shown in the code below to convert the string to tensor.

str_tensor = tf.convert_to_tensor('United States')
print(str_tensor)

Convert String to Tensor TensorFlow

Look at the output. It also converts the given string ‘United States’ into a tensor, which is tf.Tensor(b’United States’, shape=(), dtype=string).

This is how you can use the tf.convert_to_tensor() function to convert any given object into a list of strings, integers, list of lists, etc, into tensors.

Conclusion

This TensorFlow tutorial taught you how to convert list to tensor tensorflow using the tf.convert_to_tensor() function.

You converted a list of integer and string values into a tensor using the tf.convert_to_tensor. Additionally, you learned how to convert a list of lists into a tensor.

#tensorflow #convert 

Master TensorFlow: Converting Lists to Tensors Easily
1.05 GEEK