pytorch

How to Narrow Down Tensors in PyTorch?

In PyTorch, tensors are the essential data structures that represent multidimensional arrays of numbers. They can be utilized for various purposes, like storing inputs, outputs, weights, etc. However, sometimes users may need to narrow down specific tensors to select a subset of elements along a certain dimension. In this situation, PyTorch provides different methods to perform this operation.

This article will illustrate different methods to narrow down tensors in PyTorch.

Method 1: Narrow Down Tensors Using “torch.narrow()” Method in PyTorch

The basic syntax of the “torch.narrow()” method is given below:

torch.narrow(<input_tensor>, <dim>, <start>, <length>)

Here:

  • input_tensor” is the desired tensor that is required to narrow down.
  • dim” is the dimension along which the tensor will be narrowed down.
  • start” is the starting index along the specified dimension.
  • length” is the number of elements to include along the specified dimension.

To narrow down a specific tensor using the “torch.narrow()” method in PyTorch, follow the below-provided steps:

Step 1: Import PyTorch Library
First, import the “torch” library to narrow down a PyTorch tensor:

import torch

Step 2: Create a Tensor
Then, create a desired tensor using the “torch.tensor()” function and print its elements. Here, we are creating the following “tens” 2D tensor:

tens = torch.tensor([[1, 3, 5], [2, 4, 6], [10, 20, 30]])

print(tens)

This has created the tensor as seen below:

Step 3: View Tensor Size
Next, use the “size()” attribute to view the size of the above-created “tens” tensor:

print(tens.size())

The size/shape of the “tens” tensor is 3×3:

Step 4: Narrow Down Tensor
Now, narrow down the tensor using the “torch.narrow(<tensor>, <dim>, <start>, <length>)” method. It is required to specify the input tensor, specific dimension, starting dimension, and ending length. Here, we are narrowing the “tens” tensor with the following arguments:

nar_tens = torch.narrow(tens, 0, 1, 2)

Here:

  • tens” is the input tensor to narrow.
  • 0” is the dimension to narrow down the tensor along the first row.
  • 1” is the starting dimension to start the tensor from the second row.
  • 2” is the length which indicates that the 2 rows will be included from the original tensor:

Step 5: Display Narrowed Tensor and Its Size
Finally, display the elements of the narrowed tensor and its size:

print(nar_tens)

print(nar_tens.size())

According to the below output, the tensor has been narrowed successfully:

Similarly, users can also narrow down the tensor in other dimensions, such as “1”:

Method 2: Narrow Down Tensor Using “Tensor.narrow()” Method in PyTorch

The basic syntax of the “Tensor.narrow()” method is given below:

Tensor.narrow(<dim>, <start>, <length>)

Here, “Tensor” is the input tensor to narrow. The remaining arguments are the same as in the above method.

To narrow down a particular tensor using the “Tensor.narrow()” method in PyTorch, check out the below-listed steps:

Step 1: Import PyTorch Library
First, import the “torch” library to narrow down a tensor:

import torch

Step 2: Create a Tensor
Then, use the “torch.tensor()” function to create a desired tensor and print its elements. Here, we are creating the following “T1” 2D tensor:

T1 = torch.tensor([[2, 4, 6], [8, 10, 12], [14, 16, 18], [20, 22, 24]])

print(T1)

The below output shows that the ‘T1” tensor has been created successfully:

Step 3: View Tensor Size
Next, view the size of the above-created “T1” tensor using the “size()” attribute:

print(T1.size())

It can be observed that the size of the “T1” tensor is 4×3:

Step 4: Narrow Down Tensor
Now, use the “Tensor.narrow(<dim>, <start>, <length>)” method to narrow down the “T1” tensor. It is required to specify the input tensor, specific dimension, starting dimension, and ending length. Here, we are narrowing the “T1” tensor with the following arguments:

N_tens = T1.narrow(0, 1, 2)

Step 5: Display Narrowed Tensor and Its Size
Lastly, display the elements of the narrowed tensor and its size:

print(N_tens)

print(N_tens.size())

In the below output, the elements of the tensor and its size indicate that the tensor has been narrowed successfully:

Similarly, users can also narrow down the tensor in other dimensions and starting index as seen below:

We have efficiently explained the methods to narrow down tensors in PyTorch.

Note: You can access our Google Colab Notebook at this link.

Conclusion

To narrow down a tensor in PyTorch, first, import “torch” library. Then, create the desired tensor and view its elements and size. After that, use the “torch.narrow(<tensor>, <dim>, <start>, <length>)” or “Tensor.narrow(<dim>, <start>, <length>)” method to narrow down the input tensor. Lastly, display the elements of the narrowed tensor and its size. This article has illustrated different methods to narrow down tensors in PyTorch.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.