Introduction
In this tutorial, you'll learn how to implement a time series forecasting model similar to Google's TimesFM-3 using Python and the Hugging Face Transformers library. TimesFM-3 is designed to forecast future values by analyzing time series data along with external factors like sales promotions and weather forecasts. This approach is particularly useful for business applications such as inventory planning, demand forecasting, and sales optimization.
Unlike traditional forecasting models that predict one step at a time, TimesFM-3 processes all future time points simultaneously, reducing computational overhead and minimizing error accumulation. We'll create a simplified version of this approach to demonstrate the core concepts.
Prerequisites
- Basic knowledge of Python programming
- Familiarity with machine learning concepts
- Installed Python packages:
transformers,torch,numpy,pandas,matplotlib - Access to a machine with at least 8GB RAM (recommended for training)
Step-by-Step Instructions
1. Install Required Libraries
First, we need to install the necessary Python libraries. Run the following command in your terminal:
pip install transformers torch numpy pandas matplotlib
Why: The transformers library provides pre-trained models and tools for working with time series data, while torch is the deep learning framework that will power our model. numpy and pandas handle data manipulation, and matplotlib will help visualize our results.
2. Import Libraries and Prepare Sample Data
Next, create a Python script and import the required libraries:
import torch
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from transformers import AutoModelForTimeSeriesForecasting, AutoTokenizer
# Create sample data
np.random.seed(42)
# Generate time series data (e.g., daily sales)
num_days = 365
dates = pd.date_range(start='2023-01-01', periods=num_days, freq='D')
sales = 1000 + np.cumsum(np.random.randn(num_days) * 10) + np.sin(np.arange(num_days) * 2 * np.pi / 30) * 200
# Add external factors (weather and promotions)
weather = np.random.choice(['sunny', 'rainy', 'cloudy'], num_days, p=[0.5, 0.3, 0.2])
weather_encoded = [1 if w == 'sunny' else (0.5 if w == 'cloudy' else 0) for w in weather]
promotions = np.random.choice([0, 1], num_days, p=[0.9, 0.1])
# Create DataFrame
data = pd.DataFrame({
'date': dates,
'sales': sales,
'weather': weather_encoded,
'promotion': promotions
})
print(data.head())
Why: This creates a realistic dataset with sales data, weather information, and promotion indicators. The time series has a trend, seasonality, and random fluctuations, which mimics real-world business data.
3. Preprocess the Data
We need to prepare our data for training by normalizing and organizing it properly:
# Normalize the data
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
features = ['sales', 'weather', 'promotion']
scaler.fit(data[features])
# Transform data
data_scaled = data.copy()
data_scaled[features] = scaler.transform(data[features])
# Prepare sequences
sequence_length = 30 # Use 30 days to predict next 7 days
X, y = [], []
for i in range(sequence_length, len(data_scaled) - 7):
X.append(data_scaled[features].iloc[i-sequence_length:i].values)
y.append(data_scaled['sales'].iloc[i:i+7].values) # Predict next 7 days
X = np.array(X)
y = np.array(y)
print(f'X shape: {X.shape}, y shape: {y.shape}')
Why: We're creating sequences of historical data to predict future values. The sequence_length determines how many past days we use to predict the next 7 days. Normalization ensures all features contribute equally to the model training.
4. Build and Train the Model
Now, we'll create a simple neural network model to simulate the TimesFM-3 approach:
class ForecastingModel(torch.nn.Module):
def __init__(self, input_size, hidden_size=128, num_layers=2):
super().__init__()
self.lstm = torch.nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc = torch.nn.Linear(hidden_size, 7) # Predict 7 future days
def forward(self, x):
lstm_out, _ = self.lstm(x)
# Use the last time step's output
output = self.fc(lstm_out[:, -1, :])
return output
# Initialize model, loss, and optimizer
model = ForecastingModel(input_size=len(features))
criterion = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
# Convert data to tensors
X_tensor = torch.tensor(X, dtype=torch.float32)
y_tensor = torch.tensor(y, dtype=torch.float32)
# Train the model
num_epochs = 100
for epoch in range(num_epochs):
optimizer.zero_grad()
outputs = model(X_tensor)
loss = criterion(outputs, y_tensor)
loss.backward()
optimizer.step()
if (epoch + 1) % 20 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
Why: This model uses an LSTM (Long Short-Term Memory) network, which is excellent for time series data. The model processes all past time points in a single pass, similar to TimesFM-3's approach, rather than predicting step-by-step.
5. Make Predictions and Visualize Results
After training, we can make predictions and visualize them:
# Make predictions
model.eval()
with torch.no_grad():
predictions = model(X_tensor[-10:]) # Predict last 10 sequences
# Convert predictions back to original scale
predictions_original = scaler.inverse_transform(
np.concatenate([np.zeros((predictions.shape[0], 2)), predictions.numpy()], axis=1)
)[:, 0] # Only sales column
# Plot results
plt.figure(figsize=(12, 6))
plt.plot(data['date'][-50:], data['sales'][-50:], label='Actual Sales')
plt.plot(data['date'][-10:], predictions_original, label='Predicted Sales', marker='o')
plt.legend()
plt.title('Sales Forecasting with TimesFM-3 Approach')
plt.xlabel('Date')
plt.ylabel('Sales')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
Why: Visualizing the results helps us understand how well our model performs. We're comparing the actual sales data with our model's predictions to see how closely they match.
6. Evaluate Model Performance
To evaluate the model's accuracy, we'll calculate metrics like Mean Absolute Error (MAE) and Root Mean Square Error (RMSE):
# Calculate performance metrics
from sklearn.metrics import mean_absolute_error, mean_squared_error
# Use the last 10 predictions for evaluation
actual = data['sales'].iloc[-10:]
mae = mean_absolute_error(actual, predictions_original)
rmse = np.sqrt(mean_squared_error(actual, predictions_original))
print(f'Mean Absolute Error: {mae:.2f}')
print(f'Root Mean Square Error: {rmse:.2f}')
Why: These metrics give us a quantitative measure of how well our model performs. Lower values indicate better performance.
Summary
In this tutorial, we've built a simplified time series forecasting model inspired by Google's TimesFM-3. We learned how to:
- Prepare and preprocess time series data with external factors
- Build an LSTM-based model for forecasting
- Train the model to predict future values in a single pass
- Visualize and evaluate the model's performance
While this example is simplified, it demonstrates the core concepts behind TimesFM-3's approach to forecasting. The key advantage is processing all future time points simultaneously, which reduces computational costs and improves accuracy compared to traditional sequential forecasting methods.

