Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute
menu
close

Need Help? Talk to an Expert

+91 8000107255
Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute

Need Help? Talk to an Expert

+91 8000107255

torch.ceil() Method in PyTorch

Home torch.ceil() Method in PyTorch
PyTorch torch.ceil() Method
  • Written by krunallathiya21
  • May 22, 2025
  • 0 Com
PyTorch

The torch.ceil() method calculates the ceiling of each element in a tensor, returning the smallest integer greater than or equal to the input value. For example, 1.1 becomes 2 and -1.9 becomes -1.

Visual representation of torch.ceil() method

It is an element-wise operation, and the output tensor contains the same shape and data type as the input tensor.

The mathematical formula for .ceil() function is this: outi​=⌊inputi​⌋

Function signature

torch.ceil(input, out=None)

Parameters

Argument Description
input (Tensor) It represents an input tensor.

It supports any floating-point or integer tensor type (e.g., torch.float32, torch.int32).

out (Tensor, optional) It is an output tensor to store the result of the ceiling values. If you have a pre-allocated tensor, you can store the ceiling values in this tensor using the “out” argument.

Finding the ceiling of a 1D tensor

import torch

input_tensor = torch.tensor([1.1, 3.0, 5.5, 7.9])

ceil_tensor = torch.ceil(input_tensor)

print(ceil_tensor)

# Output: tensor([2., 3., 6., 8.])

You can see that .ceil() rounds up to the nearest integer greater than or equal to that element.

Multi-Dimensional Tensor

torch.ceil() method with multi-dimensional tensor

Let’s define the 2D tensor and get the ceiling of each tensor element.

import torch

multi_tensor = torch.tensor([[11.1, 32.0],
                             [54.5, 9.9]])

ceil_multi_tensor = torch.ceil(multi_tensor)

print(ceil_multi_tensor)

# Output:
# tensor([[12., 32.],
#         [55., 10.]])

Negative valued tensor

tensor with negative values The same rules apply to the tensor that contains negative values as well.
import torch

multi_negative_tensor = torch.tensor([[-10.1, -29.0],
                                      [-44.9, -9.5]])

ceil_negative_tensor = torch.ceil(multi_negative_tensor)

print(ceil_negative_tensor)

# Output:
# tensor([[-10., -29.],
#         [-44.,  -9.]])

-10 is greater than -10.1. Same as -44 is greater than -44.9, and -9 is greater than -9.5.

Integer tensor

tensor with integer values

What if an input tensor is an integer? Well, in that case, it will return the same value as input because it is already the highest.

import torch

int_tensor = torch.tensor([11, -19, 21], dtype=torch.int32)

ceil_int_tensor = torch.ceil(int_tensor)

print(ceil_int_tensor)

# Output: tensor([ 11, -19,  21], dtype=torch.int32)

Pre-allocated output tensor

To create a pre-allocated tensor, you can use torch.empty() or torch.zeros() method.

We can save the output tensor of the .ceil() method to the pre_allocated_tensor using “out” argument of the method.

import torch

tensor = torch.tensor([1.9, 4.1])

pre_allocated_tensor = torch.empty(2)

torch.ceil(tensor, out=pre_allocated_tensor)

print(pre_allocated_tensor)

# Output: tensor([2., 5.])

In-Place modification

In PyTorch, any method that ends with an underscore (_) is an in-place variant of the corresponding function. So, the ceil() method has a variant called the tensor.ceil_() method for in-place modifications.

import torch

tensor = torch.tensor([2.1, 4.5, -1.9])

print(tensor.ceil_())

# Output: tensor([ 3.,  5., -1.])

The above output shows that we modified the original input tensor rather than returning a new tensor.

Handling edge cases

What if a tensor contains infinities or NaN values? Let’s find out.

import torch

infinity_nan_tensor = torch.tensor(
    [0.2, 5.0, float('inf'), float('-inf'), float('nan')])

result = torch.ceil(infinity_nan_tensor)

print(result)

# Output: tensor([1., 5., inf, -inf, nan])

The inf becomes inf, -inf becomes -inf, and nan becomes nan. That means it does not change these values.

That’s all!
Post Views: 9
LEAVE A COMMENT Cancel reply
Please Enter Your Comments *

krunallathiya21

All Categories
  • JavaScript
  • Python
  • PyTorch
site logo

Address:  TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

sprintchasetechnologies@gmail.com

(+91) 8000107255.

ABOUT US
  • About
  • Team Members
  • Testimonials
  • Contact

Copyright by @SprintChase  All Rights Reserved

  • PRIVACY
  • TERMS & CONDITIONS