Convert list to tensor TensorFlow

In this Python tutorial, we will learn how to convert the list to the tensor in Python TensorFlow. Additionally, we will cover the following topics.

  • Convert list to tensor TensorFlow
  • Convert list of tensor to numpy array TensorFlow
  • Convert list of strings to tensor TensorFlow
  • Convert list of lists to tensorflow tensor
  • Convert array to tensor Pytorch

Convert list to tensor TensorFlow

  • In this section, we will discuss how to convert a list to a tensor by using TensorFlow Python.
  • To convert a Python list to a tensor, we are going to use the tf.convert_to_tensor() function and this function will help the user to convert the given object into a tensor.
  • In this example, the object can be a Python list and by using the function will return a tensor.

Syntax:

Let’s have a look at the Syntax and understand the working of the tf.convert_to_tensor() function in Python TensorFlow.

tf.convert_to_tensor
                   (
                    value,
                    dtype=None,
                    dtype_hint=None,
                    name=None
                   )
  • It consists of a few parameters
    • value: This parameter indicates the object to which we want to convert the tensor.
    • dtype: It is an optional parameter and it specifies the type of the value.
    • name: This parameter indicates the name of the operation and it is also an optional parameter.

Example:

Let’s take an example and check how to convert a list to a tensor in Python TensorFlow.

Source Code:

import tensorflow as tf

new_list=[145,56,89,56]
print(type(new_list))
con_lis = tf.convert_to_tensor(new_list)
print("Convert list to tensor:",con_lis)

In the following code first, we have imported the TensorFlow library and then declare a variable ‘new_list’ and assigned the values in the list object. To check whether it is a list or NumPy, you can use the syntax type(obj_name).

After that, we have used the tf.convert_to_tensor() function and within this function, we have assigned the list object ‘new_list’. Once you will execute this code the output displays the tensor object.

Here is the Screenshot of the following given code.

Python Convert list to tensor TensorFlow
Python Convert list to tensor TensorFlow

Read: TensorFlow Tensor to numpy

Convert list of tensor to numpy array TensorFlow

  • Here we are going to discuss how to convert a list of tensors to a numpy array in Python TensorFlow.
  • To perform this task we are going to use the tf.stack() function for creating a list of tensors. In Python, this function is used to combine a list of tensors with a new tensor. In this example first, we will create three tensors by using the tf.constant() function, and then to combine these tensors of the list we are going to use the tf.stack() function.
  • Next, we will use the tensor.numpy() function and this function will help the user to convert a tensor into a numpy array and it is an inbuilt function in TensorFlow.

Syntax:

Here is the Syntax of tf.stack() function in Python TensorFlow.

tf.stack
        (
         values,
         axis=0,
         name='stack'
        )
  • It consists of a few parameters
    • values: This parameter indicates a list of tensor objects with same size and shape.
    • axis: By default it takes 0 value and it defines the first dimension.
    • name: This parameter indicates the name of the operation and by default it takes ‘Stack’ value.

Example:

Let’s take an example and check how to convert a list of tensors to a numpy array in a Python TensorFlow.

Source Code:

import tensorflow as tf

tens1 = tf.constant([67, 23,56])
tens2 = tf.constant([94, 13,28])
tens3 = tf.constant([116, 178,345])
result=tf.stack([tens1, tens2, tens3])
print("Cretaed a list of tensors:",result)
con_arr = result.numpy()
print("Converting into numpy array:",con_arr)
print(type(con_arr))

In the above code, we have imported the Tensorflow library and then created a tensor by using the tf.constant() function. After creating tensors, we have combined the list of tensors by using the tf.stack() function, and then we used the tensor.numpy() function to convert the tensor into a numpy array.

Here is the execution of the following given code.

Python Convert list of tensor to numpy array TensorFlow
Python Convert list of tensor to numpy array TensorFlow

Read: TensorFlow get shape

Convert list of strings to tensor TensorFlow

  • In this section, we will discuss how to convert a list of strings to tensor in Python TensorFlow.
  • To perform this particular task, we are going to use the tf.convert_to_tensor() function and this function is used to convert the given object into a tensor. In this example, the input object will be a list of strings.
  • Next, we will use the tf.convert_to_tensor() function and pass the list of strings as an argument.

Syntax:

Here is the Syntax of tf.convert_to_tensor() function in Python TensorFlow.

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

Example:

Let’s take an example and check how to convert a list of strings to tensor in Python TensorFlow.

Source Code:

import tensorflow as tf

lis_of_strings = ["USA", "France", "Australia","Germany","China"]
print("Creation of list of strings:",lis_of_strings)
print(type(lis_of_strings))
result=tf.convert_to_tensor(lis_of_strings)
print("Convert list of strings into tensor:",result)
print(type(result))

In the following given code first, we have created a list of strings and checked the datatype by using type(object_name).

In this example the object name is lis_of_strings and then we have used the tf.convert_to_tensor() function and assigned the lis_of_strings as an argument. Once you will execute this code the output displays the tensor.

Here is the implementation of the following given code.

Python Convert list of strings to tensor TensorFlow
Python Convert list of strings to tensor TensorFlow

Read: Python TensorFlow reduce_sum

Convert list of lists to tensorflow tensor

  • In this section, we will discuss how to convert a list of lists to tensor in Python TensorFlow.
  • By using the tf.convert_to_tensor() function, we can easily convert the list of lists into a tensor. First, we will create a nested list which means a list of lists, and then we are going to assign the integer values.
  • Next, we will use the tf.convert_to_tensor() function for converting the list of lists into a tensor.

Syntax:

Here is the Syntax of tf.convert_to_tensor() function in Python.

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

Example:

Let’s take an example and check how to convert a list of lists to tensor in Python TensorFlow.

Source Code:

import tensorflow as tf

lis_of_lists = [[15, 89, 467], [122, 897, 345], [167, 945, 289]]
print("Creation of list of lists:",lis_of_lists)
print(type(lis_of_lists))
result=tf.convert_to_tensor(lis_of_lists)
print("Convert list of lists into tensor:",result)
print(type(result))

Here is the Screenshot of the following given code.

Python Convert list of lists to tensorflow tensor
Python Convert list of lists to TensorFlow tensor

Read: Python TensorFlow reduce_mean

Convert array to tensor Pytorch

  • Here we are going to discuss how to convert a numpy array to Pytorch tensor in Python.
  • To do this task we are going to use the torch.fromnumpy() function and this function is used to convert the given numpy array into pytorch tensor.
  • In Python torch.tensor is the same as numpy array that contains elements of a single data type.

Syntax:

Let’s have a look at the Syntax and understand the working of the torch.from numpy() function.

torch.fromnumpy(ndarray)

Example:

Let’s take an example and check how to convert a numpy array to a Pytorch tensor.

Source Code:

import torch
import numpy as np

new_arr=np.array([[25,78,97],
                  [12,45,89],
                  [67,89,55]])
print("Creation of numpy array:",new_arr)
result = torch.from_numpy(new_arr)
print("Converting array into Pytorch:",result)
print(type(result))

In the following given code we have imported the numpy library and then created a numpy array by using the np.array() function. After creating an array we have used the torch.from_numpy() function and within this function, we have assigned the array as an argument.

Here is the Output of the following given code

Python Convert array to tensor Pytorch
Python Convert array to tensor Pytorch

As you can see in the Screenshot the output displays the torch tensor.

Also, take a look at some more tutorials in TensorFlow.

In this Python tutorial, we have learned how to convert list to tensor TensorFlow in Python. Additionally, we have covered the following topics.

  • Convert list to tensor TensorFlow
  • Convert list of tensor to numpy array TensorFlow
  • Convert list of strings to tensor TensorFlow
  • Convert list of lists to tensorflow tensor
  • Convert array to tensor Pytorch