.ceil()
Published Jun 9, 2025
Contribute to Docs
In PyTorch, the .ceil()
function returns a new tensor with the smallest integer greater than or equal to each element of the input tensor. This operation is commonly used in applications that require rounding values up, such as setting upper bounds in optimization problems, discretizing continuous outputs, or preparing pixel-based image data where fractional values are invalid.
Syntax
torch.ceil(input,*, out=None)
Parameters:
input
: The input tensor whose elements are to be rounded up.out
(optional): A tensor to store the output. Must have the same shape asinput
.
Return value:
The .ceil()
returns a new tensor containing the ceiling values of each element in the input
tensor. Unless the out
parameter is specified, the result is a new tensor.
Example: Applying .ceil()
to a 1D Tensor
import torch# Create a tensorx = torch.tensor([1.2, -0.8, 3.0, -2.7, 5.5])# Apply the ceil operationy = torch.ceil(x)print(f"Original tensor: {x}")print(f"Ceil tensor: {y}")
This program gives the following output:
Original tensor: tensor([ 1.2000, -0.8000, 3.0000, -2.7000, 5.5000])Ceil tensor: tensor([ 2., -0., 3., -2., 6.])
Note: The result may display
-0.
due to floating-point formatting, but it is functionally equivalent to0.
.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Intro to PyTorch and Neural Networks
Learn how to use PyTorch to build, train, and test artificial neural networks in this course.Intermediate3 hours