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

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

相关推荐
陈苏同学11 分钟前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
FL162386312940 分钟前
[深度学习][python]yolov11+bytetrack+pyqt5实现目标追踪
深度学习·qt·yolo
羊小猪~~1 小时前
深度学习项目----用LSTM模型预测股价(包含LSTM网络简介,代码数据均可下载)
pytorch·python·rnn·深度学习·机器学习·数据分析·lstm
龙的爹23331 小时前
论文 | Model-tuning Via Prompts Makes NLP Models Adversarially Robust
人工智能·gpt·深度学习·语言模型·自然语言处理·prompt
工业机器视觉设计和实现1 小时前
cnn突破四(生成卷积核与固定核对比)
人工智能·深度学习·cnn
醒了就刷牙1 小时前
58 深层循环神经网络_by《李沐:动手学深度学习v2》pytorch版
pytorch·rnn·深度学习
985小水博一枚呀1 小时前
【对于Python爬虫的理解】数据挖掘、信息聚合、价格监控、新闻爬取等,附代码。
爬虫·python·深度学习·数据挖掘
想要打 Acm 的小周同学呀2 小时前
实现mnist手写数字识别
深度学习·tensorflow·实现mnist手写数字识别
萱仔学习自我记录2 小时前
微调大语言模型——超详细步骤
人工智能·深度学习·机器学习
Eric.Lee20213 小时前
音频文件重采样 - python 实现
人工智能·python·深度学习·算法·audio·音频重采样