The Pytorch library has many inbuilt Math functions. These can be used to perform various specific Mathematical Operation. In this chapter of Pytorch Tutorial, you will learn about a handful of math functions in Pytorch.
torch.abs()
torch.abs(x)
returns a tensor whose elements are the absolute values of the elements in the original tensor x
. The abs()
function does not make changes to the original tensor, it creates a new tensor to store the result and returns it. To make changes to the original tensor, you should call abs_()
function.
abs()
and abs_()
can also be called directly on a tensor.
Example
x = torch.tensor([[-1, 4], [-2, 1]])
# Does not make changes to tensor x, stores the result in y
y = torch.abs(x)
print(y)
# Outputs- tensor([[1, 4], [2, 1]])
# abs() function is called directly on the tensor
print(x.abs())
# Outputs- tensor([[1, 4], [2, 1]])
# Changes the original tensor
x.abs_()
print(x)
# Outputs- tensor([[1, 4], [2, 1]])
torch.round()
torch.round(x)
returns a tensor with the elements of the original tensor x
, rounded to the nearest integer. The round()
function does not make changes to the original tensor, it creates a new tensor to store the result and returns it. To make changes to the original tensor, you should call round_()
function.
round()
and round_()
can also be called directly on a tensor.
Example
x = torch.tensor([[-1.1, 4.7], [-2.9, 1.24]])
# Does not make changes to tensor x, stores the result in y
y = torch.round(x)
print(y)
# Outputs- tensor([[-1., 5.], [-3., 1.]])
# round() function is called directly on the tensor
print(x.round())
# Outputs- tensor([[-1., 5.], [-3., 1.]])
# Changes the original tensor
x.round_()
print(x)
# Outputs- tensor([[-1., 5.], [-3., 1.]])
Note– The round()
and round_()
functions accepts tensors with floating point datatype only.
torch.ceil()
torch.ceil(x)
returns a tensor with the elements of the original tensor x
, rounded up to the nearest integer. The ceil()
function does not make changes to the original tensor, it creates a new tensor to store the result and returns it. To make changes to the original tensor, you should call ceil_()
function.
ceil()
and ceil_()
can also be called directly on a tensor.
Example
x = torch.tensor([[-1.1, 4.7], [-2.9, 1.24]])
# Does not make changes to tensor x, stores the result in y
y = torch.ceil(x)
print(y)
# Outputs- tensor([[-1., 5.], [-2., 2.]])
# ceil() function is called directly on the tensor
print(x.ceil())
# Outputs- tensor([[-1., 5.], [-2., 2.]])
# Changes the original tensor
x.ceil_()
print(x)
# Outputs- tensor([[-1., 5.], [-2., 2.]])
Note– The ceil()
and ceil_()
functions accepts tensors with floating point datatype only.
torch.floor()
torch.floor(x)
returns a tensor with the elements of the original tensor x
, rounded down to the nearest integer. The floor()
function does not make changes to the original tensor, it creates a new tensor to store the result and returns it. To make changes to the original tensor, you should call floor_()
function.
floor()
and floor_()
can also be called directly on a tensor.
Example
x = torch.tensor([[-1.1, 4.7], [-2.9, 1.24]])
# Does not make changes to tensor x, stores the result in y
y = torch.floor(x)
print(y)
# Outputs- tensor([[-2., 4.], [-3., 1.]])
# floor() function is called directly on the tensor
print(x.floor())
# Outputs- tensor([[-2., 4.], [-3., 1.]])
# Changes the original tensor
x.floor_()
print(x)
# Outputs- tensor([[-2., 4.], [-3., 1.]])
Note– The floor()
and floor_()
functions accepts tensors with floating point datatype only.
torch.sin()
torch.sin(x)
returns a tensor whose elements are the sine of the elements(in radians) of the original tensor x
. The sin()
function does not make changes to the original tensor, it creates a new tensor to store the result and returns it. To make changes to the original tensor, you should call sin_()
function.
sin()
and sin_()
can also be called directly on a tensor.
Example
x = torch.tensor([1.0, 2.0, 3.0])
# Does not make changes to tensor x, stores the result in y
y = torch.sin(x)
print(y)
# Outputs- tensor([0.8415, 0.9093, 0.1411])
# sin() function is called directly on the tensor
print(x.sin())
# Outputs- tensor([0.8415, 0.9093, 0.1411])
# Changes the original tensor
x.sin_()
print(x)
# Outputs- tensor([0.8415, 0.9093, 0.1411])
Note– The sin()
and sin_()
functions take the input in radians.
Note– The sin()
and sin_()
functions accepts tensors with floating point datatype only.
torch.cos()
torch.cos(x)
returns a tensor whose elements are the cosine of the elements(in radians) of the original tensor x
. The cos()
function does not make changes to the original tensor, it creates a new tensor to store the result and returns it. To make changes to the original tensor, you should call cos_()
function.
cos()
and cos_()
can also be called directly on a tensor.
Example
x = torch.tensor([1.0, 2.0, 3.0])
# Does not make changes to tensor x, stores the result in y
y = torch.cos(x)
print(y)
# Outputs- tensor([0.5403, -0.4161, -0.9900])
# cos() function is called directly on the tensor
print(x.cos())
# Outputs- tensor([0.5403, -0.4161, -0.9900])
# Changes the original tensor
x.cos_()
print(x)
# Outputs- tensor([0.5403, -0.4161, -0.9900])
Note– The cos()
and cos_()
functions take the input in radians.
Note– The cos()
and cos_()
functions accepts tensors with floating point datatype only.
torch.sqrt()
torch.sqrt(x)
returns a tensor whose elements are the square root of the elements in the original tensor x
. The sqrt()
function does not make changes to the original tensor, it creates a new tensor to store the result and returns it. To make changes to the original tensor, you should call sqrt_()
function.
sqrt()
and sqrt_()
can also be called directly on a tensor.
Example
x = torch.tensor([2., 3., 4., 5.])
# Does not make changes to tensor x, stores the result in y
y = torch.sqrt(x)
print(y)
# Outputs- tensor([1.4142, 1.7321, 2.0000, 2.2361])
# sqrt() function is called directly on the tensor
print(x.sqrt())
# Outputs- tensor([1.4142, 1.7321, 2.0000, 2.2361])
# Changes the original tensor
x.sqrt_()
print(x)
# Outputs- tensor([1.4142, 1.7321, 2.0000, 2.2361])
Note– The sqrt()
and sqrt_()
function accept tensors with floating point datatype only.
torch.log()
torch.log(x)
returns a tensor whose elements are the natural logarithm(to the base e) of the elements in the original tensor x
. The log()
function does not make changes to the original tensor, it creates a new tensor to store the result and returns it. To make changes to the original tensor, you should call log_()
function.
log()
and log_()
can also be called directly on a tensor.
Example
x = torch.tensor([1.4, 2, 4.0])
# Does not make changes to tensor x, stores the result in y
y = torch.log(x)
print(y)
# Outputs- tensor([0.3365, 0.6931, 1.3863])
# log() function is called directly on the tensor
print(x.log())
# Outputs- tensor([0.5403, -0.4161, -0.9900])
# Changes the original tensor
x.log_()
print(x)
# Outputs- tensor([0.5403, -0.4161, -0.9900])
Note– The log()
and log_()
function accept tensors with floating point datatype only.
Math Reference
For a complete reference of pytorch math functions, refer to the Pytorch Math Functions Reference.