TimeGPT_(5)特定领域微调模型
微调是一种强大的方法,用于更高效地利用 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(
# 默认值为 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="your 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()
timestamp | value | |
---|---|---|
0 | 1949-01-01 | 112 |
1 | 1949-02-01 | 118 |
2 | 1949-03-01 | 132 |
3 | 1949-04-01 | 129 |
4 | 1949-05-01 | 121 |
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:正在验证输入...
INFO:nixtla.nixtla_client:推断频率: MS
INFO:nixtla.nixtla_client:查询模型元数据...
INFO:nixtla.nixtla_client:正在预处理数据框...
INFO:nixtla.nixtla_client:调用预测端点...
📘 Azure AI 可用模型
如果您使用 Azure AI 端点,请确保设置
model="azureai"
:nixtla_client.forecast(..., model="azureai")
对于公共 API,我们支持两种模型:
timegpt-1
和timegpt-1-long-horizon
。默认使用
timegpt-1
。请参阅本教程,了解如何以及何时使用timegpt-1-long-horizon
。
nixtla_client.plot(
df, timegpt_fcst_finetune_df,
time_col='timestamp', target_col='value',
)
3.1 使用 finetune_depth
控制微调深度
还可以通过 finetune_depth
参数控制微调的深度。
finetune_depth
的取值范围为 [1, 2, 3, 4, 5]
。默认值为 1,这意味着模型的少量参数被调整,而 5 表示对模型的最大参数集进行微调。增大 finetune_depth
的值会增加生成预测所需的时间。
我们通过一个小实验来观察 finetune_depth
如何影响模型性能。
train = df[:-24]
test = df[-24:]
depths = [1, 2, 3, 4, 5]
test = test.copy()
for depth in depths:
preds_df = nixtla_client.forecast(
df=train,
h=24,
finetune_steps=5,
finetune_depth=depth,
time_col='timestamp',
target_col='value')
preds = preds_df['TimeGPT'].values
test.loc[:,f'TimeGPT_depth{depth}'] = preds
INFO:nixtla.nixtla_client:正在验证输入...
INFO:nixtla.nixtla_client:推断频率: MS
WARNING:nixtla.nixtla_client:指定的预测范围 "h" 超出了模型范围。这可能会导致预测不够准确。请考虑使用较小的范围。
INFO:nixtla.nixtla_client:正在预处理数据框...
INFO:nixtla.nixtla_client:调用预测端点...
test['unique_id'] = 0
evaluation = evaluate(test, metrics=[mae, mse], time_col="timestamp", target_col="value")
evaluation
unique_id | metric | TimeGPT_depth1 | TimeGPT_depth2 | TimeGPT_depth3 | TimeGPT_depth4 | TimeGPT_depth5 | |
---|---|---|---|---|---|---|---|
0 | 0 | mae | 22.805146 | 17.929682 | 21.320125 | 24.944233 | 28.735563 |
1 | 0 | mse | 683.303778 | 462.133945 | 678.182747 | 1003.023709 | 1119.906759 |
可以看到,增大微调深度可能会提高模型性能,但也可能由于过拟合而导致性能下降。
因此,请记住,微调有时需要试验和调整。您可能需要根据具体需求和数据复杂性调整 finetune_steps
和 finetune_depth
的值。通常,较高的 finetune_depth
更适合大型数据集。在本教程中,由于我们预测的是一个很短的数据集,增加深度导致了过拟合。
建议在微调过程中监控模型性能,并根据需要进行调整。注意,更多的 finetune_steps
和更大的 finetune_depth
值可能会导致更长的训练时间,并在管理不当时可能导致过拟合。
记住,微调是一项强大的功能,但应当谨慎合理地使用。