I always make my neural network and deep learning stuffs using numpy from scratch ( this keep my mind always usefull ) and off couse for me, better for debug. After heavly use Tensor Flow and discover Pytorch I just love. First because 95% off my models ( actually not my but a implementation of many papers ) has been done from scratch ( and make my head explode many times ), see a framework make some things easy for you it’s just like win in your birthday a box of cold beer from your girlfriend ( if you drink offcourse ).

So, when I start, first problem that I have was generate rolling windows ( or slide window if you prefer) just using pytorch (not with numpy), just with a simple line or couple of stride tricks but after read the docs I see how this was easy and pratical:

# import torch
import torch

def pytorch_rolling_window(x, window_size, step_size=1):
    # unfold dimension to make our rolling window
    return x.unfold(0,window_size,step_size)

# make a range sequence sample
x = torch.range(1,20)

# ie. window size of 5, step size of 1
print(pytorch_rolling_window(x,5,1))

  1     2     3     4     5
  2     3     4     5     6
  3     4     5     6     7
  4     5     6     7     8
  5     6     7     8     9
  6     7     8     9    10
  7     8     9    10    11
  8     9    10    11    12
  9    10    11    12    13
 10    11    12    13    14
 11    12    13    14    15
 12    13    14    15    16
 13    14    15    16    17
 14    15    16    17    18
 15    16    17    18    19
 16    17    18    19    20
[torch.FloatTensor of size 16x5]

That’s it ;)