This tutorial shows how to fine-tune a model using a specific loss function. You will learn which loss functions are supported, how to configure your Nixtla client, and how to evaluate different approaches for best results.

When you fine-tune, the model trains on your dataset to tailor predictions to your specific scenario. You can specify the loss function to be used during fine-tuning. Below are the available loss functions:

Default

A proprietary function robust to outliers.

MAE

Mean Absolute Error

MSE

Mean Squared Error

RMSE

Root Mean Squared Error

MAPE

Mean Absolute Percentage Error

sMAPE

Symmetric Mean Absolute Percentage Error

1

1. Import Packages

Make sure you have the necessary dependencies installed (such as pandas and nixtla).

Imports for Fine-Tuning
import pandas as pd
from nixtla import NixtlaClient
from utilsforecast.losses import mae, mse, rmse, mape, smape

Initialize the NixtlaClient object
Replace api_key='my_api_key_provided_by_nixtla' with the API key from your Nixtla account.

Nixtla Client Initialization
nixtla_client = NixtlaClient(
    # defaults to os.environ.get("NIXTLA_API_KEY")
    api_key='my_api_key_provided_by_nixtla'
)

Using an Azure AI endpoint
Pass the base_url argument explicitly:

Azure AI Client Example
nixtla_client = NixtlaClient(
    base_url="your azure ai endpoint",
    api_key="your api_key"
)
2

2. Load Data

Load your data and prepare it for fine-tuning. Here, we will demonstrate using an example dataset of air passenger counts.

Load Air Passengers Data
df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv')
df.insert(loc=0, column='unique_id', value=1)

df.head()
unique_idtimestampvalue
011949-01-01112
111949-02-01118
211949-03-01132
311949-04-01129
411949-05-01121
3

3. Fine-Tune the Model

For other fine-tuning configurations, simply change finetune_loss to your preferred loss function (mse, rmse, mape, or smape).

Selecting the appropriate loss function during fine-tuning ensures the model is optimized around what matters most for your use case.

Perform a quick evaluation to compare metrics across different loss functions. Fine-tuning with a specific loss can yield better accuracy for your goals.

maemsermsemapesmape
Metric improvement (%)8.540.310.6431.027.36

By aligning the model’s training objective with your key evaluation metric, you achieve more targeted performance improvements for your forecasting tasks.