permute()

This is used to reorder the tensor using row and column

Syntax: tensor.permute(a,b,c)

where

  • tensor is the input tensor
  • permute(1,2,0) is used to permute the tensor by row
  • permute(2,1,0) is used to permute the tensor by column

Example: In this example, we are going to permute the tensor first by row and by column.

Python3




# import module
import torch
  
# create a tensor with 2 data
# in 3 three elements each
data = torch.tensor([[[10, 20, 30], 
                      [45, 67, 89]]])
  
# display
print(data)
  
# permute the tensor first by row
print(data.permute(1, 2, 0))
  
# permute the tensor first by column
print(data.permute(2, 1, 0))


Output:

tensor([[[10, 20, 30],
         [45, 67, 89]]])
tensor([[[10],
         [20],
         [30]],

        [[45],
         [67],
         [89]]])
tensor([[[10],
         [45]],

        [[20],
         [67]],

        [[30],
         [89]]])

Tensor Operations in PyTorch

In this article, we will discuss tensor operations in PyTorch.

PyTorch is a scientific package used to perform operations on the given data like tensor in python. A Tensor is a collection of data like a numpy array. We can create a tensor using the tensor function:

Syntax: torch.tensor([[[element1,element2,.,element n],……,[element1,element2,.,element n]]])

where,

  • torch is the module
  • tensor is the function
  • elements are the data

The Operations in PyTorch that are applied on tensor are:

Similar Reads

expand()

This operation is used to expand the tensor into a number of tensors, a number of rows in tensors, and a number of columns in tensors....

permute()

...

tolist()

This is used to reorder the tensor using row and column...

narrow()

...

where()

This method is used to return a list or nested list from the given tensor....