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

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

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

python 复制代码
# 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
python 复制代码
colab_badge('docs/tutorials/5_finetuning')
python 复制代码
# 导入load_dotenv函数,用于加载.env文件中的环境变量
from dotenv import load_dotenv
python 复制代码
# 导入load_dotenv函数,用于加载环境变量
load_dotenv()
复制代码
True
python 复制代码
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
python 复制代码
# 创建TimeGPT实例,传入token参数,如果没有传入则使用环境变量中的TIMEGPT_TOKEN
timegpt = TimeGPT(token='my_token_provided_by_nixtla')
python 复制代码
# 导入TimeGPT模块

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

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

python 复制代码
# 从指定的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
python 复制代码
# 导入所需模块和函数

# 定义一个函数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...
python 复制代码
# 导入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可能会导致更长的训练时间,并且如果管理不当可能会导致过拟合。

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

相关推荐
董建光d1 小时前
【深度学习】目标检测全解析:定义、数据集、评估指标与主流算法
深度学习·算法·目标检测
星期天要睡觉1 小时前
计算机视觉(opencv)——基于 MediaPipe 的实时面部表情识别
人工智能·深度学习·机器学习
好家伙VCC2 小时前
**TensorFlow:发散创新的深度学习框架探索**随着人工智
java·人工智能·python·深度学习·tensorflow
Dev7z2 小时前
深度学习与舌诊的结合:人工智能助力中医诊断新时代
人工智能·深度学习
AIzealot无3 小时前
Qwen3 Embedding报告随笔
人工智能·深度学习·算法·论文·embedding·论文笔记·搜广推
渡我白衣3 小时前
《深度学习进阶(四)——多模态智能:语言、视觉与语音的融合》
人工智能·深度学习
王一点er3 小时前
为什么LLM中KL散度需要近似计算
人工智能·深度学习
悠闲蜗牛�3 小时前
深度学习与大规模系统构建:AI技术在实际项目中的应用
人工智能·深度学习
Wah-Aug4 小时前
目标检测全解析:从基础概念到深度学习实战技术
深度学习·目标检测·目标跟踪
CoovallyAIHub5 小时前
清华Mars Lab发布SLAM-Former:用一个Transformer统一SLAM的前端与后端(附项目地址)
深度学习·算法·计算机视觉