From 2732b4c6a28c3e074f1ebce186f9e9e09ec2c5c5 Mon Sep 17 00:00:00 2001 From: Raja Rathour Date: Wed, 29 Oct 2025 13:23:54 +0530 Subject: [PATCH 1/7] feat(pytorch): Add complete term entry for PyTorch Tensor .real() operation (#7853) Signed-off-by: Raja Rathour --- .../tensor-operations/terms/real/real.md | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 content/pytorch/concepts/tensor-operations/terms/real/real.md diff --git a/content/pytorch/concepts/tensor-operations/terms/real/real.md b/content/pytorch/concepts/tensor-operations/terms/real/real.md new file mode 100644 index 00000000000..c771d6954df --- /dev/null +++ b/content/pytorch/concepts/tensor-operations/terms/real/real.md @@ -0,0 +1,71 @@ +

.real()

+ +

+The .real attribute returns a view of the real part of a complex PyTorch tensor. +

+ +

Introduction

+

+In PyTorch, tensors can hold complex numbers, meaning they have both a real component and an imaginary component. +The .real attribute is used to access only the real component of every element within a complex-valued tensor. +

+ +

+The resulting tensor shares the same underlying memory storage as the original complex tensor. +This means that modifying the values in the real view will directly change the values in the original complex tensor. +

+ +

+The attribute is only available when the tensor's data type is a complex type (e.g., torch.complex64 or torch.complex128). +

+ +
+ +

Syntax

+ +
tensor_name.real
+
+ +

+Where tensor_name must be a complex PyTorch tensor. +The returned tensor has the same shape as the original, but its dtype will be the corresponding real type +(e.g., torch.float32 if the original was torch.complex64). +

+ +
+ +

Example

+ +
import torch
+
+# 1. Create a 2x2 complex tensor
+# The values are (Real + Imaginary*i)
+complex_tensor = torch.tensor([
+    [1 + 2j, 3 + 4j],
+    [5 + 6j, 7 + 8j]
+])
+
+print("Original Tensor:")
+print(complex_tensor)
+
+# 2. Access the real view
+real_view = complex_tensor.real
+
+print("\nReal Part View:")
+print(real_view)
+
+# 3. Modify a value in the real view
+real_view[0, 0] = 99.0
+
+print("\nOriginal Tensor after modification:")
+print(complex_tensor)
+
+ +
+ +

Key Takeaways

+ From 880d56e468fc3efc819396963d2562b0387602fa Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 30 Oct 2025 10:33:55 +0530 Subject: [PATCH 2/7] Enhance .real() method documentation Updated documentation for the .real() method in PyTorch to include title, description, subjects, tags, and examples. --- .../tensor-operations/terms/real/real.md | 96 +++++++++++-------- 1 file changed, 55 insertions(+), 41 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/real/real.md b/content/pytorch/concepts/tensor-operations/terms/real/real.md index c771d6954df..59319fe3cf5 100644 --- a/content/pytorch/concepts/tensor-operations/terms/real/real.md +++ b/content/pytorch/concepts/tensor-operations/terms/real/real.md @@ -1,45 +1,46 @@ -

.real()

+--- +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' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'paths/data-science' +--- -

-The .real attribute returns a view of the real part of a complex PyTorch tensor. -

+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. -

Introduction

-

-In PyTorch, tensors can hold complex numbers, meaning they have both a real component and an imaginary component. -The .real attribute is used to access only the real component of every element within a complex-valued tensor. -

+This method is available only for tensors with complex data types such as `torch.complex64` or `torch.complex128`. -

-The resulting tensor shares the same underlying memory storage as the original complex tensor. -This means that modifying the values in the real view will directly change the values in the original complex tensor. -

+## Syntax -

-The attribute is only available when the tensor's data type is a complex type (e.g., torch.complex64 or torch.complex128). -

+```pseudo +torch.real(input) +``` -
+**Parameters:** -

Syntax

+- `input` (Tensor): A complex-valued input tensor. -
tensor_name.real
-
+**Return value:** -

-Where tensor_name must be a complex PyTorch tensor. -The returned tensor has the same shape as the original, but its dtype will be the corresponding real type -(e.g., torch.float32 if the original was torch.complex64). -

+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 -

Example

+This example demonstrates how to access and modify the real part of a complex tensor using `.real()`: -
import torch
+```py
+import torch
 
-# 1. Create a 2x2 complex tensor
-# The values are (Real + Imaginary*i)
+# Create a 2x2 complex tensor
 complex_tensor = torch.tensor([
     [1 + 2j, 3 + 4j],
     [5 + 6j, 7 + 8j]
@@ -48,24 +49,37 @@ complex_tensor = torch.tensor([
 print("Original Tensor:")
 print(complex_tensor)
 
-# 2. Access the real view
-real_view = complex_tensor.real
+# Access the real view
+real_view = torch.real(complex_tensor)
 
 print("\nReal Part View:")
 print(real_view)
 
-# 3. Modify a value in the 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: -

Key Takeaways

-
    -
  • .real returns a view — not a copy — of the real part of a complex tensor.
  • -
  • Any modification to .real directly affects the original tensor.
  • -
  • Only works for tensors with complex data types (torch.complex64, torch.complex128).
  • -
+```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 + +- `.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`). From de3afb1c55798fd34030ad080ef07160a3f3cfb6 Mon Sep 17 00:00:00 2001 From: Raja Rathour <193507075+Raja-89@users.noreply.github.com> Date: Thu, 30 Oct 2025 11:41:50 +0530 Subject: [PATCH 3/7] Update content/pytorch/concepts/tensor-operations/terms/real/real.md Co-authored-by: Radhika-okhade --- content/pytorch/concepts/tensor-operations/terms/real/real.md | 1 - 1 file changed, 1 deletion(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/real/real.md b/content/pytorch/concepts/tensor-operations/terms/real/real.md index 59319fe3cf5..ff7559404a4 100644 --- a/content/pytorch/concepts/tensor-operations/terms/real/real.md +++ b/content/pytorch/concepts/tensor-operations/terms/real/real.md @@ -2,7 +2,6 @@ Title: '.real()' Description: 'Returns the real part of each element in a complex tensor in PyTorch.' Subjects: - - 'AI' - 'Computer Science' - 'Data Science' Tags: From 2fb5c3aecf1a804ae3796c40ed3b2af3ad3b43e9 Mon Sep 17 00:00:00 2001 From: Raja Rathour <193507075+Raja-89@users.noreply.github.com> Date: Thu, 30 Oct 2025 11:41:59 +0530 Subject: [PATCH 4/7] Update content/pytorch/concepts/tensor-operations/terms/real/real.md Co-authored-by: Radhika-okhade --- .../pytorch/concepts/tensor-operations/terms/real/real.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/real/real.md b/content/pytorch/concepts/tensor-operations/terms/real/real.md index ff7559404a4..06385ab57aa 100644 --- a/content/pytorch/concepts/tensor-operations/terms/real/real.md +++ b/content/pytorch/concepts/tensor-operations/terms/real/real.md @@ -5,10 +5,9 @@ Subjects: - 'Computer Science' - 'Data Science' Tags: - - 'AI' - 'Functions' - - 'Pytorch' - - 'Trigonometry' + - 'PyTorch' + - 'Tensor' CatalogContent: - 'intro-to-py-torch-and-neural-networks' - 'paths/data-science' From 154cc68c40e76b7183af3e9a6d715d72cbb45bb2 Mon Sep 17 00:00:00 2001 From: Raja Rathour <193507075+Raja-89@users.noreply.github.com> Date: Thu, 30 Oct 2025 11:44:35 +0530 Subject: [PATCH 5/7] Update content/pytorch/concepts/tensor-operations/terms/real/real.md Co-authored-by: Radhika-okhade --- content/pytorch/concepts/tensor-operations/terms/real/real.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/real/real.md b/content/pytorch/concepts/tensor-operations/terms/real/real.md index 06385ab57aa..6a9c2fe4f1e 100644 --- a/content/pytorch/concepts/tensor-operations/terms/real/real.md +++ b/content/pytorch/concepts/tensor-operations/terms/real/real.md @@ -13,7 +13,7 @@ CatalogContent: - '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. +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. This method is available only for tensors with complex data types such as `torch.complex64` or `torch.complex128`. From 132c9b2ed8afd06fa741f3530f9a00257bc041c9 Mon Sep 17 00:00:00 2001 From: Raja Rathour <193507075+Raja-89@users.noreply.github.com> Date: Thu, 30 Oct 2025 11:44:55 +0530 Subject: [PATCH 6/7] Update content/pytorch/concepts/tensor-operations/terms/real/real.md Co-authored-by: Radhika-okhade --- content/pytorch/concepts/tensor-operations/terms/real/real.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/real/real.md b/content/pytorch/concepts/tensor-operations/terms/real/real.md index 6a9c2fe4f1e..a06ec903550 100644 --- a/content/pytorch/concepts/tensor-operations/terms/real/real.md +++ b/content/pytorch/concepts/tensor-operations/terms/real/real.md @@ -76,7 +76,7 @@ tensor([[99.+2.j, 3.+4.j], [ 5.+6.j, 7.+8.j]]) ``` -## Key Takeaways +Here, - `.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. From c90c50d9f67fe4428b3d932b6e790f5d45b91b31 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 30 Oct 2025 16:32:11 +0530 Subject: [PATCH 7/7] fixed lint and format --- content/pytorch/concepts/tensor-operations/terms/real/real.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/real/real.md b/content/pytorch/concepts/tensor-operations/terms/real/real.md index a06ec903550..2dd5e203215 100644 --- a/content/pytorch/concepts/tensor-operations/terms/real/real.md +++ b/content/pytorch/concepts/tensor-operations/terms/real/real.md @@ -4,11 +4,11 @@ Description: 'Returns the real part of each element in a complex tensor in PyTor Subjects: - 'Computer Science' - 'Data Science' -Tags: +Tags: - 'Functions' - 'PyTorch' - 'Tensor' -CatalogContent: +CatalogContent: - 'intro-to-py-torch-and-neural-networks' - 'paths/data-science' ---