PyTorch Sequence Data
A sequence is a list of values in time order. Sliding a fixed window along a sequence turns it into input and target pairs.
The wave as values
A sine wave gives smooth values that rise and fall. The first values climb from zero.
wave = torch.sin(torch.arange(200) * 0.1)
print([round(v, 3) for v in wave[:5].tolist()])One window by hand
The first window holds wave values 0 to 9. The target is value 10, the very next one.
print([round(v, 3) for v in wave[0:10].tolist()])
print(round(wave[10].item(), 3))Sliding along the wave
The loop slides the window one step at a time. Each stop adds one pair to the dataset.
width = 10
inputs = []
targets = []
for start in range(len(wave) - width):
inputs.append(wave[start:start + width])
targets.append(wave[start + width])Example
import torch
torch.manual_seed(0)
wave = torch.sin(torch.arange(200) * 0.1)
width = 10
inputs = []
targets = []
for start in range(len(wave) - width):
inputs.append(wave[start:start + width])
targets.append(wave[start + width])
x = torch.stack(inputs)
y = torch.stack(targets)
print("Wave values:", len(wave))
print("Windows:", x.shape[0])
print("Input shape:", tuple(x.shape))
print("Target shape:", tuple(y.shape))
first = [round(v, 3) for v in x[0].tolist()]
print("First window:", first)
print("First target:", round(y[0].item(), 3))Key points
- Time order: each value sits at one step, and the steps run in a fixed order
- Window: a short run of consecutive values used as one input
- Target: the single value that comes right after the window
- A window of width 10 turns 200 wave values into 190 pairs
- Each window starts one step later than the window before it
- The pair shape is one input row of width 10 and one target value
In this example, the wave holds 200 sine values spaced 0.1 apart. The loop slides a window of width 10 along the wave. Each window becomes one input row. The value right after the window becomes its target. Stacking gives 190 inputs with shape (190, 10) and 190 targets. The first window prints as the opening climb of the wave. Its target prints as 0.841, the next value on the climb. A model trained on these pairs learns to continue the wave by one step. The next lesson feeds such windows to a network that reads them step by step.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Python Playground
Exercise
In this windowing scheme, what is the target for the window wave[5:15]?