工具系列:TimeGPT_(5)特定领域微调模型

本文介绍了如何通过Fine-tuning(微调)将预训练的TimeGPT模型调整到特定任务,通过在新数据上进行有限的训练迭代以优化参数。微调作为关键桥梁,平衡了模型的泛化能力和任务适应性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()
timestampvalue
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:正在验证输入...
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-1timegpt-1-long-horizon

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

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

img

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_idmetricTimeGPT_depth1TimeGPT_depth2TimeGPT_depth3TimeGPT_depth4TimeGPT_depth5
00mae22.80514617.92968221.32012524.94423328.735563
10mse683.303778462.133945678.1827471003.0237091119.906759

可以看到,增大微调深度可能会提高模型性能,但也可能由于过拟合而导致性能下降。

因此,请记住,微调有时需要试验和调整。您可能需要根据具体需求和数据复杂性调整 finetune_stepsfinetune_depth 的值。通常,较高的 finetune_depth 更适合大型数据集。在本教程中,由于我们预测的是一个很短的数据集,增加深度导致了过拟合。

建议在微调过程中监控模型性能,并根据需要进行调整。注意,更多的 finetune_steps 和更大的 finetune_depth 值可能会导致更长的训练时间,并在管理不当时可能导致过拟合。

记住,微调是一项强大的功能,但应当谨慎合理地使用。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数智笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值