PyTorch torch.abs() method computes the element-wise absolute value of the given input tensor.
Syntax:
torch.abs(inp, out=None) ? TensorArguments
- inp: This is input tensor.
- out: This is optional parameter and is the output tensor.
Return: It returns a Tensor having absolute value of input inp.
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 1 a = torch.FloatTensor([-15]) print(a) # Applying the abs function and # storing the result in 'b' b = torch.abs(a) print(b) |
chevron_right
filter_none
Output:
-15 [torch.FloatTensor of size 1] 15 [torch.FloatTensor of size 1]
Example 2:
# Importing the PyTorch library import torch # A constant tensor of size n a = torch.FloatTensor([15, -5, 3, -2]) print(a) # Applying the abs function and # storing the result in 'b' b = torch.abs(a) print(b) |
chevron_right
filter_none
Output:
15 -5 3 -2 [torch.FloatTensor of size 4] 15 5 3 2 [torch.FloatTensor of size 4]
Recommended Posts:
- Python - PyTorch log() method
- Python Pytorch eye() method
- Python - PyTorch exp() method
- Python - PyTorch add() method
- Python - PyTorch div() method
- Python | PyTorch cos() method
- Python Pytorch ones() method
- Python | PyTorch sin() method
- Python | PyTorch tan() method
- Python - PyTorch trunc() method
- Python | PyTorch atan() method
- Python - PyTorch is_storage() method
- Python - PyTorch is_storage() method
- Python - PyTorch is_tensor() method
- Python - PyTorch numel() method
- Python - PyTorch ceil() method
- Python - PyTorch clamp() method
- Python | PyTorch sinh() method
- Python | PyTorch asin() method
- Python | PyTorch cosh() 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.

