Before you start
Train an LSTM that forecasts the next value of a seasonal series.
You get one training series, a 1D tensor. It has a daily and a weekly cycle
on top of a level near 50, plus noise. Write
train_forecaster(series, window). It must:
window values, each with the value that follows
it as the target (Tensor.unfold does this in one call),nn.LSTM with a linear head on the last hidden state,
using mini-batches from a DataLoader,predict(windows) function. It takes a (B, window) tensor of
raw values and returns a (B,) tensor of forecasts in raw units.def train_forecaster(series, window):
Input
series = torch.tensor([50.1, 54.3, 57.9, ...]) # 1000 values
predict = train_forecaster(series, window=24)
predict(torch.stack([series[-24:]]))
Output
tensor([53.8]) # one forecast for the step after the window
Reasoning
The forecast for a window is the model output for its last hidden state,
turned back into raw units with * std + mean. The cycles repeat, so the
shape of the last 24 values tells the model where in each cycle the next
step falls.
1.0 and
below 20% of the "repeat the last value" baseline, which is about 8.7.nn.LSTM(1, hidden, batch_first=True) expects (B, T, 1). Add the
feature axis with unsqueeze(-1).torch.no_grad() and keep training to a few
seconds.Run it in Colab
Google Colab has PyTorch and a free GPU, and needs nothing installed. Take a code, open the notebook, and paste the code into its first cell. Grading happens there and the result comes back here.
In Colab, the grading cell is your Submit. It runs the hidden cases there, and the result shows here on its own. This one is scored on a threshold, not on exact values.
⇧↵run cell