.narrow_copy()
In Pytorch, .narrow_copy()
is a function that creates a new tensor containing the same data as the input tensor but with a narrowed dimension. Unlike .narrow()
, which returns a view of the original tensor, .narrow_copy()
returns a new tensor.
Syntax
torch.narrow_copy(input, dim, start, length)
input
: The input tensor to be narrowed.dim
: The dimension along which the narrowing is applied.start
: The index where the narrowing begins. This can be a positive integer, a negative integer (to index from the end ofdim
), or a 0-dimensional integer tensor.length
: The number of elements to include from the starting position.
Example
The following example demonstrates the usage of the .narrow_copy()
function:
import torch# Create a 2D tensortensor_2d = torch.arange(1, 13).reshape(3, 4)print(f"Original 2D Tensor:\n{tensor_2d}")# Narrow the tensor along rows and create a new tensornarrowed_tensor = torch.narrow_copy(tensor_2d, 0, 1, 2)print(f"Narrowed 2D Tensor:\n{narrowed_tensor}")
The above code produces the following output:
Original 2D Tensor:tensor([[ 1, 2, 3, 4],[ 5, 6, 7, 8],[ 9, 10, 11, 12]])Narrowed 2D Tensor:tensor([[ 5, 6, 7, 8],[ 9, 10, 11, 12]])
In this example, the .narrow_copy()
function creates a new tensor by narrowing an input tensor along a specific dimension. In the given code, the input tensor is narrowed along rows (dimension 0), starting from index 1 and including 2 rows.
The .narrow_copy()
function is used to create a new tensor containing a subset of data from an existing tensor without modifying the original tensor.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Intro to PyTorch and Neural Networks
Learn how to use PyTorch to build, train, and test artificial neural networks in this course.Intermediate3 hours