Skip to content

Commit ab39eae

Browse files
authored
feat(pytorch): Add complete term entry for PyTorch Tensor .real() ope… (#7897)
* feat(pytorch): Add complete term entry for PyTorch Tensor .real() operation (#7853) Signed-off-by: Raja Rathour <[email protected]> * Enhance .real() method documentation Updated documentation for the .real() method in PyTorch to include title, description, subjects, tags, and examples. * Update content/pytorch/concepts/tensor-operations/terms/real/real.md Co-authored-by: Radhika-okhade <[email protected]> * Update content/pytorch/concepts/tensor-operations/terms/real/real.md Co-authored-by: Radhika-okhade <[email protected]> * Update content/pytorch/concepts/tensor-operations/terms/real/real.md Co-authored-by: Radhika-okhade <[email protected]> * Update content/pytorch/concepts/tensor-operations/terms/real/real.md Co-authored-by: Radhika-okhade <[email protected]> * fixed lint and format ---------
1 parent 5ec94a3 commit ab39eae

File tree

1 file changed

+83
-0
lines changed
  • content/pytorch/concepts/tensor-operations/terms/real

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
Title: '.real()'
3+
Description: 'Returns the real part of each element in a complex tensor in PyTorch.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Functions'
9+
- 'PyTorch'
10+
- 'Tensor'
11+
CatalogContent:
12+
- 'intro-to-py-torch-and-neural-networks'
13+
- 'paths/data-science'
14+
---
15+
16+
The **`.real()`** method in PyTorch returns a view of the real part of a complex tensor. It provides access to the real component of each element while sharing the same underlying memory as the original complex tensor. Any modification to this real view directly changes the original tensor’s data.
17+
18+
This method is available only for tensors with complex data types such as `torch.complex64` or `torch.complex128`.
19+
20+
## Syntax
21+
22+
```pseudo
23+
torch.real(input)
24+
```
25+
26+
**Parameters:**
27+
28+
- `input` (Tensor): A complex-valued input tensor.
29+
30+
**Return value:**
31+
32+
Returns a tensor containing only the real components of the original complex tensor. The returned tensor shares memory with the original tensor and has the corresponding real data type (e.g., `torch.float32` for `torch.complex64`).
33+
34+
## Example
35+
36+
This example demonstrates how to access and modify the real part of a complex tensor using `.real()`:
37+
38+
```py
39+
import torch
40+
41+
# Create a 2x2 complex tensor
42+
complex_tensor = torch.tensor([
43+
[1 + 2j, 3 + 4j],
44+
[5 + 6j, 7 + 8j]
45+
])
46+
47+
print("Original Tensor:")
48+
print(complex_tensor)
49+
50+
# Access the real view
51+
real_view = torch.real(complex_tensor)
52+
53+
print("\nReal Part View:")
54+
print(real_view)
55+
56+
# Modify a value in the real view
57+
real_view[0, 0] = 99.0
58+
59+
print("\nOriginal Tensor after modification:")
60+
print(complex_tensor)
61+
```
62+
63+
The output of this code is:
64+
65+
```shell
66+
Original Tensor:
67+
tensor([[1.+2.j, 3.+4.j],
68+
[5.+6.j, 7.+8.j]])
69+
70+
Real Part View:
71+
tensor([[1., 3.],
72+
[5., 7.]])
73+
74+
Original Tensor after modification:
75+
tensor([[99.+2.j, 3.+4.j],
76+
[ 5.+6.j, 7.+8.j]])
77+
```
78+
79+
Here,
80+
81+
- `.real()` returns a view, not a copy, of the real component of a complex tensor.
82+
- Any modification made through `.real()` affects the original complex tensor.
83+
- Applicable only to tensors with complex dtypes (`torch.complex64`, `torch.complex128`).

0 commit comments

Comments
 (0)