Rate this Page

torch.Tensor.unfold#

Tensor.unfold(dimension,size,step)Tensor#

Returns a view of the original tensor which contains all slices of sizesize fromself tensor in the dimensiondimension.

Step between two slices is given bystep.

Ifsizedim is the size of dimensiondimension forself, the size ofdimensiondimension in the returned tensor will be(sizedim - size) / step + 1.

An additional dimension of sizesize is appended in the returned tensor.

Parameters
  • dimension (int) – dimension in which unfolding happens

  • size (int) – the size of each slice that is unfolded

  • step (int) – the step between each slice

Example:

>>>x=torch.arange(1.,8)>>>xtensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.])>>>x.unfold(0,2,1)tensor([[ 1.,  2.],        [ 2.,  3.],        [ 3.,  4.],        [ 4.,  5.],        [ 5.,  6.],        [ 6.,  7.]])>>>x.unfold(0,2,2)tensor([[ 1.,  2.],        [ 3.,  4.],        [ 5.,  6.]])