How to make the window size a setting (Forecast, version 3)?

Howard Pong Yuen Lam · AI Notes · November 13, 2026 · 4 min read

English 繁體中文 简体中文

This is version 3 of the forecasting series. Version 2 cut a series into windows with three numbers typed straight into the call: unfold(0, 5, 1). Version 3 moves the cutting into a function, so the window size becomes a setting you can change in one obvious place.

Why bother with a function?

A function is a named piece of code you can call again and again. Putting the cutting into one has two payoffs. First, the three arguments of unfold are now given names of your own choosing — data, window_size, shift — which say what they mean. Second, a longer series becomes easy to try: change one line, run the program, and the whole output changes with it.

Later versions will call this same function from inside a training loop, so the tidying done here pays off more than once.

How the program works

1. Define the function

import torch
def create_sliding_windows(data, window_size, shift):
    windows = data.unfold(0, window_size, shift)
    return windows

The def line names the function create_sliding_windows and lists what it needs: the series to cut, the length of each piece, and how far to slide each time. Inside, unfold receives those three values instead of the fixed 0, 5, 1 from version 2. The last line hands the result back, so whoever called the function can use it.

Defining a function does not run it. Nothing happens until the function is called lower down the file.

2. Make a longer series

data = torch.arange(1, 61)
window_size = 30

torch.arange(1, 61) builds the whole numbers from 1 up to 60. The 61 is the stopping point and is not included by Python. So the series holds 60 values — two months of daily sales. The second line states the window length once, in a variable whose name says exactly what it is.

3. Call the function

windows = create_sliding_windows(data, window_size, 1)

This is the line that actually does the work. The series, the window length and the step are passed in, and the pieces come back in windows. If you want a window of 10 instead, change window_size above and run the file again; nothing else in the program needs to move.

4. Show each window

for window in windows:
    print(window.numpy())

The loop is unchanged from version 2: take the pieces one at a time and print each as a plain array. What has changed is how many lines come out. With 60 numbers and a window of 30, there are 60 − 30 + 1 = 31 possible starting positions, so 31 windows are printed, as shown below.

What you should see

Thirty-one windows, each holding 30 numbers. The first two and the last one look like this:

[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 25 26 27 28 29 30]
[ 2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 26 27 28 29 30 31]
...
[31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 55 56 57 58 59 60]

Each window starts one number later than the one before it, and the whole series has been swept from left to right. The last window ends on 60, the final value in the series, which confirms that the sweep did reach the end.

The whole program

The pieces above, put back together. This is the complete file, 9 lines in all:

import torch
def create_sliding_windows(data, window_size, shift):
    windows = data.unfold(0, window_size, shift)
    return windows
data = torch.arange(1, 61)
window_size = 30
windows = create_sliding_windows(data, window_size, 1)
for window in windows:
    print(window.numpy())

Running it on your own computer

Download the zip file and unzip it to a folder. The zip holds the programs in this series so far, the time_series.csv data file, and pyproject.toml. This note uses time_03.py. Open the folder in Visual Studio Code, then type these three lines in the terminal:

uv python pin 3.12.10
uv sync
uv run time_03.py

The first line records which Python version this folder uses, and the second installs every package that pyproject.toml lists. The third line prints the 31 windows in the terminal.

Those three lines are needed only once. The next time you want to run the program again, type just uv run time_03.py.

If you have not set up uv yet, start with How to install Visual Studio Code? and then the uv note for Windows or Mac.

What comes next

The windows are still just blocks of numbers. Nothing in the program says which numbers are the given part and which is the answer. Version 4 splits every window into features and targets, and feeds them to PyTorch as a dataset the model can learn from.