Contents

- 1 Introduction
- 2 Element Wise Tensor Multiplication with torch.mul() & torch.multiply()
- 3 Examples

## 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()**

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

In [1]:

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

tensor([[13, 16, 18], [11, 15, 6], [ 3, 0, 4]])

output_tensor = torch.mul(5,tensor) output_tensor

tensor([[65, 80, 90], [55, 75, 30], [15, 0, 20]])

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

output_tensor = torch.multiply(5,tensor) output_tensor

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

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

tensor([[17, 19, 13, 19], [ 8, 5, 10, 2], [ 0, 7, 18, 5]])

tensor2 = torch.randint(high = 10, size=(1,4)) tensor2

tensor([[2, 8, 5, 3]])

output_tensor = torch.mul(tensor1,tensor2) output_tensor

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

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

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

tensor([[ 1, 1, 9, 15], [ 7, 3, 0, 1], [14, 4, 10, 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]])

output_tensor = torch.mul(tensor1,tensor2) output_tensor

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

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

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]]])

tensor2 = torch.randint(high = 10, size=(3,4)) tensor2

Out[10]:

tensor([[4, 0, 9, 1], [5, 6, 0, 4], [4, 9, 5, 6]])

output_tensor = torch.mul(tensor1,tensor2) output_tensor

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**

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

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]]])

tensor2 = torch.randint(high = 10, size=(1,4)) tensor2

tensor([[2, 4, 0, 6]])

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