Introduction
In this tutorial, we will explain how to multiply tensors in PyTorch with torch.matmul() function. We will see its syntax and see various examples to understand its functionality in a better way.
Syntax of torch.matmul() function
The syntax of torch matmul function is as follows â€“
torch.matmul(tensor1, tensor2, out)
 tensor1 â€“ The first tensor for multiplication
 tensor2 â€“ The second tensor for multiplication
 out â€“ Output tensor, result of multiplication of tensor1 with tensor2
Functionality of torch matmul function
The functionality of torch matmul depends on the dimensions of the two input tensors as follows â€“
 When both tensor1 and tensor2 are of 1Dimension then the dot product is done whose output is a scalar. [See example 1 below]
 When both tensor1 and tensor2 are of 2Dimension then the matrix multiplication is done. However the inner dimension of the two tensors should be the same, i.e. the number of columns in tensor1 should match the number of rows of tensor2. (It is the fundamental rule of matrix multiplication) [See example 2 below]
 When tensor1 is 1Dimension and tensor2 is 2Dimension then internally one dimension is added to tensor1 to make it 2Dimension. After that, matrix multiplication is performed between the two tensors, and the extra dimension is removed from the final result. It should be noted that the 1Dimension tensor should be such that when an extra dimension is added it should match the number of rows of tensor2 to obey the matrix multiplication rule. Otherwise, it will throw an error. [See example 3 below]
 When tensor1 is 2Dimension and tensor2 is 1Dimension then the matrixvector multiplication is done. [See example 4 below]
 When at least one tensor has dimension N where N>2 then batched matrix multiplication is done where broadcasting logic is used. [See example 5 & 6 below]
Examples
To start with the examples, let us first of all import PyTorch library.
In [0]:
importÂ torch;
Â
Example â€“ 1: Multiplying Two 1Dimension Tensors with torch.matmul()
In the first example, we multiply two 1D dimension tensors with torch matmul and the resulting output is scalar.
In [1]:
tensor1Â =Â torch.tensor([2,3]) tensor1
tensor([2, 3])
tensor2Â =Â torch.tensor([4,4]) tensor2
Out[2]:
tensor([4, 4])
In[3]:
outputÂ =Â torch.matmul(tensor1,tensor2) output
Out[3]:
tensor(20)
Example â€“ 2: Multiplying Two 2Dimension Tensors with torch.matmul
In this example, we generate two 2D tensors with randint function of size 4Ã—3 and 3Ã—2 respectively. Do notice that their inner dimension is of the same size i.e. 3 thus making them eligible for matrix multiplication. The output tensor after multiplying with torch matmul is of size 4Ã—2.
In [4]:
tensor1Â =Â torch.randint(highÂ =Â 20,Â size=(4,3)) tensor1
Out[4]:
tensor([[13, 15, 12], [ 6, 13, 19], [ 8, 10, 18], [ 8, 17, 17]])
tensor2Â =Â torch.randint(highÂ =Â 20,Â size=(3,2)) tensor2
Out[5]:
tensor([[ 2, 18], [ 5, 2], [ 9, 10]])
outputÂ =Â torch.matmul(tensor1,tensor2) output
tensor([[209, 384], [248, 324], [228, 344], [254, 348]])
Example â€“ 3: Multiplying 1D Tensor with 2D Tensor
Here we create a 1D tensor and a 2D tensor of size 2Ã—3 and then multiply them with matmul function.
In [7]:
tensor1Â =Â torch.tensor([6,5]) tensor1
tensor([6, 5])
tensor2Â =Â torch.randint(highÂ =Â 20,Â size=(2,3)) tensor2
tensor([[10, 12, 12], [18, 13, 12]])
outputÂ =Â torch.matmul(tensor1,tensor2) output
tensor([150, 137, 132])
Example â€“ 4: Multiplying 2D Tensor with 1D Tensor
Here we create a 2D tensor of size 3Ã—2 and a 1D tensor and then multiply them with PyTorch matmul function.
In [10]:
tensor1Â =Â torch.randint(highÂ =Â 20,Â size=(3,2)) tensor1
tensor([[ 5, 10], [13, 0], [ 7, 1]])
tensor2Â =Â torch.tensor([2,5]) tensor2
tensor([2, 5])
outputÂ =Â torch.matmul(tensor1,tensor2) output
tensor([60, 26, 19])
Example â€“ 5: Multiplying 2D Tensor with 3D Tensor
In this example, we create a 2D tensor of size 4Ã—3 and a 3D tensor of size 2x3x2 and then multiply them.
In [13]:
tensor1 = torch.randint(high = 20, size=(4,3)) tensor1
tensor([[16, 18, 8], [ 5, 12, 12], [17, 7, 5], [15, 5, 14]])
tensor2Â =Â torch.randint(highÂ =Â 20,Â size=(2,3,2)) tensor2
Out[14]:
tensor([[[14, 15], [13, 3], [ 8, 13]], [[ 2, 12], [ 4, 7], [ 1, 3]]])
outputÂ =Â torch.matmul(tensor1,tensor2) output
tensor([[[522, 398], [322, 267], [369, 341], [387, 422]], [[112, 342], [ 70, 180], [ 67, 268], [ 64, 257]]])
Example â€“ 6: Multiplying Two 3D Tensors
In this example, we create two 3D tensors and then multiply them with PyTorch matmul function.
In [16]:
tensor1Â =Â torch.randint(highÂ =Â 20,Â size=(2,4,3)) tensor1
tensor([[[ 6, 14, 8], [16, 19, 7], [ 2, 1, 13], [ 0, 17, 19]], [[ 4, 16, 12], [14, 16, 15], [18, 18, 5], [ 4, 8, 8]]])
tensor2Â =Â torch.randint(highÂ =Â 20,Â size=(2,3,2)) tensor2
tensor([[[18, 19], [17, 5], [ 4, 8]], [[ 1, 8], [ 2, 13], [14, 2]]])
outputÂ =Â torch.matmul(tensor1,tensor2) output
tensor([[[378, 248], [639, 455], [105, 147], [365, 237]], [[204, 264], [256, 350], [124, 388], [132, 152]]])

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.