Skip to content

Commit bd562d0

Browse files
Add entry for abs tensor operation
1 parent d1cfbd1 commit bd562d0

File tree

1 file changed

+29
-2
lines changed
  • content/pytorch/concepts/tensor-operations/terms/abs

1 file changed

+29
-2
lines changed

content/pytorch/concepts/tensor-operations/terms/abs/abs.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Title: '.abs()'
3-
Description: 'Computes the adjoint of a 2D complex-valued tensor in PyTorch.'
3+
Description: 'Computes the absolute value of each element in a PyTorch tensor. For complex numbers, returns their magnitude.'
44
Subjects:
55
- 'Computer Science'
66
- 'Data Science'
@@ -13,22 +13,49 @@ CatalogContent:
1313
- 'paths/data-science'
1414
---
1515

16-
The **`.abs()`** method in PyTorch
16+
The **`.abs()`** method in PyTorch calculates the absolute value (magnitude) of each element in a tensor. For real numbers, this returns their non-negative value. For complex numbers, it computes the magnitude using the formula √(real² + imag²). This operation is widely used in data preprocessing, signal processing, and mathematical transformations.
1717

1818
## Syntax
1919

2020
```pseudo
2121
tensor.abs()
2222
```
2323

24+
The `.abs()` method returns a tensor where each element is the absolute value of the corresponding element in the input tensor.
25+
2426
## Example
2527

28+
This example shows how to use .abs() to compute absolute values for both real and complex tensors:
29+
2630
```py
2731
import torch
2832

33+
# Define a tensor with real and complex numbers
34+
tensor = torch.tensor([[-3.0, 2.5], [1 - 2j, 3 + 4j]])
35+
36+
# Compute absolute values
37+
abs_tensor = tensor.abs()
38+
39+
print("Original Tensor:")
40+
print(tensor)
2941

42+
print("\nAbsolute Values:")
43+
print(abs_tensor)
3044
```
3145

3246
This example results in the following output:
3347

48+
```shell
49+
Original Tensor:
50+
tensor([[-3.0000+0.j, 2.5000+0.j],
51+
[1.0000-2.j, 3.0000+4.j]])
52+
53+
Absolute Values:
54+
tensor([[3.0000, 2.5000],
55+
[2.2361, 5.0000]])
56+
```
57+
3458
In this example:
59+
60+
- **Real numbers**: -3.0 becomes 3.0, 2.5 remains 2.5.
61+
- **Complex numbers**: 1 - 2j → √(1² + (-2)²) ≈ 2.2361; 3 + 4j → √(3² + 4²) = 5.0

0 commit comments

Comments
 (0)