Upload model.py with huggingface_hub
Browse files
model.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
A small, patch-based transformer for zero-shot time series forecasting —
|
| 3 |
+
a toy-scale version of the same general recipe used by real time-series
|
| 4 |
+
foundation models (Chronos, TimesFM, t0-alpha, etc.):
|
| 5 |
+
|
| 6 |
+
1. Per-series normalization: each input window is normalized by its
|
| 7 |
+
OWN mean and std before entering the model, and de-normalized
|
| 8 |
+
using those same stats afterward. This is the single most
|
| 9 |
+
important design choice for generalization — it means the model
|
| 10 |
+
learns SHAPE (trend, seasonality, noise structure), not absolute
|
| 11 |
+
numbers, so a model trained on synthetic series scaled 0.5-500 can
|
| 12 |
+
meaningfully forecast a real series scaled in the thousands.
|
| 13 |
+
|
| 14 |
+
2. Patching: rather than feeding every single timestep as a separate
|
| 15 |
+
token (expensive, and treats each point independently), the input
|
| 16 |
+
window is split into small chunks ("patches") and each patch is
|
| 17 |
+
projected into the model's embedding space as one token. This is
|
| 18 |
+
the same idea PatchTST and most modern time-series transformers
|
| 19 |
+
use — it's both more efficient and tends to generalize better
|
| 20 |
+
than point-wise tokenization.
|
| 21 |
+
|
| 22 |
+
3. A standard transformer encoder processes the patch tokens, and a
|
| 23 |
+
linear head maps the final representation directly to the
|
| 24 |
+
forecast horizon.
|
| 25 |
+
|
| 26 |
+
CONTEXT_LEN and HORIZON are fixed (96 and 42) to match the evaluation
|
| 27 |
+
setup already used for SARIMA/Prophet/statsforecast/t0-alpha in the
|
| 28 |
+
sibling demand-forecasting project, so results are directly comparable.
|
| 29 |
+
"""
|
| 30 |
+
import torch
|
| 31 |
+
import torch.nn as nn
|
| 32 |
+
|
| 33 |
+
CONTEXT_LEN = 96
|
| 34 |
+
HORIZON = 42
|
| 35 |
+
PATCH_SIZE = 8
|
| 36 |
+
N_PATCHES = CONTEXT_LEN // PATCH_SIZE # 12
|
| 37 |
+
D_MODEL = 32
|
| 38 |
+
N_HEADS = 4
|
| 39 |
+
N_LAYERS = 2
|
| 40 |
+
DIM_FEEDFORWARD = 64
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class TinyForecaster(nn.Module):
|
| 44 |
+
def __init__(self):
|
| 45 |
+
super().__init__()
|
| 46 |
+
self.patch_embed = nn.Linear(PATCH_SIZE, D_MODEL)
|
| 47 |
+
self.pos_embed = nn.Parameter(torch.randn(1, N_PATCHES, D_MODEL) * 0.02)
|
| 48 |
+
|
| 49 |
+
encoder_layer = nn.TransformerEncoderLayer(
|
| 50 |
+
d_model=D_MODEL, nhead=N_HEADS, dim_feedforward=DIM_FEEDFORWARD,
|
| 51 |
+
batch_first=True, dropout=0.1,
|
| 52 |
+
)
|
| 53 |
+
self.encoder = nn.TransformerEncoder(encoder_layer, num_layers=N_LAYERS)
|
| 54 |
+
|
| 55 |
+
self.head = nn.Sequential(
|
| 56 |
+
nn.Linear(D_MODEL * N_PATCHES, 128),
|
| 57 |
+
nn.ReLU(),
|
| 58 |
+
nn.Linear(128, HORIZON),
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
def forward(self, context_raw: torch.Tensor) -> torch.Tensor:
|
| 62 |
+
"""
|
| 63 |
+
context_raw: (batch, CONTEXT_LEN) raw (unnormalized) values.
|
| 64 |
+
Returns: (batch, HORIZON) forecast, in the ORIGINAL scale.
|
| 65 |
+
"""
|
| 66 |
+
mean = context_raw.mean(dim=1, keepdim=True)
|
| 67 |
+
std = context_raw.std(dim=1, keepdim=True).clamp(min=1e-3)
|
| 68 |
+
normed = (context_raw - mean) / std
|
| 69 |
+
|
| 70 |
+
patches = normed.view(-1, N_PATCHES, PATCH_SIZE)
|
| 71 |
+
tokens = self.patch_embed(patches) + self.pos_embed
|
| 72 |
+
encoded = self.encoder(tokens)
|
| 73 |
+
flat = encoded.reshape(encoded.size(0), -1)
|
| 74 |
+
forecast_normed = self.head(flat)
|
| 75 |
+
|
| 76 |
+
forecast = forecast_normed * std + mean
|
| 77 |
+
return forecast
|