Artificial Intelligence is the process of teaching the machine based on the provided data to make predictions about future events. PyTorch framework is used to optimize deep learning models and the tensors are used to store the data that is going to be used in teaching these models. The models are trained on the given data to find the hidden patterns that are not visible to the naked eye and give better predictions.

Table of Content

This guide explains the following sections:

How to Shuffle PyTorch Tensor

PyTorch offers multiple methods of shuffling the tensors like row, column, and random shuffles of the matrix which is a multidimensional structure. The platform also enables the user to shuffle the tensors and come back to the original form if the data structure is important. The numpy library can also be used to call the shuffle() method to change the order of values of the PyTorch tensor.

To learn how to shuffle PyTorch tensors, go through the following guide:

Note: The Python code can be accessed from the Colab Notebook:


Getting started with the process requires some prerequisites to be completed as mentioned below:

  • Access Python Notebook
  • Install Modules
  • Import Libraries

Access Python Notebook

Open the notebook for writing the Python code by clicking on the “New Notebook” from the Google Colaboratory website. The user can write the code in any of the notebooks like Jupyter and others as well:

Install Modules

On the colab notebook, install the numpy and torch modules for getting their dependencies and libraries to shuffle the PyTorch tensors. These modules can be downloaded using the pip command that manages all the Python modules:

pip install numpy
pip install torch

Import Libraries

Import the torch library to use its methods for creating and shuffling the tensors using the import keyword:

import torch

Verify the installation of the torch and its library by displaying its version on the screen using the following code:


Once all the requirements are met start implementing the shuffling of the PyTorch tensor with the help of the following examples:

Example 1: Columns Shuffle in PyTorch Tensor

This example creates the multi-dimensional tensor and shuffles its columns by changing the indexes of the tensors:

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


print(t[torch.tensor([0, 1, 2])][:, torch.tensor([1, 2, 0])])

The “t” variable contains a tensor with three columns and three rows, and the print() function is used to display the values on the screen. Use the name of the tensor with a couple of tensor() methods as the first one specifies the original order of the columns, and the second one gives the shuffled order. Wrap these methods in the print() method to display the shuffled tensor on the screen as well:

Example 2: Row Shuffle in PyTorch Tensor

This example shows the process of shuffling the rows in multidimensional tensors:

ten = torch.tensor([[1., 2., 3.],[4., 5., 6.],[7, 8, 9]])
print("Original Tensor:", ten)

r = torch.tensor([1, 0, 2])
c = torch.tensor([0, 1, 2])
shuffle=ten[r[:, None], c]
print("Row Shuffled Tensor:", shuffle)

Design the “ten” variable which is initialized with the multi-dimensional tensor and print the original values on the screen. Define two variables containing the structure for rows and columns as they can be manipulated to shuffle the structure of rows or columns. Another variable containing a shuffled tensor is defined as the “shuffle” which uses the indexing process to rearrange the rows in the tensor. The None keyword is used to add another dimension to the tensor and is required for performing the indexing to shuffle the tensor:

Example 3: Random Shuffle in PyTorch Tensor

The third example shuffles the tensor randomly without giving the order of the rows and columns:

print("Original Tensor:",ten)

idx = torch.randperm(ten.nelement())
row_shuffled = ten.view(-1)[idx].view(ten.size())
print("\nShuffled Tensor:",row_shuffled)

The above code shuffles the values randomly without keeping the order of its structure. It creates a 2D tensor and returns the values stored in it. The randperm() method refers to the random permutations of values from 0 to the number of elements using the nelement() method. The ten.view() method changes the structure of the tensor to 1D and then applies shuffling to it before changing it back to a 2D structure:

Example 4: Shuffle PyTorch Tensor & Back to the Original

The PyTorch platform enables the user to shuffle the tensor and then get its original order as this example explains the process:

N, M, K = 2, 2, 5
x = torch.arange(N*M*K).view(N, M, K)

The above code creates two tensors with two rows and five columns each and returns their values:

The following code applies the randperm() method to get the shuffled tensor using the indexing process and displays the values on the screen:

idx = torch.randperm(x.size(1))
y = x[:, idx]

To get back the original tensor, the torch library provides the sort() method to return the sorted indices:

idx_inv = torch.sort(idx).indices
print(y[:, idx_inv])

Example 5: Using “numpy” to Shuffle PyTorch Tensor

Another library that can be used to shuffle the PyTorch tensors is numpy and importing its library is required to use the functions:

import numpy as np

Once the numpy is imported as the “np”, the user can simply use the np keyword to call the numpy library instead of using the complete name. The np.random.shuffle() method gives a random shuffle to the values of the tensor that is used in its argument with the numpy() method:

t = torch.arange(5)
print("\nOriginal Tensor:",t)
print("\nShuffled Tensor:",t)

Example 6: Using “arange()” Method to Shuffle PyTorch Tensor

The following code creates and returns a 2D tensor with 3 rows and 5 columns:

x = torch.rand(3, 5)

The “torch.argsort(torch.rand(*x.shape), dim=-1)” line creates a tensor with the same structure as the “x” containing random values. The “x[torch.arange(x.shape[0]).unsqueeze(-1), indices]” creates a tensor that selects the rows from the tensor x and indices rearranges the elements of each row:

indices = torch.argsort(torch.rand(*x.shape), dim=-1)
result = x[torch.arange(x.shape[0]).unsqueeze(-1), indices]

That’s all about the process of shuffling the PyTorch tensor.


To shuffle the PyTorch tensors, the framework offers a variety of functionalities like shuffling rows, columns, etc. The user can also shuffle the tensors and bring back the original structure of tensors using the sort() method with the name of the tensor. Another module that can be used to shuffle the PyTorch tensors is the numpy as it provides the random.shuffle() method. This blog has implemented all the methods of shuffling the PyTorch tensors using multiple examples.