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

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

相关推荐
yvestine5 小时前
自然语言处理——Transformer
人工智能·深度学习·自然语言处理·transformer
码上地球11 小时前
卷积神经网络设计指南:从理论到实践的经验总结
人工智能·深度学习·cnn
MYH51611 小时前
神经网络 隐藏层
人工智能·深度学习·神经网络
king of code porter15 小时前
深度学习之模型压缩三驾马车:模型剪枝、模型量化、知识蒸馏
人工智能·深度学习·剪枝
聚客AI16 小时前
PyTorch进阶:从自定义损失函数到生产部署全栈指南
人工智能·pytorch·深度学习
寻丶幽风19 小时前
论文阅读笔记——Muffin: Testing Deep Learning Libraries via Neural Architecture Fuzzing
论文阅读·笔记·深度学习·网络安全·差分测试
强盛小灵通专卖员21 小时前
DL00871-基于深度学习YOLOv11的盲人障碍物目标检测含完整数据集
人工智能·深度学习·yolo·目标检测·计算机视觉·无人机·核心期刊
Morpheon21 小时前
循环神经网络(RNN):从理论到翻译
人工智能·rnn·深度学习·循环神经网络
Blossom.11821 小时前
基于机器学习的智能故障预测系统:构建与优化
人工智能·python·深度学习·神经网络·机器学习·分类·tensorflow
DartistCode21 小时前
动手学深度学习pytorch(第一版)学习笔记汇总
pytorch·深度学习·学习