Python

Torch.ceil() and Torch.floor() Methods in PyTorch

We will discuss about the torch.ceil() and torch.floor() in this PyTorch tutorial.

PyTorch is an open-source framework available with a Python programming language. We can process the data in PyTorch in the form of a Tensor. Tensor is a multidimensional array that is used to store the data. To use a tensor, we have to import the torch module. To create a tensor, the method used is tensor().

Syntax:

torch.tensor(data)

Where the data is a multi-dimensional array.

Torch.ceil()

Torch.ceil() is used to return the ceil (top) value of the given decimal value. It can be float or double. It is applied on the tensor array such that the ceil() is applied to all the values present in the tensor.

Syntax:
torch.ceil(tensor_object)

Parameter:
It takes tensor_object as a parameter.

Return:
tensor_object containing ceiled values

Example 1:

Let’s create a one-dimensional tensor that has 5 values of the double type and return the ceil values.

#import torch module
import torch
 
#create a 1D tensor - data1 with 5 values
data1 = torch.tensor([34.67,89.65,89.21,41.89,0.88])
 
#display
print("Tensor: ",data1)
 
#perform ceil()
print("Ceil values: ",torch.ceil(data1))

Output:

Tensor:  tensor([34.6700, 89.6500, 89.2100, 41.8900,  0.8800])
Ceil values:  tensor([35., 90., 90., 42.,  1.])

We can see that the ceil(top) values were returned from the actual values in a tensor.

  1. 34.6700 is ceiled to 35.0
  2. 89.6500 is ceiled to 90.0
  3. 89.2100 is ceiled to 90.0
  4. 41.8900 is ceiled to 42.0
  5. 0.8800 is ceiled to 1.0

Example 2:

Let’s create a two-dimensional tensor that has 5 values of the double type in two rows and return the ceil values.

#import torch module
import torch
 
#create a 2D tensor - data1 with 5 values
data1 = torch.tensor([[34.67,89.65,89.21,41.89,0.88],[45.78,9.76,0.45,78.90,90.55]])
 
#display
print("Tensor: ",data1)
 
#perform ceil()
print("Ceil values: ",torch.ceil(data1))

Output:

Tensor:  tensor([[34.6700, 89.6500, 89.2100, 41.8900,  0.8800],
        [45.7800,  9.7600,  0.4500, 78.9000, 90.5500]])
Ceil values:  tensor([[35., 90., 90., 42.,  1.],
        [46., 10.,  1., 79., 91.]])

We can see that the ceil (top) values were returned from the actual values in a tensor.

  1. 34.6700 is ceiled to 35.0, 45.7800 is ceiled to 46.0
  2. 89.6500 is ceiled to 90.0, 9.7600 is ceiled to 10.0
  3. 89.2100 is ceiled to 90.0, 0.4500 is ceiled to 1.0
  4. 41.8900 is ceiled to 42.0, 78.9000 is ceiled to 79.0
  5. 0.8800 is ceiled to 1.0, 90.5500 is ceiled to 91.0

Torch.floor()

Torch.floor() is used to return the floor (below) value of the given decimal value. It can be float or double. It is applied on the tensor array such that the floor() is applied to all the values present in the tensor.

Syntax:
torch.floor(tensor_object)

Parameter:
It takes tensor_object as a parameter.

Return:
tensor_object containing floor values

Example 1:

Let’s create a one-dimensional tensor that has 5 values of the double type and return the floor values.

#import torch module
import torch
 
#create a 1D tensor - data1 with 5 values
data1 = torch.tensor([34.67,89.65,89.21,41.89,0.88])
 
#display
print("Tensor: ",data1)
 
#perform floor()
print("Floor values: ",torch.floor(data1))

Output:

Tensor:  tensor([34.6700, 89.6500, 89.2100, 41.8900,  0.8800])
Floor values:  tensor([34., 89., 89., 41.,  0.])

We can see that the floor (bottom) values were returned from the actual values in a tensor.

  • 34.6700 floor value is 34.0
  • 89.6500 floor value is 89.0
  • 89.2100 floor value is 89.0
  • 41.8900 floor value is 41.0
  • 0.8800 floor value is 0.0

Example 2:

Let’s create a two-dimensional tensor that has 5 values of the double type in two rows and return the floor values.

#import torch module
import torch
 
#create a 2D tensor - data1 with 5 values
data1 = torch.tensor([[34.67,89.65,89.21,41.89,0.88],[45.78,9.76,0.45,78.90,90.55]])
 
#display
print("Tensor: ",data1)
 
#perform floor()
print("Floor values: ",torch.floor(data1))

Output:

Tensor:  tensor([[34.6700, 89.6500, 89.2100, 41.8900,  0.8800],
        [45.7800,  9.7600,  0.4500, 78.9000, 90.5500]])
Floor values:  tensor([[34., 89., 89., 41.,  0.],
        [45.,  9.,  0., 78., 90.]])

We can see that the floor (below) values were returned from the actual values in a tensor.

  1. 34.6700 floor value is 34.0, 45.7800 floor value is 45.0
  2. 89.6500 floor value is 89.0, 9.7600 floor value is 9.0
  3. 89.2100 floor value is 89.0, 0.4500 floor value is 0.0
  4. 41.8900 floor value is 41.0, 78.9000 floor value is 78.0
  5. 0.8800 floor value is 0.0, 90.5500 floor value is 90.0

Conclusion

In this PyTorch lesson, we learned about the torch.ceil() and torch.floor() methods applied on the tensor. The objects.torch.ceil() is used to return the ceil (top) value of the given double value and the orch.floor() is used to return the floor (below) value of the given double value.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain