码迷,mamicode.com
首页 > 其他好文 > 详细

pytorch中torch.narrow()函数

时间:2019-11-14 22:06:39      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:turned   param   not   href   pre   sha   har   lis   utils   

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()函数

标签:turned   param   not   href   pre   sha   har   lis   utils   

原文地址:https://www.cnblogs.com/qinduanyinghua/p/11862641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!