工具系列: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可能会导致更长的训练时间,并且如果管理不当可能会导致过拟合。

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

相关推荐
shangyingying_17 小时前
关于小波降噪、小波增强、小波去雾的原理区分
人工智能·深度学习·计算机视觉
书玮嘎9 小时前
【WIP】【VLA&VLM——InternVL系列】
人工智能·深度学习
要努力啊啊啊9 小时前
YOLOv2 正负样本分配机制详解
人工智能·深度学习·yolo·计算机视觉·目标跟踪
Blossom.11810 小时前
机器学习在智能建筑中的应用:能源管理与环境优化
人工智能·python·深度学习·神经网络·机器学习·机器人·sklearn
m0_6786933311 小时前
深度学习笔记29-RNN实现阿尔茨海默病诊断(Pytorch)
笔记·rnn·深度学习
胡耀超11 小时前
标签体系设计与管理:从理论基础到智能化实践的综合指南
人工智能·python·深度学习·数据挖掘·大模型·用户画像·语义分析
fzyz12313 小时前
Windows系统下WSL从C盘迁移方案
人工智能·windows·深度学习·wsl
FF-Studio15 小时前
【硬核数学 · LLM篇】3.1 Transformer之心:自注意力机制的线性代数解构《从零构建机器学习、深度学习到LLM的数学认知》
人工智能·pytorch·深度学习·线性代数·机器学习·数学建模·transformer
云渚钓月梦未杳15 小时前
深度学习03 人工神经网络ANN
人工智能·深度学习
贾全16 小时前
第十章:HIL-SERL 真实机器人训练实战
人工智能·深度学习·算法·机器学习·机器人