How Does Torch Transpose Work?

This easy-to-understand Pytorch code shows you how torch transpose works using the torch.transpose(input, dim0, dim1) function.

Objective: How Does Torch Transpose Work?

This PyTorch code example will show you how to transpose torch tensor using the torch.transpose(input, dim0, dim1) function. 

How To Transpose Torch Tensor?

You can transpose a Torch tensor using the torch.transpose() function. This function takes three arguments-

  • input (matrix value)- The tensor to transpose.

  • If the input is a strided tensor, then altering the content of one will alter the content of the other, as the resulting out tensor shares the same underlying storage as the original tensor.

  • The resulting out tensor does not share the underlying storage with the original tensor if the input is a sparse tensor.

  • Dim0 and dim1 must be batch or sparse dimensions if the input is a sparse tensor with a compressed layout (SparseCSR, SparseBSR, etc.). For a sparse tensor, the dimensions that come before the sparse dimensions are its batch dimensions.

  • dim0 (integer value)- The first dimension to transpose.

  • dim1 (integer value)- The second dimension to transpose.

If you do not specify dim0 and dim1, the function will transpose the first two dimensions of the tensor.

You can also use the t() method to transpose a tensor. This method is equivalent to calling torch.transpose() with the first two dimensions as arguments.

When To Use Torch Transpose Tensor Function?

torch.transpose() is a useful function for various tasks, such as

  • Transposing images from the format used by PyTorch to the format used by other libraries, such as OpenCV.

  • Transposing tensors to prepare them for specific operations, such as matrix multiplication.

  • Transposing tensors to make them more readable or interpretable.

Steps Showing How To Find Transpose Of A Tensor

The following steps will show you how to transpose a torch tensor using the ‘torch.transpose ()’ function.

Step 1 - Import Library

First, you must import the required libraries.

import torch

Get Closer To Your Dream of Becoming a Data Scientist with Solved End-to-End PyTorch Projects

Step 2 - Take Sample Tensor

The next step is to take any sample tensor. Here, we will take a random 4x5 sample tensor as our original matrix.

torch_tensor = torch.randn(4, 5)

print("This is the Sample data:","\n",torch_tensor)

The output of the above code is-

This is the Sample data: 

 tensor([[-0.9121,  0.3992, -0.2460, -0.6212,  0.1731],

        [ 0.5473,  2.5027, -0.6945,  0.9575, -0.3117],

        [ 0.6560,  1.4413,  0.4640,  1.2929, -0.1153],

        [-0.7414,  0.7231, -1.5851, -0.5660, -0.1068]])

Step 3 - Apply Torch Tensor ‘Transpose()’

The final step is to change the order of dimensions in the sample tensor using the torch.transpose() function and print the transposed tensor. We will take the sample tensor as the input tensor, the first dimension as 1, and the second dimension as 0.

print("This is the transposed data:","\n",torch.transpose(torch_tensor, 1, 0))

The output of the above code is the following new tensor-

This is the transposed data: 

 tensor([[-0.9121,  0.5473,  0.6560, -0.7414],

        [ 0.3992,  2.5027,  1.4413,  0.7231],

        [-0.2460, -0.6945,  0.4640, -1.5851],

        [-0.6212,  0.9575,  1.2929, -0.5660],

        [ 0.1731, -0.3117, -0.1153, -0.1068]])

Torch Transpose Multiple Dimensions

Transposing multiple dimensions in PyTorch is a common operation, especially when working with multi-dimensional data. There are a few different ways to transpose multiple dimensions in PyTorch, but the most common way is to use the transpose() method.

To transpose multiple dimensions, you must pass a list of dimensions to the transpose() method. For example, to transpose a 3D tensor along the first and third dimensions, you would do the following-

import torch

tensor = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

transposed_tensor = torch.transpose(tensor, [0, 2])

print(transposed_tensor)

In the above code, we have used the PyTorch transpose() function, taking a sample 3x3 tensor as the input tensor, with the first dimension as 0 and the second dimension as 2.

The output of the above code is the following transposed tensor-

tensor([[1, 4, 7],

       [2, 5, 8],

       [3, 6, 9]])

You can also use the transpose() method to transpose a subset of dimensions. For example, to transpose a 4D tensor along the second and third dimensions, you would do the following-

import torch

tensor = torch.tensor([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

transposed_tensor = torch.transpose(tensor, [1, 2])

print(transposed_tensor)

In the above code, we have used the PyTorch transpose() function, taking a sample 4x3 tensor as the input tensor, with the first dimension as 1 and the second dimension as 2.

The output of the above code is the following transposed tensor-

tensor([[[1, 7],

       [2, 8],

       [3, 9]],

      [[4, 10],

       [5, 11],

       [6, 12]]])

Learn More About The Torch Transpose PyTorch Function With ProjectPro

This step-by-step PyTorch code example gives you a solid grasp of how to use the Torch Transpose function in PyTorch to manipulate the shape and structure of tensors efficiently. We have explored the essential steps for transposing tensors, including multiple dimensions scenarios, and discussed when to employ this function in data science and machine learning workflows. Furthermore, if you want to expand your proficiency in PyTorch and apply it to real-world data science and machine learning solutions, you must explore the ProjectPro platform. By engaging with over 270 end-to-end solved projects in the ProjectPro repository, you can gain the skills and expertise needed to excel in data science and machine learning.

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.