# How to perform element-wise multiplication on tensors in PyTorch?

PythonPyTorchServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

torch.mul() method is used to perform element-wise multiplication on tensors in PyTorch. It multiplies the corresponding elements of the tensors. We can multiply two or more tensors. We can also multiply scalar and tensors. Tensors with same or different dimensions can also be multiplied. The dimension of the final tensor will be same as the dimension of higher-dimensional tensor. Element-wise multiplication on tensors are also known as Hadamard product.

## Steps

• Import the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.

• Define two or more PyTorch tensors and print them. If you want to multiply a scalar quantity, define it.

• Multiply two or more tensors using torch.mul() and assign the value to a new variable. You can also multiply a scalar quantity and a tensor. Multiplying the tensors using this method does not make any change in the original tensors.

• Print the final tensor.

## Example 1

The following program shows how to multiply a scalar with a tensor. The same result can also be obtained using a tensor instead of the scalar.

# Python program to perform element--wise multiplication
# import the required library
import torch

# Create a tensor
t = torch.Tensor([2.05, 2.03, 3.8, 2.29])
print("Original Tensor t:\n", t)

# Multiply a scalar value to a tensor
v = torch.mul(t, 7)
print("Element-wise multiplication result:\n", v)

# Same result can also be obtained as below
t1 = torch.Tensor([7])
w = torch.mul(t, t1)
print("Element-wise multiplication result:\n", w)

# other way to do above operation
t2 = torch.Tensor([7,7,7,7])
x = torch.mul(t, t2)
print("Element-wise multiplication result:\n", x)

## Output

Original Tensor t:
tensor([2.0500, 2.0300, 3.8000, 2.2900])
Element-wise multiplication result:
tensor([14.3500, 14.2100, 26.6000, 16.0300])
Element-wise multiplication result:
tensor([14.3500, 14.2100, 26.6000, 16.0300])
Element-wise multiplication result:
tensor([14.3500, 14.2100, 26.6000, 16.0300])

## Example 2

The following Python program shows how to multiply a 2D tensor with a 1D tensor.

import torch
# Create a 2D tensor
T1 = torch.Tensor([[3,2],[7,5]])

# Create a 1-D tensor
T2 = torch.Tensor([10, 8])
print("T1:\n", T1)
print("T2:\n", T2)

# Multiply 1-D tensor with 2-D tensor
v = torch.mul(T1, T2) # v = torch.mul(T2,T1)
print("Element-wise multiplication result:\n", v)

## Output

T1:
tensor([[3., 2.],
[7., 5.]])
T2:
tensor([10., 8.])
Element-wise multiplication result:
tensor([[30., 16.],
[70., 40.]])

## Example 3

The following Python program shows how to multiply two 2D tensors.

import torch

# create two 2-D tensors
T1 = torch.Tensor([[8,7],[3,4]])
T2 = torch.Tensor([[0,3],[4,9]])
print("T1:\n", T1)
print("T2:\n", T2)

# Multiply above two 2-D tensors
v = torch.mul(T1,T2)
print("Element-wise subtraction result:\n", v)

## Output

T1:
tensor([[8., 7.],
[3., 4.]])
T2:
tensor([[0., 3.],
[4., 9.]])
Element-wise subtraction result:
tensor([[ 0., 21.],
[12., 36.]])
Updated on 06-Nov-2021 09:49:57