Below is a concise “menu” of today’s most popular foundation models for time-series / tabular forecasting, followed by two walk-throughs you can copy-paste to get started right away.
Which foundation model should I pick?
────────────────────────────────────────────────────────
(Use this decision table as a rule-of-thumb; pick the first row that matches your situation.)
Scenario / Need
Model Family (open-source)
Why it fits
Good libraries
🏬 Dozens–thousands of related time series, want probabilistic output
DeepAR-style autoregressive RNNs
Handles many series & seasonality; gives full predictive distribution
A. Temporal Fusion Transformer with PyTorch-Forecasting (open-source, GPU or CPU)
B. DeepAR with GluonTS (fast, probabilistic, also runs on CPU)
Feel free to skip whichever you don’t need.
────────────────
A) TFT in 20 lines
────────────────
bash
# 1. install once
pip install pytorch-lightning pytorch-forecasting torch==2.x
python
# 2. Imports
import pandas as pd, torch
from pytorch_forecasting import TimeSeriesDataSet, TemporalFusionTransformer
from pytorch_forecasting.data import GroupNormalizer
from pytorch_lightning import Trainer
# 3. Load or create a dataframe with at least:
# time_idx (int), value (float), group_id (str/int), and any covariates
df = pd.read_csv("my_series.csv")
# 4. Train/valid split
max_time = df.time_idx.max()
train_df = df[df.time_idx <= max_time - 24]
val_df = df[df.time_idx > max_time - 24]
# 5. Wrap in TimeSeriesDataSet
training = TimeSeriesDataSet(
train_df,
time_idx="time_idx",
target="value",
group_ids=["group_id"],
min_encoder_length=48, # look-back
max_encoder_length=48,
min_prediction_length=24, # forecast horizon
max_prediction_length=24,
time_varying_known_reals=["time_idx"],
time_varying_unknown_reals=["value"],
add_relative_time_idx=True,
target_normalizer=GroupNormalizer(),
)
# 6. Dataloaders
batch_size = 64
train_dl = training.to_dataloader(train=True, batch_size=batch_size)
val_dl = training.to_dataloader(train=False, batch_size=batch_size*2)
# 7. Build model
tft = TemporalFusionTransformer.from_dataset(
training,
learning_rate=2e-4,
hidden_size=32,
attention_head_size=4,
dropout=0.1,
)
# 8. Train
trainer = Trainer(max_epochs=20, accelerator="auto")
trainer.fit(tft, train_dl, val_dl)
# 9. Forecast
raw_preds, x = tft.predict(val_dl, mode="raw", return_x=True)
That’s it—raw_preds includes point forecasts and quantiles.
Behind the scenes you get feature importance, attention plots, etc. (tft.interpret_output(raw_preds, reduction="sum")).
────────────────
B) DeepAR in 15 lines
────────────────
bash
pip install gluonts mxnet numpy pandas
python
import pandas as pd, numpy as np, gluonts
from gluonts.dataset.common import ListDataset
from gluonts.mx.model.deepar import DeepAREstimator
from gluonts.mx.trainer import Trainer
# 1. Prepare a list-of-dicts dataset
def make_gluonts(ds):
return ListDataset(
[{"start": s.index[0], "target": s.values} for s in ds],
freq="D") # <-- change freq accordingly
series = pd.read_csv("my_series.csv", index_col="date", parse_dates=True)
train_ds = make_gluonts([series[:-30]])
test_ds = make_gluonts([series])
# 2. Build & train
estimator = DeepAREstimator(
freq="D",
prediction_length=30,
trainer=Trainer(epochs=50, learning_rate=1e-3, num_batches_per_epoch=100)
)
predictor = estimator.train(train_ds)
# 3. Forecast
from gluonts.evaluation.backtest import make_evaluation_predictions
forecast_it, ts_it = make_evaluation_predictions(test_ds, predictor, num_samples=100)
forecast = list(forecast_it)[0] # GluonTS Forecast object
print(forecast.mean)
You’ll now have 100 Monte-Carlo sample paths and stats such as mean, quantiles ( .quantile(0.9) ).
────────────────────────────────────────────────────────
3. Implementation tips & gotchas
────────────────────────────────────────────────────────
• Scale/normalise per-series or with GroupNormalizer; otherwise Transformers struggle.
• Always reserve the last k timesteps for validation so that temporal leakage doesn’t happen.
• For multi-series data, encode the series id (group_id) and optionally embeddings (category, region).
• Hyper-parameters matter less than data cleanliness. Check for missing timestamps, outliers, and correct freq.
• Hardware: Any modern GPU (even laptop RTX) speeds up training 10-20×, but CPU works for ≤100 k samples.
• Probabilistic metrics: prefer WAPE / MASE / pinball loss over plain RMSE if you care about business targets.
────────────────────────────────────────────────────────
4. What to do next?
────────────────────────────────────────────────────────
Clarify your horizon & granularity (hourly vs monthly).
Pick TFT or DeepAR if you’re starting; swap to Autoformer / PatchTST if you hit long-horizon issues.
Add covariates, holidays, promos—those usually improve accuracy more than model tweaks.
Use backtesting (rolling windows) to prove value before going to prod.
Put the trained predictor behind a small REST/Batch job (FastAPI, Airflow, or AWS Forecast/Vertex AI).
Happy forecasting! If you share a bit more about your dataset (frequency, number of series, horizon), I can tailor the code and hyper-parameters even further.