PyTorch is the framework with the application for building and training Machine Learning (ML) algorithms using Python programming language. ML algorithms need data for their training and data should be properly structured to train the model efficiently. Tensors are the structures to store the data and they are saved in the arrays in the numpy without the knowledge about the working of the ML models.

**Table of Content**

This guide explains the following sections:

- How to Convert List to Tensor in PyTorch
- Prerequisites
- Method 1: Using tensor() Function
- Method 2: Using FloatTensor() Function
- Method 3: Using as_tensor() Function
- Method 4: Using from_numpy() Function
- Bonus Tip: Creating Tensor in PyTorch
- Conclusion

**How to Convert List to Tensor in PyTorch**

PyTorch allows the user to create the tensors to store data efficiently so it can be extracted whenever required to perform some analysis. Sometimes, the user needs to use the lists as the tensors as the built-in tensors don’t allow some values to be generated using them. To avoid these complications, the user can create values in the list and then easily convert them to tensors and train the machine accordingly.

To learn the process of converting the lists to the tensors using the PyTorch framework, simply follow this guide, and the Notebook with the code is attached here:

**Prerequisites**

Before getting into the main process of converting the list to a tensor, it is required to perform the following prerequisites and if any of the following steps are done, simply skip it:

- Get Environment for Python
- Install Modules
- Import Libraries

**Get Environment for Python**

To run the code for converting the list to tensors, visit the official website of the Google Colab and click on the “**New Notebook**” button to open an environment for running the Python script:

**Install Modules**

On the Python Notebook, install the **numpy** to build the multi-dimensional arrays and **torch** for getting the tensors, if they are yet to be installed:

```
pip install numpy
pip install torch
```

Install both frameworks separately as displayed in the following screenshot:

**Import Libraries**

Once the torch is installed successfully, simply import its library using the following code:

`import torch`

After importing the torch library, use the following code to get its version:

`print(torch.__version__)`

The following screenshot displays the installed version of the torch which is **2.1.0+cu118**:

**Method 1: Using tensor() Function**

After completing all the prerequisites, simply head towards the process of converting the list to the tensor using the tensor() method and following the steps below:

- Step 1: Creating a List
- Step 2: List to Tensor
- Step 3: Printing the Tensor

**Step 1: Creating a List**

Create a variable to initialize it with a list which is a multi-dimensional list:

`list_pytorch = [[1,7,3,2],[5,6,0,8],[5,10,11,4]]`

To confirm that the variable contains the list, simply execute the following code and the output will display its type:

`type(list_pytorch)`

**Step 2: List to Tensor**

Now, convert the list to the tensor using the tensor() method with the variable name as its argument and initialize another variable with the tensor:

`pytorch_tensor = torch.tensor(list_pytorch)`

Confirm that the variable contains the tensor which is converted from a list by using the following code:

`type(pytorch_tensor)`

The following screenshot confirms that the tensor is created from the list:

**Step 3: Printing the Tensor**

Get the data type of the tensor variable by calling the “**dtype**” with the name of the variable:

`pytorch_tensor.dtype`

Print the tensor by calling the print() method with the name of the variable in its argument:

`print(pytorch_tensor)`

The list has been converted to the tensor successfully as the following snippet displays the contents:

**Method 2: Using FloatTensor() Function **

The second approach to converting the list in Python to the tensor is using the FloatTensor() method and the following sections explain it:

- Step 1: Creating a Floating List
- Step 2: List to Tensor
- Step 3: Printing the Tensor

**Step 1: Creating a Floating List**

Create a list with the floating points and assign it to the variable called floatinglist to make the understandability better:

`floatinglist = [[1.5,2.7,3.1,4.2],[5.3,6.2,7.0,8.1],[9.5,10.3,11.7,12.4]]`

Get the type of the list using the name of the variable in the argument of the type() method:

`type(floatinglist)`

**Step 2: List to Tensor**

Convert the list to the tensor using the FloatTensor() method and the name of the variable as its argument:

`Ftensor = torch.FloatTensor(floatinglist)`

Confirm that the list is converted to the tensor by calling the type() method:

`type(Ftensor)`

The list has been successfully converted to the tensors as it is displayed in the screenshot:

**Step 3: Printing the Tensor**

This code is used to check the data type of the values stored in the tensor:

`Ftensor.dtype`

Get the values stored in the tensor using the following code:

`print(Ftensor)`

**Method 3: Using as_tensor() Function**

The next method to convert the list to the tensor is using the as_tensor() function and the following section implements it:

- Step 1: Creating a List
- Step 2: List to Tensor
- Step 3: Printing the Tensor

**Step 1: Creating a List**

Build a simple list to be stored in the variable which can be used to access the list and get the type of the list as well:

```
pylist = [5, 4, 3, 2, 1]
print(type(pylist))
```

**Step 2: List to Tensor**

Use the as_torch() method to convert the list into the tensor and initialize the tensor in the “**pytensor**” variable:

`pytensor = torch.as_tensor(pylist)`

Now, get the type of the values stored in the pytensor variable to confirm that it is converted to the tensor form a list:

`print(type(pytensor))`

**Step 3: Printing the Tensor**

Print the values of the tensor using the following code:

`print(pytensor)`

**Method 4: Using from_numpy() Function **

Another method to convert the list to the tensor is by taking the indirect route which is using the numpy arrays. The list can be converted into arrays and then the arrays can be converted to tensors using the from_numpy() method which is explained below:

- Step 1: Importing “numpy” Library
- Step 2: List to Array & Array to Tensor
- Step 3: Printing the Tensor

**Step 1: Importing “numpy” Library**

To use the from_numpy() method, we need to import the **numpy** library as **np** and then use it to get the job done:

`import numpy as np`

**Step 2: List to Array & Array to Tensor**

Using the list from the previous section and converting it into the array which is stored in the py_array variable. After that, convert the array to the tensor and store it in the py_tensor variable as the Python code is written below:

```
py_array = np.array(pylist)
py_tensor = torch.from_numpy(py_array)
```

Getting the type of the data stored in the py_tensor to validate that the list is successfully converted to the tensor:

`print(type(py_tensor))`

**Step 3: Printing the Tensor**

Print the contents of the variable to make sure that the content of the tensor matches that of list’s created earlier:

`print(py_tensor)`

That’s all about the process of converting the list of the Python to the tensor in PyTorch.

**Bonus Tip: Creating Tensor in PyTorch**

The user can simply create their tensor provided by the PyTorch framework and the following sections explain the process:

**Using a torch.empty() Function**

Build the tensor variable and store the values in 3 rows and 4 columns by providing the arguments in the torch.empty() method:

`tensor = torch.empty(3, 4)`

Print the type of the values stored in the tensor variable:

`print(type(tensor))`

Also, print the values stored in the tensor variable:

`print(tensor)`

**Using torch.zeros() Function**

Store all zeros in the tensor structure by calling the torch.zeros() method with the dimensions as the argument of the method:

```
zeros = torch.zeros(2, 3)
print(zeros)
```

**Using torch.ones() Function**

Store all ones in the tensor and print it on the screen by using the following code:

```
ones = torch.ones(2, 3)
print(ones)
```

That’s all about it.

**Conclusion**

To convert the list to tensor in PyTorch, the framework offers multiple methods like tensor(), FloatTensor(), and as_tensor() methods which are considered as the direct approaches. The indirect approach to converting a list to a tensor is using the from_numpy() method from the numpy library. This guide has explained the process of using all the methods for both approaches with the code implementation.