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

#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:**

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.

- 34.6700 is ceiled to 35.0
- 89.6500 is ceiled to 90.0
- 89.2100 is ceiled to 90.0
- 41.8900 is ceiled to 42.0
- 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

#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:**

[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.

- 34.6700 is ceiled to 35.0, 45.7800 is ceiled to 46.0
- 89.6500 is ceiled to 90.0, 9.7600 is ceiled to 10.0
- 89.2100 is ceiled to 90.0, 0.4500 is ceiled to 1.0
- 41.8900 is ceiled to 42.0, 78.9000 is ceiled to 79.0
- 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

#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:

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

#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:**

[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.

- 34.6700 floor value is 34.0, 45.7800 floor value is 45.0
- 89.6500 floor value is 89.0, 9.7600 floor value is 9.0
- 89.2100 floor value is 89.0, 0.4500 floor value is 0.0
- 41.8900 floor value is 41.0, 78.9000 floor value is 78.0
- 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.