Skip to main content

Building Time Series Forecasting Foundation Models

This comprehensive tutorial demonstrates building state-of-the-art time series forecasting foundation models through cross-modal continual pretraining. We’ll start with a pretrained language model (Gemma-3-4B) and adapt it for time series forecasting using advanced patch-based tokenization and a custom research multi-objective optimization with MAE and Quantile Loss with 20 quantiles.

What You’ll Learn

In this tutorial, we’ll cover:
  • Cross-modal continual pretraining from language models to time series
  • Patch-based time series tokenization with masking for robust learning
  • Custom multi-objective loss functions combining MAE and pinball loss
  • Advanced forecasting techniques using foundation model approaches
  • Production-ready model deployment for time series prediction

Step 1: Data Preparation

Your time series data should be in JSONL format following the AutoGluonTS format. Each line should contain a dictionary with the following structure: We’ll use patch-based tokenization to convert time series into sequences that can be processed by transformer architectures, enabling transfer from language models.
data_config.py
Run Data Preparation

Step 2: Custom Loss Function Implementation

Our custom loss function combines Mean Absolute Error (MAE) with Pinball Loss for quantile forecasting. While MAE is well-known, let’s focus on the key component:

Pinball Loss (Quantile Loss)

Pinball loss enables probabilistic forecasting by predicting multiple quantiles with asymmetric penalties: Pinball(τ)=1ni=1nmax(τ(yiy^iτ),(τ1)(yiy^iτ))\text{Pinball}(\tau) = \frac{1}{n} \sum_{i=1}^{n} \max\left(\tau(y_i - \hat{y}_i^\tau), (\tau-1)(y_i - \hat{y}_i^\tau)\right) Where:
  • τ\tau is the quantile level (e.g., 0.1 for 10th percentile)
  • y^iτ\hat{y}_i^\tau is the predicted value for quantile τ\tau
  • The loss penalizes under-prediction more for high quantiles, over-prediction more for low quantiles

Custom Multi-Objective Loss Function

Create a custom multi-objective loss function that combines MAE and pinball loss for comprehensive forecasting:
custom_loss.py

Step 3: Training Configuration

Configure the model for cross-modal continual pretraining from Gemma-3-4B to time series forecasting:
train_config.py
Launch the training process:
The platform will automatically:
  • Adapt the Gemma-3-4B architecture for time series processing
  • Apply patch-based tokenization during training
  • Optimize using the custom multi-objective loss function
  • Scale across multiple GPUs for efficient training

Advanced Foundation Model Features

The tutorial showcases several cutting-edge capabilities:
Language-to-Time Series Transfer LearningOur approach leverages pretrained language models for time series:
  • Preserves rich representational knowledge from language pretraining
  • Adapts transformer architectures to temporal patterns
  • Enables few-shot learning on new time series domains
  • Significantly reduces training time compared to training from scratch
Advanced Time Series RepresentationThe patch-based approach treats time series like sequences:
  • Input patches of 32 time steps for context understanding
  • Output patches of 128 time steps for multi-step forecasting
  • Patch masking during training improves robustness
  • Enables efficient processing of long time series
Sophisticated Loss Function DesignOur custom loss combines multiple objectives:
  • 70% MAE weight for robust point forecasting
  • 30% pinball loss weight for uncertainty quantification
  • 20 quantiles (vs. standard 10) for detailed probabilistic forecasting
  • Asymmetric penalty structure for realistic cost modeling
Scalable and Transferable ArchitectureThe foundation model approach provides:
  • Zero-shot forecasting on new time series
  • Few-shot adaptation to domain-specific patterns
  • Robust performance across diverse time series types
  • Efficient fine-tuning for specialized applications