From 1ad53de7aeb12ab1e9eec187ce3ce7d44abf8d6d Mon Sep 17 00:00:00 2001 From: denis Date: Fri, 27 Dec 2024 13:00:00 +0200 Subject: [PATCH 1/3] [Term Entry] PyTorch Tensor Operations: .swapaxes() --- .../terms/swapaxes/swapaxes.md | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 content/pytorch/concepts/tensor-operations/terms/swapaxes/swapaxes.md diff --git a/content/pytorch/concepts/tensor-operations/terms/swapaxes/swapaxes.md b/content/pytorch/concepts/tensor-operations/terms/swapaxes/swapaxes.md new file mode 100644 index 00000000000..ddd29bbd7e0 --- /dev/null +++ b/content/pytorch/concepts/tensor-operations/terms/swapaxes/swapaxes.md @@ -0,0 +1,87 @@ +--- +Title: '.swapaxes()' +Description: 'Alias for torch.transpose(). This function is equivalent to NumPy’s swapaxes function.' +Subjects: + - 'Computer Science' + - 'Machine Learning' + - 'Data Science' +Tags: + - 'AI' + - 'Data Types' + - 'Deep Learning' + - 'Functions' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'paths/data-science' +--- + +In PyTorch, the **`.swapaxes()`** function swaps two specified axes (dimensions) of a tensor. + +## Syntax + +```pseudo +torch.swapaxes(input, axis0, axis1) +``` + +- `input`: The tensor input array. +- `axis0`: First axis of the tensor. +- `axis1`: Second axis of the tensor. + +## Example + +The following example demonstrates the usage of the `.swapaxes()` function: + +```py +import torch + +# Create a tensor +x = torch.tensor([[[0,1],[2,3]],[[4,5],[6,7]]]) + +# Swap the axes +torch.swapaxes(x, 0, 1) + +# Swap the axes +torch.swapaxes(x, 0, 2) +``` + +In the above example, the order the value of the tensor is: + +- Before the swap + ``` + tensor([ + [ + [0, 1], + [2, 3] + ], + [ + [4, 5], + [6, 7] + ] + ]) + ``` +- After the first swap + ``` + tensor([ + [ + [0, 1], + [4, 5] + ], + [ + [2, 3], + [6, 7] + ] + ]) + ``` +- After the second swap + ``` + tensor([ + [ + [0, 4], + [2, 6] + ], + [ + [1, 5], + [3, 7] + ] + ]) + ``` \ No newline at end of file From 40368d136f56d1a2e76e4eb7f155beb68058f4d6 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 30 Dec 2024 15:40:28 +0530 Subject: [PATCH 2/3] Update swapaxes.md minor fixes --- .../terms/swapaxes/swapaxes.md | 76 +++++++------------ 1 file changed, 28 insertions(+), 48 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/swapaxes/swapaxes.md b/content/pytorch/concepts/tensor-operations/terms/swapaxes/swapaxes.md index ddd29bbd7e0..b48735b60b7 100644 --- a/content/pytorch/concepts/tensor-operations/terms/swapaxes/swapaxes.md +++ b/content/pytorch/concepts/tensor-operations/terms/swapaxes/swapaxes.md @@ -1,6 +1,6 @@ --- Title: '.swapaxes()' -Description: 'Alias for torch.transpose(). This function is equivalent to NumPy’s swapaxes function.' +Description: 'Swaps two specified axes (dimensions) of a tensor, effectively rearranging its shape.' Subjects: - 'Computer Science' - 'Machine Learning' @@ -24,8 +24,8 @@ torch.swapaxes(input, axis0, axis1) ``` - `input`: The tensor input array. -- `axis0`: First axis of the tensor. -- `axis1`: Second axis of the tensor. +- `axis0`: The first axis to be swapped. +- `axis1`: The second axis to be swapped. ## Example @@ -35,53 +35,33 @@ The following example demonstrates the usage of the `.swapaxes()` function: import torch # Create a tensor -x = torch.tensor([[[0,1],[2,3]],[[4,5],[6,7]]]) +x = torch.tensor([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) -# Swap the axes -torch.swapaxes(x, 0, 1) +# Swap the axes (0 and 1) +swapped1 = torch.swapaxes(x, 0, 1) +print("After swapping axes 0 and 1:") +print(swapped1) -# Swap the axes -torch.swapaxes(x, 0, 2) +# Swap the axes (0 and 2) +swapped2 = torch.swapaxes(x, 0, 2) +print("\nAfter swapping axes 0 and 2:") +print(swapped2) ``` -In the above example, the order the value of the tensor is: +The output of the above code is as follows: -- Before the swap - ``` - tensor([ - [ - [0, 1], - [2, 3] - ], - [ - [4, 5], - [6, 7] - ] - ]) - ``` -- After the first swap - ``` - tensor([ - [ - [0, 1], - [4, 5] - ], - [ - [2, 3], - [6, 7] - ] - ]) - ``` -- After the second swap - ``` - tensor([ - [ - [0, 4], - [2, 6] - ], - [ - [1, 5], - [3, 7] - ] - ]) - ``` \ No newline at end of file +```shell +After swapping axes 0 and 1: +tensor([[[0, 1], + [4, 5]], + + [[2, 3], + [6, 7]]]) + +After swapping axes 0 and 2: +tensor([[[0, 4], + [2, 6]], + + [[1, 5], + [3, 7]]]) +``` From 462cd888c0cc9df298d79da83980c4c9e066bd6f Mon Sep 17 00:00:00 2001 From: Sriparno Roy <89148144+Sriparno08@users.noreply.github.com> Date: Sun, 12 Jan 2025 13:24:53 +0530 Subject: [PATCH 3/3] Minor changes --- .../concepts/tensor-operations/terms/swapaxes/swapaxes.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/swapaxes/swapaxes.md b/content/pytorch/concepts/tensor-operations/terms/swapaxes/swapaxes.md index b48735b60b7..ff495f2b4ea 100644 --- a/content/pytorch/concepts/tensor-operations/terms/swapaxes/swapaxes.md +++ b/content/pytorch/concepts/tensor-operations/terms/swapaxes/swapaxes.md @@ -4,7 +4,6 @@ Description: 'Swaps two specified axes (dimensions) of a tensor, effectively rea Subjects: - 'Computer Science' - 'Machine Learning' - - 'Data Science' Tags: - 'AI' - 'Data Types' @@ -23,7 +22,7 @@ In PyTorch, the **`.swapaxes()`** function swaps two specified axes (dimensions) torch.swapaxes(input, axis0, axis1) ``` -- `input`: The tensor input array. +- `input`: The input tensor. - `axis0`: The first axis to be swapped. - `axis1`: The second axis to be swapped.