# Python – PyTorch ceil() and floor() methods

PyTorchServer Side ProgrammingProgramming

A ceiling value of a number is the smallest integer greater than or equal to the number. To find the ceiling of the elements of a torch tensor, we use the torch.ceil() function. This function takes a torch tensor as input parameter and returns a torch tensor with the ceil values of each element of the input tensor. This function supports only real-valued inputs. It supports torch tensors of any dimension.

A floor value of a number is the largest integer less than or equal to the number. To find the floor of the elements of a torch tensor, we use the torch.floor() function. This function takes a torch tensor as input parameter and returns a torch tensor with the floor values of each element of the input tensor. This function supports only real-valued input and it can support torch tensors of any dimension.

## Syntax

torch.ceil(input)
torch.floor(input)

## Parameters

• input - It's the input tensor.

## Output

It returns a tensor of ceil or floor of elements of input tensor.

## Steps

You could use the following steps to find the tensor with ceil or floor of elements of an input tensor

• Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.

import torch
• Create a torch tensor and print the tensor.

T = torch.tensor([1, 2.3, .2, 0, 4.01, 4.5, 4.99])
print("Tensor:\n", T)
• Compute torch.ceil(input) or torch.floor(input) to find the tensor with ceil or floor of the elements of input and print the computed value

print("Tensor with ceil values:\n",torch.ceil(T))
print("Tensor with floor values:\n",torch.floor(T))

## Example 1

In the following Python code, we compute the ceiling and floor values of the elements of a torch tensor. We have taken all the elements of the tensor as positive numbers.

# Import the required library
import torch

# define a torch tensor
T = torch.tensor([1, 2.3, .2, 0, 4.01, 4.5, 4.99])
print("Tensor:\n", T)
print("Tensor with ceil values:\n",torch.ceil(T))
print("Tensor with floor values:\n",torch.floor(T))

## Output

Tensor:
tensor([1.0000, 2.3000, 0.2000, 0.0000, 4.0100, 4.5000, 4.9900])
Tensor with ceil values:
tensor([1., 3., 1., 0., 5., 5., 5.])
Tensor with floor values:
tensor([1., 2., 0., 0., 4., 4., 4.])

## Example 2

The following Python program shows how to find the ceiling and floor of the elements of a tensor. Some elements are negative. Notice how the ceiling and floor values of negative numbers are computed.

# Import the required library
import torch
T = torch.tensor([-1, -2.3, .2, 0, -4.01, 4.5, -4.99])
print("Tensor:\n", T)
print("Tensor with ceil values:\n",torch.ceil(T))
print("Tensor with floor values:\n",torch.floor(T))

## Output

Tensor:
tensor([-1.0000, -2.3000, 0.2000, 0.0000, -4.0100, 4.5000, -4.9900])
Tensor with ceil values:
tensor([-1., -2., 1., 0., -4., 5., -4.])
Tensor with floor values:
tensor([-1., -3., 0., 0., -5., 4., -5.])

## Example 3

In the following Python code, the input is a two-dimensional tensor. It computes the ceiling and floor of each element of the tensor

# Import the required library
import torch
T = torch.randn(4,3)
print("Tensor:\n", T)
print("Tensor with ceil values:\n",torch.ceil(T))
print("Tensor with floor values:\n",torch.floor(T))

## Output

Tensor:
tensor([[-0.4304, -0.5405, -0.7153],
[ 0.8230, -0.0368, -0.0357],
[-1.3842, 0.2168, -0.0332],
[ 0.3007, 0.2878, 0.1758]])
Tensor with ceil values:
tensor([[-0., -0., -0.],
[ 1., -0., -0.],
[-1., 1., -0.],
[ 1., 1., 1.]])
Tensor with floor values:
tensor([[-1., -1., -1.],
[ 0., -1., -1.],
[-2., 0., -1.],
[ 0., 0., 0.]])
Updated on 20-Jan-2022 07:18:48