PyTorch Zero to Hero (Math operations) ~ 3
This guide, which is the 3rd installment of the series “PyTorch Zero to Hero,” will help you learn basic math operations that are normally used in deep learning projects. By mastering these math operations, you’ll be able to use PyTorch effectively for your ML, and DL projects.
Check out the previous blogs of this series:
1. torch.abs
torch.abs(input, *, out=None) → Tensor
Calculates the absolute value of each element in input
import torch
torch.abs(torch.tensor([[-3, -1, 5],
[-1, -4, 2]]))
## Output
tensor([[3, 1, 5],
[1, 4, 2]])
2. torch.add
torch.add(input, other, *, alpha=1, out=None) → Tensor
Input is added to other which is scaled by a factor of alpha
a = torch.tensor([[-3, -1, 5],
[-1, -4, 2]])
a
## Output
tensor([[-3, -1, 5],
[-1, -4, 2]])
b = torch.tensor([[2, 0, 5],
[5, -1, 3]])
c = torch.tensor([[-4, 3, 0],
[5, 4, -2]])
torch.add(b,c,alpha=2)
## Output
tensor([[-6, 6, 5],
[15, 7, -1]])
3. torch.angle
torch.angle(input, *, out=None) → Tensor
Computes element wise angle of the given input tensor in radians
torch.angle(torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j]))*180/3.14159
## Output
tensor([135.0001, 135.0001, -45.0000])
4. torch.ceil
torch.ceil(input, *, out=None) → Tensor
Returns a new tensor where each element is replaced by the smallest integer greater than or equal to that element.
a = torch.randn(4)
print("Original tensor",a)
print("Ceiled tensor",torch.ceil(a))
## Output
Original tensor tensor([ 0.4485, -0.5410, -0.9393, 1.0870])
Ceiled tensor tensor([1., -0., -0., 2.])
5. torch.cos
torch.cos(input, *, out=None) → Tensor
Returns a new tensor with the cosine of the elements of
input
a = torch.randn(4)
print("Original tensor",a)
print("Cosine of Original tensor",torch.cos(a))
## Output
Original tensor tensor([-1.4555, -0.1321, 1.6708, 1.1740])
Cosine of Original tensor tensor([ 0.1150, 0.9913, -0.0999, 0.3865])
6. torch.div
torch.div(input, other, *, rounding_mode=None, out=None) → Tensor
Each element of the input is divided by corresponding element of the other
## Division by a scalar value
x = torch.tensor([[2, 0, 4],
[6, -4, 1]])
torch.div(x, 2)
## Output
tensor([[ 1.0000, 0.0000, 2.0000],
[ 3.0000, -2.0000, 0.5000]])
a = torch.tensor([[2, 0, 4],
[4, -1, 3]])
b = torch.tensor([-4, 3, 1])
print("No rounding \n",torch.div(a,b))
print("Trunc division \n", torch.div(a, b, rounding_mode='trunc'))
print("Floor division \n", torch.div(a, b, rounding_mode='floor'))
## Output
No rounding
tensor([[-0.5000, 0.0000, 4.0000],
[-1.0000, -0.3333, 3.0000]])
Trunc division
tensor([[ 0, 0, 4],
[-1, 0, 3]])
Floor division
tensor([[-1, 0, 4],
[-1, -1, 3]])
rounding_mode (str, optional) –
Type of rounding applied to the result:
None — default behavior. Performs no rounding and, if both
input
andother
are integer types, promotes the inputs to the default scalar type. Equivalent to true division in Python (the/
operator) and NumPy’snp.true_divide
.
"trunc"
- rounds the results of the division towards zero. Equivalent to C-style integer division.
"floor"
- rounds the results of the division down. Equivalent to floor division in Python (the//
operator) and NumPy’snp.floor_divide
. ~ Source
Note: torch.div is the same as torch.divide
7. torch.exp
torch.exp(input, *, out=None) → Tensor
Return the exponential of the elements of the input tensor in tensor format.
import math
torch.exp(torch.tensor([0, math.log(2.), math.log(4.)]))
## Output
tensor([1., 2., 4.])
8. torch.floor
torch.floor(input, *, out=None) → Tensor
A new tensor is returned with the floor of the elements of
input
tensor
a = torch.tensor([-0.3,1.2,4.5])
torch.floor(a)
## Output
tensor([-1., 1., 4.])
9. torch.log
torch.log(input, *, out=None) → Tensor
A new tensor with the natural logarithm of the elements of input
tensor
import math
torch.log(torch.tensor([1, math.exp(2.), math.exp(4.)]))
## Output
tensor([0., 2., 4.])
10. torch.mul
torch.mul(input, other, *, out=None) → Tensor
Return a new tensor which is the product of input and other
a = torch.tensor([2,2,2])
print("Multiplication by a scalar", torch.mul(a,5))
b = torch.tensor([3,4,3])
print("Multiplication by a tensor", torch.mul(a,b))
## Output
Multiplication by a scalar tensor([10, 10, 10])
Multiplication by a tensor tensor([6, 8, 6])
Note : torch.mul and torch.multiply does the same thing
11. torch.neg
torch.neg(input, *, out=None) → Tensor
A new tensor is returned with negative of the elements from input tensor
a = torch.tensor([-3,4.3,-1.5,0])
print("Negative of the tensor", torch.neg(a))
## Output
Negative of the tensor tensor([ 3.0000, -4.3000, 1.5000, -0.0000])
Note: torch.neg is the same as torch.negative
12. torch.pow
torch.pow(input, exponent) → Tensor
Calculates the power of each element in the input tensor with a given exponent and returns a tensor with the results.
- Exponent as scalar
a = torch.tensor([2,2,2])
print("Original Tensor",a)
print("Powered Tensor",torch.pow(a, 2))
## Output
Original Tensor tensor([2, 2, 2])
Powered Tensor tensor([4, 4, 4])
- Exponent as tensor
exp = torch.arange(1, 6)
a = torch.tensor([2,2,2,2,2])
print("Original Tensor",a)
print("Powered Tensor",torch.pow(a,exp))
## Output
Original Tensor tensor([2, 2, 2, 2, 2])
Powered Tensor tensor([ 2, 4, 8, 16, 32])
13. torch.round
torch.round(input, decimals=0) → Tensor
Rounds each element of the input to the nearest integer.
a = torch.round(torch.tensor((1.7, -3.3, 0.1, -5.7)))
print("Rounded tensor: ",a)
# A positive decimals argument rounds to the to that decimal place
b = torch.round(torch.tensor([506.3217]), decimals=3)
print("Rounded tensor: ",b)
# A negative decimals argument rounds to the left of the decimal
c = torch.round(torch.tensor([506.3217]), decimals=-3)
print("Rounded tensor: ",c)
## Output
Rounded tensor: tensor([ 2., -3., 0., -6.])
Rounded tensor: tensor([506.3220])
Rounded tensor: tensor([1000.])
Note:
torch.ceil()
, which rounds up.torch.floor()
, which rounds down.torch.trunc()
, which rounds towards zero.
14. torch.sqrt
torch.sqrt(input, *) → Tensor
Returns a new tensor containing the square roots of the elements from the input.
a = torch.tensor([-4, 0, 9, 15])
torch.sqrt(a)
## Output
tensor([ nan, 0.0000, 3.0000, 3.8730])
15. torch.square
torch.square(input, *, out=None) → Tensor
Returns a new tensor containing the squares of the elements from the input.
a = torch.tensor([-4, 0, 9, 15])
torch.square(a)
## Output
tensor([ 16, 0, 81, 225])
16. torch.trunc
torch.trunc(input, *, out=None) → Tensor
Returns a new tensor with the elements of the input truncated to their integer values.
a =torch.tensor([-4.121, -0.812, 0.121, 1.430, 9.11, -15.543])
torch.trunc(a)
## Output
tensor([ -4., -0., 0., 1., 9., -15.])