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:
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:
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:
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:
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:
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.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:
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:
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:
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:
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:
Step 5: Display Narrowed Tensor and Its Size
Lastly, display the elements of the narrowed tensor and its size:
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.