From 1d4c4c529dead75f723d390b85b9c55f70de0e9e Mon Sep 17 00:00:00 2001 From: Avdhoot Fulsundar Date: Thu, 23 Jan 2025 21:08:08 +0530 Subject: [PATCH] Pushed file --- .../terms/transpose/transpose.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 content/pytorch/concepts/tensor-operations/terms/transpose/transpose.md diff --git a/content/pytorch/concepts/tensor-operations/terms/transpose/transpose.md b/content/pytorch/concepts/tensor-operations/terms/transpose/transpose.md new file mode 100644 index 00000000000..967755d9515 --- /dev/null +++ b/content/pytorch/concepts/tensor-operations/terms/transpose/transpose.md @@ -0,0 +1,48 @@ +--- +Title: '.transpose()' +Description: 'Reorders dimensions of a tensor by swapping two specified dimensions.' +Subjects: + - 'AI' + - 'Data Science' +Tags: + - 'AI' + - 'Arrays' + - 'Data Structures' + - 'Deep Learning' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'paths/computer-science' +--- + +In PyTorch, the `.transpose()` operation creates a view of the input tensor with the specified dimensions swapped, commonly used for matrix operations and reshaping data for neural network inputs. Unlike `permute()`, `transpose()` only swaps two dimensions at a time. + +## Syntax + +```pseudo +tensor.transpose(dim0, dim1) +``` + +- `dim0`: First dimension to be transposed +- `dim1`: Second dimension to be transposed + +## Example + +The following code creates a 2x3 tensor with 2 rows and 3 columns, then uses `transpose()` to swap row and column dimensions, resulting in a 3x2 tensor where original rows become columns and vice versa: + +```py +import torch + +# Create a 2x3 tensor and transpose its dimensions +x = torch.tensor([[1, 2, 3], + [4, 5, 6]]) +y = x.transpose(0, 1) +print(y) +``` + +The above code will result in the following output: + +```shell +tensor([[1, 4], + [2, 5], + [3, 6]]) +```