# Element Wise Multiplication of Tensors in PyTorch with torch.mul() & torch.multiply()

## Introduction

In this article, we will see how we can perform element-wise multiplication of tensors in PyTorch by using torch.mul() or torch.multiply() function. We will see various examples to understand better how these functions work.

## Element Wise Tensor Multiplication with torch.mul() & torch.multiply()

torch.mul() function in PyTorch is used to do element-wise multiplication of tensors. It should be noted here that torch.multiply() is just an alias for torch.mul() function and they do the same work.

Using either of torch.mul() or torch.multiply() you can do element-wise tensor multiplication between –

• A scalar and tensor.
• Two tensors of the same dimensions.
• Two tensors of different dimensions provided the size of at least one dimension is the same.

## Examples

First of all, let us import PyTorch library before we start the examples.

In [0]:

import torch;

### Example – 1: Multiplying 2D Tensor and Scalar with torch.mul()

We first create a random 2D tensor of size 3×3 and then multiply it with the scalar number 5. It can be done in three ways –

#### Method 1: By using torch.mul()

In [1]:

tensor = torch.randint(high = 20, size=(3,3))

tensor
Out[1]:
tensor([[13, 16, 18],
[11, 15,  6],
[ 3,  0,  4]])
In [2]:
output_tensor = torch.mul(5,tensor)

output_tensor
Out[2]:
tensor([[65, 80, 90],
[55, 75, 30],
[15,  0, 20]])

#### Method 2: By using the alias torch.multiply()

In [3]:
output_tensor = torch.multiply(5,tensor)

output_tensor
Out[3]:
tensor([[65, 80, 90],
[55, 75, 30],
[15,  0, 20]])

#### Method 3: By using Mathematical Multiplication Symbol

In [4]:

output_tensor = tensor*5

output_tensor
Out[4]:
tensor([[65, 80, 90],
[55, 75, 30],
[15,  0, 20]])

### Example – 2: Multiplying 2D Tensor with 1D Tensor

In this example, we first create a 2D tensor of size 3×4 and one 1D tensor of size 1×4. Finally, we do element-wise multiplication with torch mul function.

In [5]:

tensor1 = torch.randint(high = 20, size=(3,4))

tensor1
Out[5]:
tensor([[17, 19, 13, 19],
[ 8,  5, 10,  2],
[ 0,  7, 18,  5]])
tensor2 = torch.randint(high = 10, size=(1,4))

tensor2
Out[4]:
tensor([[2, 8, 5, 3]])
In[5]:
output_tensor = torch.mul(tensor1,tensor2)

output_tensor
Out[5]:
tensor([[ 34, 152,  65,  57],
[ 16,  40,  50,   6],
[  0,  56,  90,  15]])

### Example – 3: Multiplying two 2D Tensors of Same Size

In this example, we create two 2D tensors of same size 3×4 and then multiply them with PyTorch mul function.
In[6]:
tensor1 = torch.randint(high = 20, size=(3,4))

tensor1
Out[6]:
tensor([[ 1,  1,  9, 15],
[ 7,  3,  0,  1],
[14,  4, 10,  7]])
In[7]:
tensor2 = torch.randint(high = 10, size=(3,4))

tensor2

Out[7]:

tensor([[0, 4, 7, 8],
[0, 9, 5, 6],
[9, 7, 5, 5]])
In[8]:
output_tensor = torch.mul(tensor1,tensor2)

output_tensor
Out[8]:
tensor([[  0,   4,  63, 120],
[  0,  27,   0,   6],
[126,  28,  50,  35]])

### Example – 4: Multiplying 3D Tensor with 2D Tensor

In this example, we create a 3D tensor of size 2x3x4 and one 2D tensor of size 3×4 and then multiply them both to get the final output.
In[9]:
tensor1 = torch.randint(high = 20, size=(2,3,4))

tensor1

Out[9]:

tensor([[[ 4,  9, 15,  1],
[ 7, 18, 14, 19],
[19, 19, 15, 10]],

[[ 4,  1,  2, 17],
[19,  4,  2,  9],
[14,  1, 14, 13]]])
In[10]:
tensor2 = torch.randint(high = 10, size=(3,4))

tensor2

Out[10]:

tensor([[4, 0, 9, 1],
[5, 6, 0, 4],
[4, 9, 5, 6]])
In[11]:
output_tensor = torch.mul(tensor1,tensor2)

output_tensor
Out[11]:
tensor([[[ 16,   0, 135,   1],
[ 35, 108,   0,  76],
[ 76, 171,  75,  60]],

[[ 16,   0,  18,  17],
[ 95,  24,   0,  36],
[ 56,   9,  70,  78]]])

### Example – 5: Multiplying 3D Tensor with 1D Tensor

In this example, we create a 3D tensor of size 2x3x4 and another 1D tensor of size 1×4 and then multiply them element-wise with torch mul function.
In[12]:
tensor1 = torch.randint(high = 20, size=(2,3,4))

tensor1
Out[12]:
tensor([[[ 5, 10, 13, 16],
[ 4, 17, 18,  7],
[19, 14,  4, 10]],

[[ 2,  3,  0, 10],
[15, 15,  9, 16],
[ 5,  2,  3,  5]]])
In [13]:
tensor2 = torch.randint(high = 10, size=(1,4))

tensor2
Out[13]:
tensor([[2, 4, 0, 6]])
In[14]:
output_tensor = torch.mul(tensor1,tensor2)

output_tensor

Out[14]:

tensor([[[10, 40,  0, 96],
[ 8, 68,  0, 42],
[38, 56,  0, 60]],

[[ 4, 12,  0, 60],
[30, 60,  0, 96],
[10,  8,  0, 30]]])

Reference: PyTorch Documentation

• MLK is a knowledge sharing community platform for machine learning enthusiasts, beginners and experts. Let us create a powerful hub together to Make AI Simple for everyone.