pytorch中torch.narrow()函数

Posted qinduanyinghua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pytorch中torch.narrow()函数相关的知识,希望对你有一定的参考价值。

torch.narrow(inputdimstartlength) → Tensor

Returns a new tensor that is a narrowed version of input tensor. The dimension dim is input from start to start +length. The returned tensor and input tensor share the same underlying storage.

Parameters
  • input (Tensor) – the tensor to narrow

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

  • start (int) – the starting dimension

  • length (int) – the distance to the ending dimension

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]])

根据定义得知,这个函数是返回tensor的第dim维切片start: start+length的数, 针对例子,

x.size() = (3, 3)

torch.narrow(x, 0, 0, 2) == x[0:0+2, :]

torch.narrow(x, 1, 2, 1) == x[:, 2:2+1]

 

以上是关于pytorch中torch.narrow()函数的主要内容,如果未能解决你的问题,请参考以下文章

pytorch 笔记: 复现论文 Stochastic Weight Completion for Road Networks using Graph Convolutional Networks(代

Pytorch解决使用BucketIterator.splits警告volatile was removed and now has no effect. Use `with torch.no_g(代

Pytorch解决使用BucketIterator.splits警告volatile was removed and now has no effect. Use `with torch.no_g(代

pytorch中gather函数的理解。

Pytorch 文档中的 Autograd 函数

pytorch中序数多分类的损失函数