-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(pytorch): Add complete term entry for PyTorch Tensor .real() ope… #7897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mamtawardhani
merged 11 commits into
Codecademy:main
from
Raja-89:docs/pytorch-tensor-real-7853
Oct 30, 2025
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2732b4c
feat(pytorch): Add complete term entry for PyTorch Tensor .real() ope…
Raja-89 77582eb
Merge branch 'main' into docs/pytorch-tensor-real-7853
Raja-89 e212cab
Merge branch 'main' into docs/pytorch-tensor-real-7853
Raja-89 880d56e
Enhance .real() method documentation
mamtawardhani e0bdf79
Merge branch 'main' into docs/pytorch-tensor-real-7853
Raja-89 de3afb1
Update content/pytorch/concepts/tensor-operations/terms/real/real.md
Raja-89 2fb5c3a
Update content/pytorch/concepts/tensor-operations/terms/real/real.md
Raja-89 154cc68
Update content/pytorch/concepts/tensor-operations/terms/real/real.md
Raja-89 132c9b2
Update content/pytorch/concepts/tensor-operations/terms/real/real.md
Raja-89 c90c50d
fixed lint and format
mamtawardhani ea28d0a
Merge branch 'main' into docs/pytorch-tensor-real-7853
mamtawardhani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
content/pytorch/concepts/tensor-operations/terms/real/real.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| --- | ||
| Title: '.real()' | ||
| Description: 'Returns the real part of each element in a complex tensor in PyTorch.' | ||
| Subjects: | ||
| - 'AI' | ||
| - 'Computer Science' | ||
| - 'Data Science' | ||
| Tags: | ||
| - 'AI' | ||
| - 'Functions' | ||
| - 'Pytorch' | ||
| - 'Trigonometry' | ||
Raja-89 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| CatalogContent: | ||
| - 'intro-to-py-torch-and-neural-networks' | ||
| - 'paths/data-science' | ||
| --- | ||
|
|
||
| The **`.real()`** 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. | ||
Raja-89 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| This method is available only for tensors with complex data types such as `torch.complex64` or `torch.complex128`. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ```pseudo | ||
| torch.real(input) | ||
| ``` | ||
|
|
||
| **Parameters:** | ||
|
|
||
| - `input` (Tensor): A complex-valued input tensor. | ||
|
|
||
| **Return value:** | ||
|
|
||
| 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`). | ||
|
|
||
| ## Example | ||
|
|
||
| This example demonstrates how to access and modify the real part of a complex tensor using `.real()`: | ||
|
|
||
| ```py | ||
| import torch | ||
|
|
||
| # Create a 2x2 complex tensor | ||
| complex_tensor = torch.tensor([ | ||
| [1 + 2j, 3 + 4j], | ||
| [5 + 6j, 7 + 8j] | ||
| ]) | ||
|
|
||
| print("Original Tensor:") | ||
| print(complex_tensor) | ||
|
|
||
| # Access the real view | ||
| real_view = torch.real(complex_tensor) | ||
|
|
||
| print("\nReal Part View:") | ||
| print(real_view) | ||
|
|
||
| # Modify a value in the real view | ||
| real_view[0, 0] = 99.0 | ||
|
|
||
| print("\nOriginal Tensor after modification:") | ||
| print(complex_tensor) | ||
| ``` | ||
|
|
||
| The output of this code is: | ||
|
|
||
| ```shell | ||
| Original Tensor: | ||
| tensor([[1.+2.j, 3.+4.j], | ||
| [5.+6.j, 7.+8.j]]) | ||
|
|
||
| Real Part View: | ||
| tensor([[1., 3.], | ||
| [5., 7.]]) | ||
|
|
||
| Original Tensor after modification: | ||
| tensor([[99.+2.j, 3.+4.j], | ||
| [ 5.+6.j, 7.+8.j]]) | ||
| ``` | ||
|
|
||
| ## Key Takeaways | ||
Raja-89 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| - `.real()` returns a view, not a copy, of the real component of a complex tensor. | ||
| - Any modification made through `.real()` affects the original complex tensor. | ||
| - Applicable only to tensors with complex dtypes (`torch.complex64`, `torch.complex128`). | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.