Rate this Page

torch.narrow#

torch.narrow(input,dim,start,length)Tensor#

Returns a new tensor that is a narrowed version ofinput tensor. Thedimensiondim is input fromstart tostart+length. Thereturned tensor andinput tensor share the same underlying storage.

Parameters
  • input (Tensor) – the tensor to narrow

  • dim (int) – the dimension along which to narrow

  • start (int orTensor) – index of the element to start the narrowed dimensionfrom. Can be negative, which means indexing from the end ofdim. IfTensor, it must be an 0-dim integralTensor (bools not allowed)

  • length (int) – length of the narrowed dimension, must be weakly positive

Example:

>>>x=torch.tensor([[1,2,3],[4,5,6],[7,8,9]])>>>torch.narrow(x,0,0,2)tensor([[ 1,  2,  3],        [ 4,  5,  6]])>>>torch.narrow(x,1,1,2)tensor([[ 2,  3],        [ 5,  6],        [ 8,  9]])>>>torch.narrow(x,-1,torch.tensor(-1),1)tensor([[3],        [6],        [9]])