PyTorch Tensors store and manage data in multi-dimensions to build deep learning models efficiently. The user can integrate multiple tensors to be used as one containing the contents of all the tensors with the same structure. Adding the values of the different tensors does not change or alter the original one in any way only if the user changes the structure of one tensor to match the other one before appending them.

**Table of Content**

This guide explains the following sections:

- What are the Methods for Appending PyTorch Tensor
- How to Append PyTorch Tensor
- Prerequisites
- Method 1: Appending PyTorch Tensors Using cat() Function
- Method 2: Appending PyTorch Tensors Using stack() Function
- Conclusion

**What are the Methods for Appending PyTorch Tensor**

The torch framework offers a couple of methods to append the tensors in PyTorch such as cat() and stack(). These methods are very simple to use as the cat() method simply concatenates the tensors without changing their current dimensions. The stack() method also joins the tensors in PyTorch; however, it adds a dimension to the tensors with the current dimensions.

**How to Append PyTorch Tensor**

Tensors contain the values in the structured format for the models to extract useful information and provide better performance. Sometimes, the user needs to append multiple tensors providing huge datasets to the model or add useful features to the data. To learn the process of appending PyTorch tensors, simply go through the following instructions:

**Note**: The Python code used to complete the process can be accessed from here

**Prerequisites**

Get started with the process by performing the following prerequisites that are required to smoothly go through with the use of PyTorch functions:

- Get Environment for Python
- Install Modules
- Import Libraries

**Get Environment for Python**

The first requirement is the creation of the Google colaboratory which is the development environment for the Python language. Head into the official website, click on the “**New Notebook**” button, and wait for a few moments for its creation on the cloud:

**Install Modules**

From the notebook, install the torch framework to get the libraries and dependencies provided by the platform using the pip command. The pip is the manager for the Python packages containing all the frameworks and modules used in the Python programming language:

`pip install torch`

**Import Libraries**

Once the torch module is installed, get the torch library by typing the import keyword with the name of the library and start using its functions:

`import torch`

Confirm the installation of the torch library to the colaboratory notebook by printing its version on the screen:

`print(torch.__version__)`

**Method 1: Appending PyTorch Tensors Using cat() Function**

After completing all the requirements from the prerequisite section, the first method to append the PyTorch tensors is using the cat() function. It simply integrates the values from different tensors and then creates a single tensor containing all of their values. The following is the syntax for using the cat() method to complete the process and then it will be implemented using multiple examples:

**Syntax: **

The following is the syntax for using the cat() function to append multiple tensors without changing its structure:

`torch.cat ( (tens_1, tens_2, --, tens_n), dim=0, *, out=None)`

The above code suggests:

- The
**torch**is the name of the parent library to call the function **cat()**is the method that integrates all the tensors with multiple arguments- The first argument contains the
**list of the tensors**that are going to be integrated together which are separated by commas - The
**dim**argument refers to the dimensions of the resultant tensor and it can also take the dimensions from the list of tensors if it is not specified in the method - The
**out**arguments explain the output tensor and are set to “**None**” by default.

The syntax for using the cat() methods can be implemented in multiple ways and the following examples illustrate exactly that:

- Example 1: Append List of Tensors in One Tensor
- Example 2: Append Same Dimensional Tensors
- Example 3: Append Tensors Using Dimensions
- Example 4: Append MultiDimensional Tensor
- Example 5: Append Tensors Using Loop
- Example 6: Append One Value to Multiple Tensors
- Example 7: Append Different Dimensional Tensors
- Example 8: Append a List to the Tensor

**Example 1: Append List of Tensors in One Tensor**

The first example simply creates four tensors and stores them in different variables containing different values. Then, it simply uses the cat() function with the name of the tensors as the argument of the method to add them together:

```
a = torch.tensor([9, 8, 7])
b = torch.tensor([4, 5, 6])
c = torch.tensor([3, 2, 1])
d = torch.tensor([-2, -1, 0])
result= torch.cat((a,b,c,d))
```

After applying the cat() method, simply print the result variable containing the integration of all the tensors:

`result`

**Example 2: Append Same Dimensional Tensors**

Create two tensors and use the cat() method to integrate them in the resultant tensor with the exact formation in which the values are required. It means that the values of the tensor will be stored in the resultant tensors according to the arguments given to the cat() method. The xy variable takes the x tensor first and then the y one so the output will return the values of x first and then values of y:

```
x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])
xy = torch.cat((x,y))
yx = torch.cat((y,x))
```

Print both the variables to understand the simple difference and use them accordingly if the orientation of the values is important for the project:

```
print('xy:', xy)
print('yx:', yx)
```

**Example 3: Append Tensors Using Dimensions **

Create two tensors and append them using the cat() function with the additional argument called dim. The dim keyword is not a requirement as only values will be considered as the dimension and they can be concatenated along 0 and 1 dimension in 2 dimensional tensors.

```
x = torch.tensor([[1, 2, 3]])
y = torch.tensor([[4, 5, 6]])
print('0:', torch.cat((x, y), 0))
print('\n1:', torch.cat((x, y), 1))
```

The 0 dimension takes the first dimensions and adds both the tensors separately making it one tensor. The 1 value uses the second dimension for the output tensor and adds all the values from both tensors making only one tensor:

**Example 4: Append MultiDimensional Tensor**

Create 2 multi-dimensional tensors storing them in different variables and then print their values on the screen:

```
one = torch.Tensor([[11, 12, 13], [14, 15, 16]])
two = torch.Tensor([[17, 18, 19], [20, 21, 22]])
print("tens_1 \n", one)
print("tens_2 \n", two)
```

For multi-dimensional tensors, we can use two dimensions such as -1 and 0 as the following code uses both of them. Use the cat() method with the list of tensors to be appended as the argument and the dimensions for structuring the resultant tensor:

```
tens = torch.cat((one, two), -1)
print("appended tensors in the -1 dimension \n", tens)
tens = torch.cat((one, two), 0)
print("\n\n appended tensors in the 0 dimension \n", tens)
```

The -1 dimension takes the first dimension of each tensor according to their location and then takes the 2nd dimension of both the tensors. However, the 0 dimension simply takes both dimensions of the first tensor followed by the dimensions of the second tensor:

**Example 5: Append Tensors Using Loop**

Appending tensors using a **loop** in Python is another useful approach as adding values to the tensors is a time-consuming job. If the user doesn’t have specific values to add to the tensor then simply create an empty tensor and run the loop as many times as many values required to be stored on the tensor. With each **iteration** of the loop, a value will be added to the tensor and when the loop ends, we will have an updated tensor:

```
a = torch.tensor(())
for i in range(3):
i = torch.tensor([i]).float()
a = torch.cat((a, i), 0)
print(a)
```

An empty tensor is stored in the variable “**a**” and the loop starts from 0 to the 3 iterations. With each iteration of the loop, a value will be stored in the variable **i**, and the **float()** method determines the datatype of the values. After that, run the cat() method with each iteration to concatenate the values stored in the variables **a** and **i** updating the variable **a** with each iteration:

**Example 6: Append One Value to Multiple Tensors**

The following example takes multiple tensors and adds a zero at the end of each tensor using the cat() method. Create four tensors with similar structures and random values before printing them on the screen:

```
my_tensor = torch.tensor([[0.1111, 0.2333], [0.6667, 2.0000], [0.2222, 0.3333], [0.7778, 2.8889]])
my_tensor
```

Create another tensor with 4 rows and one column containing only zeros and then use the cat() method with both tensors. It will add zeros at the end of all the dimensions in the my_tensor variable and store it in the n variable:

```
z = torch.zeros((4,1))
n= torch.cat((my_tensor,z), 1)
n
```

**Example 7: Append Different Dimensional Tensors**

Another example uses the cat() method to concatenate tensors with different dimensions. Call the **unsqueeze()** method inside the cat() method to change the dimensions of the second tensor making them similar in structure. After that, it simply adds the structure of both the tensors and returns the structure by calling the **shape** keyword with the name of the tensor:

```
ten_1 = torch.randn([32, 100, 1])
ten_2 = torch.randn([32, 100])
new_ten = torch.cat((ten_1, ten_2.unsqueeze(2)), dim=-1)
print(new_ten.shape)
```

**Example 8: Append a List to the Tensor**

This example adds the tensor to the list which can not be directly and it is required to convert the list to tensor. Create a tensor and a list by executing the following code:

```
tensor = torch.tensor([1, 2, 3])
list = [4, 5, 6]
```

Convert the list to the tensor using the **torch.tensor()** method with the name of the list as the argument. the tensor() method converts the list or arrays provided to its arguments to the tensors:

`new_list = torch.tensor(list)`

Ads the tensor and the **new_list** which is converted to the tensor now and can be appended to the tensors using the cat() method:

```
result = torch.cat((tensor, new_list), dim=0)
print(result)
```

**Method 2: Appending PyTorch Tensors Using stack() Function**

The second method of appending the PyTorch tensors using the stack() function adds the list of tensors to one tensor. The stack() method also changes the dimensions of the tensors while appending them together in a single tensor. The following is the syntax of using the stack() method to append the tensors:

**Syntax: **

The following is the syntax of using the **stack()** method with the torch keyword referring to its library. It contains similar arguments as the cat() method possesses and is explained previously and the dimension should lie between 0 and the tensor’s dimension:

`torch.stack ( (tens_1, tens_2, -- , tens_n), dim=0, *, out=None)`

The following sections use the stack() method with multiple examples as mentioned below:

- Example 1: Append Multiple Tensors in One Tensor
- Example 2: Append Different Dimensional Tensors

**Example 1: Append Multiple Tensors in One Tensor**

Create two tensors in the variable x and y containing one tensor with 3 values each. Use the stack() method to concatenate them together and print the contents of the new tensor:

```
x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])
xy = torch.stack((x,y))
yx = torch.stack((y,x))
print('xy:', xy)
print('yx:', yx)
```

The stack method adds a dimension to the original one as displayed in the screenshot below

**Example 2: Append Different Dimensional Tensors**

The last example is for appending the tensors with different dimensions using the stack() method. Create multiple tensors with different dimensions and then use the **squeeze()** method to change the dimension of the first tensor and add it to the second one. After that, print the size of the new tensor using the shape keyword to check the dimensions of the new tensor:

```
ten_1 = torch.randn([32, 100, 1])
ten_2 = torch.randn([32, 100])
new_ten = torch.stack((ten_1.squeeze(), ten_2), dim=-1)
print(new_ten.shape)
```

The stack method has added a dimension to the original one by appending both the tensors:

That’s all about how to append PyTorch tensors with the help of various examples.

**Conclusion**

To append the PyTorch tensors, the platform offers the **cat()** and **stack()** methods with the torch keyword suggesting the parent library. The cat() method concatenates the tensors without changing the dimensions of the original tensors. The stack() method adds the tensors together with an additional dimension added to them making the stack of the tensors. This guide has explained both methods with the help of multiple examples to append the PyTorch tensors.