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

Fine-tuning(微调)是一种更有效地利用TimeGPT的强大过程。基础模型在大量数据上进行预训练,捕捉广泛的特征和模式。然后可以将这些模型专门用于特定的上下文或领域。通过微调,可以对模型的参数进行优化,以预测新任务,使其将其广泛的预先存在的知识调整到新数据的要求上。因此,微调作为一个关键的桥梁,将TimeGPT的广泛能力与您任务的特定性联系起来。

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

# Import the colab_badge module from the nixtlats.utils package
from nixtlats.utils import colab_badge
/home/ubuntu/miniconda/envs/nixtlats/lib/python3.11/site-packages/statsforecast/core.py:25: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from tqdm.autonotebook import tqdm
colab_badge('docs/tutorials/5_finetuning')
# 导入load_dotenv函数,用于加载.env文件中的环境变量
from dotenv import load_dotenv
# 导入load_dotenv函数,用于加载环境变量
load_dotenv()
True

import pandas as pd
from nixtlats import TimeGPT

/home/ubuntu/miniconda/envs/nixtlats/lib/python3.11/site-packages/statsforecast/core.py:25: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from tqdm.autonotebook import tqdm
# 创建TimeGPT实例,传入token参数,如果没有传入则使用环境变量中的TIMEGPT_TOKEN
timegpt = TimeGPT(token='my_token_provided_by_nixtla')
# 导入TimeGPT模块

timegpt = TimeGPT()  # 创建TimeGPT对象的实例

以下是如何对TimeGPT进行微调的示例:

# 从指定的URL读取CSV文件,并将其存储在DataFrame中
df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv')

# 显示DataFrame的前几行数据
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
# 导入所需模块和函数

# 定义一个函数timegpt_fcst_finetune_df,用于对时间序列数据进行预测和微调
# 参数df表示输入的时间序列数据
# 参数h表示预测的时间步长
# 参数finetune_steps表示微调的步数
# 参数time_col表示时间列的名称
# 参数target_col表示目标列的名称
def timegpt_fcst_finetune_df(df, h, finetune_steps, time_col, target_col):
    
    # 调用timegpt模块中的forecast函数,对时间序列数据进行预测
    # 将预测结果赋值给变量timegpt_fcst_finetune_df
    timegpt_fcst_finetune_df = timegpt.forecast(df=df, h=h, finetune_steps=finetune_steps, time_col=time_col, target_col=target_col)
    
    # 返回预测结果
    return timegpt_fcst_finetune_df
INFO:nixtlats.timegpt:Validating inputs...
INFO:nixtlats.timegpt:Preprocessing dataframes...
INFO:nixtlats.timegpt:Calling Forecast Endpoint...
# 导入timegpt模块中的plot函数

# 使用plot函数绘制图表,传入以下参数:
# - df: 原始数据集,包含时间戳和值两列
# - timegpt_fcst_finetune_df: 经过时间序列预测和微调后的数据集,包含时间戳和预测值两列
# - time_col: 时间戳所在的列名
# - target_col: 值所在的列名
timegpt.plot(
    df, timegpt_fcst_finetune_df, 
    time_col='timestamp', target_col='value',
)

在这段代码中,finetune_steps=10表示模型将在您的时间序列数据上进行10次训练迭代。

请记住,微调可能需要一些试错。您可能需要根据您的特定需求和数据的复杂性来调整finetune_steps的数量。建议在微调过程中监控模型的性能并根据需要进行调整。请注意,更多的finetune_steps可能会导致更长的训练时间,并且如果管理不当可能会导致过拟合。

请记住,微调是一个强大的功能,但应该谨慎使用。

Guess you like

Origin blog.csdn.net/wjjc1017/article/details/135244030