在进行微调时,模型会在您的数据集上进行训练,以根据您的特定场景调整其预测。因此,可以指定微调过程中使用的损失函数。

具体来说,您可以选择以下选项:

  • "default" - 一种对异常值具有鲁棒性的专有损失函数
  • "mae" - 平均绝对误差
  • "mse" - 均方误差
  • "rmse" - 均方根误差
  • "mape" - 平均绝对百分比误差
  • "smape" - 对称平均绝对百分比误差

1. 导入包

首先,我们导入所需的包并初始化 Nixtla 客户端。

import pandas as pd
from nixtla import NixtlaClient
from utilsforecast.losses import mae, mse, rmse, mape, smape
nixtla_client = NixtlaClient(
    # defaults to os.environ.get("NIXTLA_API_KEY")
    api_key = 'my_api_key_provided_by_nixtla'
)

👍 使用 Azure AI 端点

要使用 Azure AI 端点,请记住同时设置 base_url 参数

nixtla_client = NixtlaClient(base_url="你的 azure ai 端点", api_key="你的 api_key")

2. 加载数据

让我们使用平均绝对误差 (MAE) 在数据集上微调模型。

为此,我们只需将代表损失函数的相应字符串传递给 forecast 方法的 finetune_loss 参数即可。

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. 使用平均绝对误差进行微调

让我们使用平均绝对误差 (MAE) 在数据集上微调模型。

为此,我们只需将代表损失函数的相应字符串传递给 forecast 方法的 finetune_loss 参数即可。

timegpt_fcst_finetune_mae_df = nixtla_client.forecast(
    df=df, 
    h=12, 
    finetune_steps=10,
    finetune_loss='mae',   # Set your desired loss function
    time_col='timestamp', 
    target_col='value',
)
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Inferred freq: MS
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...

📘 Azure AI 中的可用模型

如果您正在使用 Azure AI 端点,请务必设置 model="azureai"

nixtla_client.forecast(..., model="azureai")

对于公共 API,我们支持两种模型:timegpt-1timegpt-1-long-horizon

默认情况下使用 timegpt-1。请参阅本教程,了解如何以及何时使用 timegpt-1-long-horizon

nixtla_client.plot(
    df, timegpt_fcst_finetune_mae_df, 
    time_col='timestamp', target_col='value',
)

现在,根据您的数据,您将使用特定的误差指标来准确评估您的预测模型的性能。

以下是关于根据您的用例选择哪种指标的非详尽指南。

平均绝对误差 (MAE)

  • 对异常值具有鲁棒性
  • 易于理解
  • 您同等重视所有误差大小
  • 单位与您的数据相同

均方误差 (MSE)

  • 您希望对大误差施加比小误差更大的惩罚
  • 对异常值敏感
  • 用于必须避免大误差的情况
  • 与您的数据单位不同

均方根误差 (RMSE)

  • 将 MSE 恢复到原始数据单位
  • 对大误差施加比小误差更大的惩罚

平均绝对百分比误差 (MAPE)

  • 非技术利益相关者易于理解
  • 以百分比表示
  • 对正误差施加比负误差更重的惩罚
  • 如果您的数据值接近或等于 0,则应避免使用

对称平均绝对百分比误差 (sMAPE)

  • 修正了 MAPE 的偏差
  • 对预测过高和预测过低同样敏感
  • 如果您的数据值接近或等于 0,则应避免使用

使用 TimeGPT,您可以在微调过程中选择损失函数,以便最大化模型在您的特定用例中的性能指标。

让我们进行一个小实验,看看与默认设置相比,每种损失函数如何改善其相关的指标。

train = df[:-36]
test = df[-36:]
losses = ['default', 'mae', 'mse', 'rmse', 'mape', 'smape']

test = test.copy()

for loss in losses:
    preds_df = nixtla_client.forecast(
    df=train, 
    h=36, 
    finetune_steps=10,
    finetune_loss=loss,
    time_col='timestamp', 
    target_col='value')

    preds = preds_df['TimeGPT'].values

    test.loc[:,f'TimeGPT_{loss}'] = preds
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Inferred freq: MS
WARNING:nixtla.nixtla_client:The specified horizon "h" exceeds the model horizon. This may lead to less accurate forecasts. Please consider using a smaller horizon.
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Inferred freq: MS
WARNING:nixtla.nixtla_client:The specified horizon "h" exceeds the model horizon. This may lead to less accurate forecasts. Please consider using a smaller horizon.
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Inferred freq: MS
WARNING:nixtla.nixtla_client:The specified horizon "h" exceeds the model horizon. This may lead to less accurate forecasts. Please consider using a smaller horizon.
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Inferred freq: MS
WARNING:nixtla.nixtla_client:The specified horizon "h" exceeds the model horizon. This may lead to less accurate forecasts. Please consider using a smaller horizon.
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Inferred freq: MS
WARNING:nixtla.nixtla_client:The specified horizon "h" exceeds the model horizon. This may lead to less accurate forecasts. Please consider using a smaller horizon.
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Inferred freq: MS
WARNING:nixtla.nixtla_client:The specified horizon "h" exceeds the model horizon. This may lead to less accurate forecasts. Please consider using a smaller horizon.
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...

📘 Azure AI 中的可用模型

如果您正在使用 Azure AI 端点,请务必设置 model="azureai"

nixtla_client.forecast(..., model="azureai")

对于公共 API,我们支持两种模型:timegpt-1timegpt-1-long-horizon

默认情况下使用 timegpt-1。请参阅本教程,了解如何以及何时使用 timegpt-1-long-horizon

太棒了!我们使用所有不同的损失函数获得了 TimeGPT 的预测结果。我们可以使用与其相关的指标评估性能并衡量改进程度。

loss_fct_dict = {
    "mae": mae,
    "mse": mse,
    "rmse": rmse,
    "mape": mape,
    "smape": smape
}

pct_improv = []

for loss in losses[1:]:
    evaluation = loss_fct_dict[f'{loss}'](test, models=['TimeGPT_default', f'TimeGPT_{loss}'], id_col='unique_id', target_col='value')
    pct_diff = (evaluation['TimeGPT_default'] - evaluation[f'TimeGPT_{loss}']) / evaluation['TimeGPT_default'] * 100
    pct_improv.append(round(pct_diff, 2))
data = {
    'mae': pct_improv[0].values,
    'mse': pct_improv[1].values,
    'rmse': pct_improv[2].values,
    'mape': pct_improv[3].values,
    'smape': pct_improv[4].values
}

metrics_df = pd.DataFrame(data)
metrics_df.index = ['Metric improvement (%)']

metrics_df
maemsermsemapesmape
指标改进 (%)8.540.310.6431.027.36

从上表中可以看出,与默认损失函数相比,在微调过程中使用特定的损失函数会改善其相关的误差指标。

在此示例中,使用 MAE 作为损失函数与使用默认损失函数相比,将指标提高了 8.54%。

这样,根据您的用例和性能指标,您可以使用适当的损失函数来最大化预测的准确性。