Pytorch Arithmetic Operations

Pytorch allows you to perform arithmetic operations on tensors. In this chapter of Pytorch tutorial, you will learn how to do arithmetic operations of Pytorch tensors. These operations can be performed by using pytorch arithmetic operation functions or python operands. The arithmetic operations are performed element-wise.

Addition

The addition of two tensors can be performed by using the + operand.

Example

tensor_1 = torch.tensor([[1, 2], [1, 3]])
tensor_2 = torch.tensor([[2, 3], [0, 4]])

print(tensor_1 + tensor_2)
# Outputs- tensor([[3, 5], [1, 7]])

Addition operation can also be performed using the torch.add() function. Calling add() function on the tensor can also be used to perform the addition of two tensors.

Example

tensor_1 = torch.tensor([[1, 2], [1, 3]])
tensor_2 = torch.tensor([[2, 3], [0, 4]])

print(torch.add(tensor_1, tensor_2))
# Outputs- tensor([[3, 5], [1, 7]])

print(tensor_1.add(tensor_2))
# Outputs- tensor([[3, 5], [1, 7]])

Subtraction

The subtraction of two tensors can be performed by using the - operand.

Example

tensor_1 = torch.tensor([[1, 2], [1, 3]])
tensor_2 = torch.tensor([[2, 3], [0, 4]])

print(tensor_1 - tensor_2)
# Outputs- tensor([[-1, -1], [1, -1]])

Subtraction operation can also be performed using the torch.sub() function. Calling sub() function on the tensor can also be used to perform the subtraction of two tensors.

Example

tensor_1 = torch.tensor([[1, 2], [1, 3]])
tensor_2 = torch.tensor([[2, 3], [0, 4]])

print(torch.sub(tensor_1, tensor_2))
# Outputs- tensor([[-1, -1], [1, -1]])

print(tensor_1.sub(tensor_2))
# Outputs- tensor([[-1, -1], [1, -1]])

Multiplication

The multiplication of two tensors can be performed by using the * operand.

Example

tensor_1 = torch.tensor([[1, 2], [1, 3]])
tensor_2 = torch.tensor([[2, 3], [0, 4]])

print(tensor_1 * tensor_2)
# Outputs- tensor([[2, 6], [0, 12]])

Multiplication operation can also be performed using the torch.mul() function. Calling mul() function on the tensor can also be used to perform the multiplication of two tensors.

Example

tensor_1 = torch.tensor([[1, 2], [1, 3]])
tensor_2 = torch.tensor([[2, 3], [0, 4]])

print(torch.mul(tensor_1, tensor_2))
# Outputs- tensor([[2, 6], [0, 12]])

print(tensor_1.mul(tensor_2))
# Outputs- tensor([[2, 6], [0, 12]])

Division

The division of two tensors can be performed by using the / operand.

Example

tensor_1 = torch.tensor([[1, 2], [1, 3]])
tensor_2 = torch.tensor([[2, 3], [0, 4]])

print(tensor_1 / tensor_2)
# Outputs- tensor([[0.5000, 0.6667], [inf, 0.7500]])

Note– Notice how division by 0 yields a value of inf – Infinity.

Division operation can also be performed using the torch.div() function. Calling div() function on the tensor can also be used to perform division of two tensors.

Example

tensor_1 = torch.tensor([[1, 2], [1, 3]])
tensor_2 = torch.tensor([[2, 3], [0, 4]])

print(torch.div(tensor_1, tensor_2))
# Outputs- tensor([[0.5000, 0.6667], [inf, 0.7500]])

print(tensor_1.div(tensor_2))
# Outputs- tensor([[0.5000, 0.6667], [inf, 0.7500]])

Exponent

The elements of a tensor can be raised to the power of their respective elements in the other tensor using the ** operator.

Example

tensor_1 = torch.tensor([[1, 2], [1, 3]])
tensor_2 = torch.tensor([[2, 3], [0, 4]])

print(tensor_1 ** tensor_2)
# Outputs- tensor([[1, 8], [1, 81]])

Exponentiation operation can also be performed using the torch.pow() function. Calling pow() function on the tensor can also be used to perform the exponetiation.

Example

tensor_1 = torch.tensor([[1, 2], [1, 3]])
tensor_2 = torch.tensor([[2, 3], [0, 4]])

print(torch.pow(tensor_1, tensor_2))
# Outputs- tensor([[1, 8], [1, 81]])

print(tensor_1.pow(tensor_2))
# Outputs- tensor([[1, 8], [1, 81]])

Note– To raise all the elements in the tensor to constant power, supply a scalar to the pow() function instead of a tensor. This is known as broadcasting, and will be discussed in the next chapter.


Remainder

The remainder operation can be performed using the % operand. This will calculate the remainder of elements in the first tensor after division by respective elements in the second tensor.

Example

tensor_1 = torch.tensor([[3, 8], [9, 4]])
tensor_2 = torch.tensor([[2, 3], [5, 4]])

print(tensor_1 % tensor_2)
# Outputs- tensor([[1, 2], [4, 0]])

Note– A ZeroDivisionError will be returned if any of the elements in the divisor tensor is 0.

The remainder operation can also be performed by using the torch.remainder() function. Calling the remainder() function on the tensor can also be used to perform remainder operation on two tensors.

Example

tensor_1 = torch.tensor([[1, 2], [1, 3]])
tensor_2 = torch.tensor([[2, 3], [0, 4]])

print(torch.remainder(tensor_1, tensor_2))
# Outputs- tensor([[1, 2], [4, 0]])

print(tensor_1.remainder(tensor_2))
# Outputs- tensor([[1, 2], [4, 0]])