.abs()
Published Mar 8, 2025
Contribute to Docs
The .abs()
method in PyTorch computes the absolute value of each tensor element. For real numbers, it returns the non-negative value. For complex numbers, it calculates the magnitude using √(real² + imag²). This method is useful in data preprocessing, signal processing, and mathematical transformations.
Syntax
torch.abs(input)
input
(Tensor): The input tensor (can be real or complex).
The .abs()
method returns a tensor where each element is the absolute value of the matching element in the input tensor.
Example
This example shows how to use the .abs()
method to compute absolute values for both real and complex tensors:
import torch# Define a tensor with real and complex numberstensor = torch.tensor([[-3.0, 2.5], [1 - 2j, 3 + 4j]])# Compute absolute valuesabs_tensor = tensor.abs()print("Original Tensor:")print(tensor)print("\nAbsolute Values:")print(abs_tensor)
This example results in the following output:
Original Tensor:tensor([[-3.0000+0.j, 2.5000+0.j],[1.0000-2.j, 3.0000+4.j]])Absolute Values:tensor([[3.0000, 2.5000],[2.2361, 5.0000]])
In this example:
- Real numbers: -3.0 becomes 3.0, 2.5 remains 2.5.
- Complex numbers: 1 - 2j → √(1² + (-2)²) ≈ 2.2361; 3 + 4j → √(3² + 4²) = 5.0
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Free course
Intro to PyTorch and Neural Networks
Learn how to use PyTorch to build, train, and test artificial neural networks in this course.Intermediate3 hours