PyTorch .floor()
Anonymous contributor
Published Aug 27, 2025
In PyTorch, the .floor() function rounds each element of the input tensor down to the nearest integer, including negative numbers.
Syntax
torch.floor(input, *, out=None)
Parameters:
input: The input tensor whose elements will be rounded down.out(optional): A tensor to store the output. Must have the same shape as input.
Return value:
Returns a new tensor with each element rounded down to the nearest integer. If out is provided, the result is written there instead.
Example 1: Basic Rounding Down with .floor()
The following example demonstrates the usage of the .floor() function:
import torch# Define inputinput = torch.randn(4)print(input)# Round down to the nearest integeroutput = torch.floor(input)print(output)
A sample output of this code is:
tensor([-0.0032, -0.0333, -0.2857, -1.3986])tensor([-1., -1., -1., -2.])
Example 2: Using the out Parameter
This example shows how to use the optional out argument to store the results in a pre-allocated tensor:
import torch# Define inputinput = torch.tensor([1.7, -0.2, 3.9])# Pre-allocate an output tensor with the same shaperesult = torch.empty_like(input)# Compute floor and store result in `result` tensortorch.floor(input, out=result)print(result)
The output of this code is:
tensor([ 1., -1., 3.])
All contributors
- Anonymous contributor
Learn PyTorch on Codecademy
- Learn to build machine learning models with Python.
- Includes 10 Courses
- With Certificate
- Beginner Friendly.23 hours
- Learn the basics of the world's fastest growing and most popular programming language used by software engineers, analysts, data scientists, and machine learning engineers alike.
- Beginner Friendly.17 hours