 ## Introduction

In this tutorial, we will learn how to use torch.add() function to add tensors in PyTorch. We will see various examples of this function for easy understanding by beginners. So let us get started.

In PyTorch, the tensors can be added easily with the torch add function whose usage is quite intuitive. It also supports broadcasting to a common shape this means the tensors of different sizes can be added together if their shapes are compatible for broadcasting.

### Syntax

The syntax of torch.add function is as follows –

• input: This is the input tensor for addition.
• other: This is the other tensor or scalar to be added to the input tensor above.
• alpha: An optional multiplier to other tensor before it is added to the input tensor. By default, it is 1.
• out: This is the output tensor.

### Import Torch Library

Before going through examples of the torch add function let us first import the torch library.

In :

`import torch;`

In this example, we create a 2D tensor and add a scalar value of 5 to it by using the torch.add function. The scalar value is broadcasted and added to all elements of the tensor as evident in the output.

In :

```tensor1 = torch.arange(12).reshape(4,3)
scalar = 5

print("First Tensor:")
print(tensor1)

print("Scalar:" + str(scalar))
print("Sum of Tensor and Scalar:")

print(sum)```

Out:

```First Tensor:
tensor([[ 0,  1,  2],
[ 3,  4,  5],
[ 6,  7,  8],
[ 9, 10, 11]])

Scalar:5

Sum of Tensor and Scalar:
tensor([[ 5,  6,  7],
[ 8,  9, 10],
[11, 12, 13],
[14, 15, 16]])
```

### Example 2: Adding a 1-D Tensor to 2-D Tensor

In this example, we create a 1-D tensor of size 1×3 and add it to a 2-D tensor of size 4×3. The 1-D tensor is added to each row of the 2D tensor elementwise.

In :

```tensor1 = torch.arange(12).reshape(4,3)
tensor2 = torch.arange(3)

print("First Tensor:")
print(tensor1)

print("Second Tensor:")
print(tensor2)

print("Sum of Tensor1 and Tensor2:")
print(sum)```
Out:
```First Tensor:
tensor([[ 0,  1,  2],
[ 3,  4,  5],
[ 6,  7,  8],
[ 9, 10, 11]])
Second Tensor:
tensor([0, 1, 2])
Sum of Tensor1 and Tensor2:
tensor([[ 0,  2,  4],
[ 3,  5,  7],
[ 6,  8, 10],
[ 9, 11, 13]])```

### Example 3: Adding a 1-D Tensor to 2-D Tensor

In this example, we create a 1-D tensor of size 3×1 and add it to a 2-D tensor of size 4×3. The 1-D tensor is added to each column of the 2D tensor elementwise.
In :
```tensor1 = torch.arange(12).reshape(4,3)
tensor2 = torch.arange(4).reshape(4,1)

print("First Tensor:")
print(tensor1)

print("Second Tensor:")
print(tensor2)

print("Sum of Tensor1 and Tensor2:")
print(sum)```
Out:
```First Tensor:
tensor([[ 0,  1,  2],
[ 3,  4,  5],
[ 6,  7,  8],
[ 9, 10, 11]])
Second Tensor:
tensor([,
,
,
])
Sum of Tensor1 and Tensor2:
tensor([[ 0,  1,  2],
[ 4,  5,  6],
[ 8,  9, 10],
[12, 13, 14]])
```

### Example 4: Element Wise Addition of two 2-D Tensors

In this example, we create two 2D Tensors of the same size 4×3 and then do element-wise addition with the torch add function.

In :

```tensor1 = torch.arange(12).reshape(4,3)
tensor2 = torch.arange(start=4, end=16).reshape(4,3)

print("First Tensor:")
print(tensor1)

print("Second Tensor:")
print(tensor2)

print("Sum of Tensor1 and Tensor2:")
print(sum)```
Out:
```First Tensor:
tensor([[ 0,  1,  2],
[ 3,  4,  5],
[ 6,  7,  8],
[ 9, 10, 11]])
Second Tensor:
tensor([[ 4,  5,  6],
[ 7,  8,  9],
[10, 11, 12],
[13, 14, 15]])
Sum of Tensor1 and Tensor2:
tensor([[ 4,  6,  8],
[10, 12, 14],
[16, 18, 20],
[22, 24, 26]])
```

### Example 5: Adding a 1-D Tensor to 3-D Tensor

In this example, we create a 1-D tensor of size 1×3 and add it to a 3-D tensor of size 2x4x3. In this case, intuitively we can say that the 1-D tensor is added element-wise to each row of the 3-D tensor.

In :

```tensor1 = torch.arange(24).reshape(2,4,3)
tensor2 = torch.arange(3)

print("First Tensor:")
print(tensor1)

print("Second Tensor:")
print(tensor2)

print("Sum of Tensor1 and Tensor2:")
print(sum)```
Out:
```First Tensor:
tensor([[[ 0,  1,  2],
[ 3,  4,  5],
[ 6,  7,  8],
[ 9, 10, 11]],

[[12, 13, 14],
[15, 16, 17],
[18, 19, 20],
[21, 22, 23]]])
Second Tensor:
tensor([0, 1, 2])
Sum of Tensor1 and Tensor2:
tensor([[[ 0,  2,  4],
[ 3,  5,  7],
[ 6,  8, 10],
[ 9, 11, 13]],

[[12, 14, 16],
[15, 17, 19],
[18, 20, 22],
[21, 23, 25]]])
```

### Example 6: Adding a 2-D Tensor to 3-D Tensor

In this example, we are adding a 2-D tenor of size 4×3 to a 3-D tensor of size 2x4x3.

In :

```tensor1 = torch.arange(24).reshape(2,4,3)
tensor2 = torch.arange(12).reshape(4,3)

print("First Tensor:")
print(tensor1)

print("Second Tensor:")
print(tensor2)

print("Sum of Tensor1 and Tensor2:")
print(sum)```
Out:
```First Tensor:
tensor([[[ 0,  1,  2],
[ 3,  4,  5],
[ 6,  7,  8],
[ 9, 10, 11]],

[[12, 13, 14],
[15, 16, 17],
[18, 19, 20],
[21, 22, 23]]])
Second Tensor:
tensor([[ 0,  1,  2],
[ 3,  4,  5],
[ 6,  7,  8],
[ 9, 10, 11]])
Sum of Tensor1 and Tensor2:
tensor([[[ 0,  2,  4],
[ 6,  8, 10],
[12, 14, 16],
[18, 20, 22]],

[[12, 14, 16],
[18, 20, 22],
[24, 26, 28],
[30, 32, 34]]])
```

### Example 7: Adding two 3-D Tensors Element Wise

In this example, we create two 3D Tensors of the same size 2x4x3 and then do element-wise addition with the torch add function.

In :

```tensor1 = torch.arange(24).reshape(2,4,3)
tensor2 = torch.arange(start=4, end=28).reshape(2,4,3)

print("First Tensor:")
print(tensor1)

print("Second Tensor:")
print(tensor2)

print("Sum of Tensor1 and Tensor2:")
print(sum)```
Out:
```First Tensor:
tensor([[[ 0,  1,  2],
[ 3,  4,  5],
[ 6,  7,  8],
[ 9, 10, 11]],

[[12, 13, 14],
[15, 16, 17],
[18, 19, 20],
[21, 22, 23]]])
Second Tensor:
tensor([[[ 4,  5,  6],
[ 7,  8,  9],
[10, 11, 12],
[13, 14, 15]],

[[16, 17, 18],
[19, 20, 21],
[22, 23, 24],
[25, 26, 27]]])
Sum of Tensor1 and Tensor2:
tensor([[[ 4,  6,  8],
[10, 12, 14],
[16, 18, 20],
[22, 24, 26]],

[[28, 30, 32],
[34, 36, 38],
[40, 42, 44],
[46, 48, 50]]])
```

### Example 8: Using torch.add() with Alpha Parameter

In this example, we will see how to use the parameter alpha with torch.add() function. The second tensor is multiplied by the value of alpha = 5 and its output is added to the first tensor.

In :

```tensor1 = torch.arange(12).reshape(4,3)
tensor2 = torch.arange(3)

print("First Tensor:")
print(tensor1)

print("Second Tensor:")
print(tensor2)

print("Sum of Tensor1 and Tensor2:")
print(sum)```
Out:
```First Tensor:
tensor([[ 0,  1,  2],
[ 3,  4,  5],
[ 6,  7,  8],
[ 9, 10, 11]])
Second Tensor:
tensor([0, 1, 2])
Sum of Tensor1 and Tensor2:
tensor([[ 0,  6, 12],
[ 3,  9, 15],
[ 6, 12, 18],
[ 9, 15, 21]])
```