PyTorch torch.trunc() method returns a new tensor with the truncated integer values of the elements of input/ after removing the decimal portion of the number.
Syntax:
torch.trunc(input, out=None)Arguments
- input: This is input tensor.
- out: The output tensor.
Return: It returns a Tensor.
Let’s see this concept with the help of few examples:
Example 1:
Example 1:
# Importing the PyTorch library import torch # A constant tensor of size n a = torch.randn(6) print(a) # Applying the trunc function and # storing the result in 'out' out = torch.trunc(a) print(out) |
chevron_right
filter_none
Output:
1.1257 0.4493 -0.7309 1.5523 -0.2877 0.1155 [torch.FloatTensor of size 6] 1 0 -0 1 -0 0 [torch.FloatTensor of size 6]
Example 2:
# Importing the PyTorch library import torch # A constant tensor of size n a = torch.FloatTensor([1.5, 3.9, -6.9, 3.678]) print(a) # Applying the trunc function and # storing the result in 'out' out = torch.trunc(a) print(out) |
chevron_right
filter_none
Output:
1.5000 3.9000 -6.9000 3.6780 [torch.FloatTensor of size 4] 1 3 -6 3 [torch.FloatTensor of size 4]
Recommended Posts:
- Python | PyTorch tan() method
- Python Pytorch ones() method
- Python | PyTorch cos() method
- Python - PyTorch div() method
- Python | PyTorch sin() method
- Python Pytorch eye() method
- Python - PyTorch log() method
- Python - PyTorch exp() method
- Python - PyTorch abs() method
- Python - PyTorch add() method
- Python | PyTorch tanh() method
- Python | PyTorch cosh() method
- Python - PyTorch frac() method
- Python - PyTorch is_storage() method
- Python | PyTorch sinh() method
- Python - PyTorch clamp() method
- Python - PyTorch ceil() method
- Python - PyTorch numel() method
- Python - PyTorch is_storage() method
- Python - PyTorch is_tensor() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

