Introduction to the basic usage of torch.narrow()

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

  • From the inputtensor returns a tensor to limit the scope of the range limits is: along a dimension dimfrom startthe start+lengthrange of the interval, similar to the array slice usage, the tensor returned inputto share the same storage base tensor

parameter

  • input(Tensor) , The tensor to be processed;
  • dim(int), Along the axis of restriction;
  • start(int) , The starting point of the tensor;
  • length(int) , Narrow the length;

Examples are as follows:

rand_float = torch.randn((5,3))# 随机生成 5*3数据
rand_float
>>>
tensor([[-0.4972, -0.1363, -1.8918],
        [ 1.2994, -1.0091,  0.1862],
        [ 0.5525,  1.3073,  1.3741],
        [-1.7242, -0.3593, -0.7546],
        [-0.3328,  0.3333,  0.0096]])
        
rand_float.narrow(0,1,2)# 沿第一维度开始,第一行为开始,长度为2
>>>
tensor([[ 1.2994, -1.0091,  0.1862],
        [ 0.5525,  1.3073,  1.3741]])

Guess you like

Origin blog.csdn.net/weixin_42512684/article/details/110789511