How to Shuffle Columns or Rows of Matrix in PyTorch?
In this article, we will see how to shuffle columns and rows of a matrix in PyTorch.
Column Shuffling:
Row and Column index starts with 0 so by specifying column indices in the order, we will shuffle columns. Here we will change the column positions.
Syntax: t1[torch.tensor([row_indices])][:,torch.tensor([column_indices])]
where,
- row_indices and column_indices are the index positions in which they are shuffled based on the positions.
- t1 represents tensor which of 2 dimensional.
Example 1:
In this example, we are creating a tensor named t1, which is of 2 dimensions of 3 rows, and 3 columns are created. After that, we are shuffling columns in such a way that we are moving column elements from the first position to the third position and the third position to the first position.
Python3
import torch
t1 = torch.tensor([[ 1 , 2 , 3 ],
[ 5 , 6 , 7 ],
[ 9 , 10 , 11 ]])
print (t1)
print ()
print (t1[torch.tensor([ 0 , 1 , 2 ])][:, torch.tensor([ 2 , 1 , 0 ])])
|
Output:
tensor([[ 1, 2, 3],
[ 5, 6, 7],
[ 9, 10, 11]])
tensor([[ 3, 2, 1],
[ 7, 6, 5],
[11, 10, 9]])
Example 2:
In this example, we are creating a tensor named t1, which is of 2 dimensions of 3 rows and 3 columns. After that we are shuffling columns in such a way that second position elements are moved to the third position, third position elements are moved to a first position and first position elements are moved to the second position.
Python3
import torch
t1 = torch.tensor([[ 1 , 2 , 3 ],
[ 5 , 6 , 7 ],
[ 9 , 10 , 11 ]])
print (t1)
print ()
print (t1[torch.tensor([ 0 , 1 , 2 ])][:, torch.tensor([ 1 , 2 , 0 ])])
|
Output:
tensor([[ 1, 2, 3],
[ 5, 6, 7],
[ 9, 10, 11]])
tensor([[ 2, 3, 1],
[ 6, 7, 5],
[10, 11, 9]])
Row Shuffling:
Row and Column index starts with 0 so by specifying column indices in the order, we will shuffle columns. Here we will change the row positions.
Syntax:t1[torch.tensor([row_indices])][:,torch.tensor([column_indices])]
where,
- row_indices and column_indices are the index positions in which they are shuffled based on the positions.
- t1 represents tensor which of 2 dimensional.
Example 1:
In this example, we are creating a tensor named t1, which is of 2 dimensions of 3 rows and 3 columns. After that, we are shuffling rows from the first position to the third position and from the third position to the first position.
Python3
import torch
t1 = torch.tensor([[ 1 , 2 , 3 ],
[ 5 , 6 , 7 ],
[ 9 , 10 , 11 ]])
print (t1)
print ()
print (t1[torch.tensor([ 2 , 1 , 0 ])][:, torch.tensor([ 0 , 1 , 2 ])])
|
Output:
tensor([[ 1, 2, 3],
[ 5, 6, 7],
[ 9, 10, 11]])
tensor([[ 9, 10, 11],
[ 5, 6, 7],
[ 1, 2, 3]])
Example 2:
In this example, we are creating a tensor named t1, which is of 2 dimensions of 3 rows and 3 columns. After that we are shuffling the rows in such a way that second position elements are moved to the third position, third position elements are moved to the first position and first position elements are moved to the second position.
Python3
import torch
t1 = torch.tensor([[ 1 , 2 , 3 ],
[ 5 , 6 , 7 ],
[ 9 , 10 , 11 ]])
print (t1)
print ()
print (t1[torch.tensor([ 1 , 2 , 0 ])][:, torch.tensor([ 0 , 1 , 2 ])])
|
Output:
tensor([[ 1, 2, 3],
[ 5, 6, 7],
[ 9, 10, 11]])
tensor([[ 5, 6, 7],
[ 9, 10, 11],
[ 1, 2, 3]])
Last Updated :
03 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...