PyTorch torch.cat() method is **“used to ****concatenate the input sequence of seq tensors in the provided dimension.”** All tensors must either have the same shape (except in the concatenating dimension) or be empty.

**Syntax**

`torch.cat(tensors, dim=0, out=None)`

**Parameters**

**tensors:**A sequence of tensors to concatenate. These tensors must have the same shape, except in the dimension corresponding to dim.

**dim:**The dimension along which the tensors will be concatenated. The default is 0.

**out:**The output tensor (optional).

**Example 1: Concatenating 1-D Tensors**

```
import torch
a = torch.tensor([1, 2])
b = torch.tensor([3, 4])
c = torch.tensor([5, 6])
result = torch.cat((a, b, c), dim=0)
print(result)
```

**Output**

```
tensor([1, 2, 3, 4, 5, 6])
```

**Example 2: Concatenating 2-D Tensors Along Rows (dim=0)**

```
import torch
a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6]])
result = torch.cat((a, b), dim=0)
print(result)
```

**Output**

```
tensor([[1, 2],
[3, 4],
[5, 6]])
```

**Example 3: Concatenating 2-D Tensors Along Columns (dim=1)**

```
import torch
a = torch.tensor([[1, 2]])
b = torch.tensor([[3, 4]])
result = torch.cat((a, b), dim=1)
print(result)
```

**Output**

```
tensor([[1, 2, 3, 4]])
```

**Example 4: Concatenating 3-D Tensors**

```
import torch
a = torch.randn(2, 3, 4)
b = torch.randn(2, 3, 4)
result = torch.cat((a, b), dim=0)
print(result)
```

**Output**

That’s it!

**Related posts**

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Machine Learning frameworks like PyTorch and Tensorflow is a testament to his versatility and commitment to the craft.