微调是更有效地利用 TimeGPT 的强大过程。TimeGPT 等基础模型经过海量数据预训练,捕捉了广泛的特征和模式。然后可以针对特定上下文或领域对这些模型进行专门化。通过微调,模型的参数将被优化以预测新任务,从而使其能够根据新数据的要求调整其庞大的现有知识。因此,微调是连接 TimeGPT 广泛功能与您的特定任务的关键桥梁。

具体来说,微调过程包括在您的输入数据上执行一定数量的训练迭代,以最小化预测误差。然后将使用更新后的模型生成预测。要控制迭代次数,请使用 forecast 方法的 finetune_steps 参数。

1. 导入软件包

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

import pandas as pd
from nixtla import NixtlaClient
from utilsforecast.losses import mae, mse
from utilsforecast.evaluation import evaluate
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="you azure ai endpoint", api_key="your api_key")

2. 加载数据

df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv')
df.head()
时间戳
01949-01-01112
11949-02-01118
21949-03-01132
31949-04-01129
41949-05-01121

3. 微调

这里,finetune_steps=10 意味着模型将在您的时间序列数据上进行 10 次训练迭代。

timegpt_fcst_finetune_df = nixtla_client.forecast(
    df=df, h=12, finetune_steps=10,
    time_col='timestamp', target_col='value',
)
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Inferred freq: MS
INFO:nixtla.nixtla_client:Querying model metadata...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
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_df, 
    time_col='timestamp', target_col='value',
)

请记住,微调可能需要一些反复试验。您可能需要根据您的特定需求和数据的复杂性调整 finetune_steps 的数量。通常,对于大型数据集,较大的 finetune_steps 值效果更好。

建议在微调期间监控模型的性能并根据需要进行调整。请注意,更多的 finetune_steps 可能会导致更长的训练时间,如果管理不当,还可能导致过拟合。

记住,微调是一个强大的功能,但应深思熟虑、谨慎使用。

有关使用特定损失函数进行微调的详细指南,请查看使用特定损失函数进行微调教程。

另请阅读我们关于使用 finetune_depth 控制微调程度的详细教程。