使用GPT2预训练模型的方法

使用GPT2预训练模型的方法

flyfish

transformers库对所有模型统一的API

安装

复制代码
pip install transformers

GPT2模型主要包括以下文件

复制代码
config.json
merges.txt
model.safetensors
tokenizer.json
tokenizer_config.json
vocab.json

模型所在目录

复制代码
\.cache\huggingface\hub\models--openai-community--gpt2\blobs

模型链接

复制代码
.cache\huggingface\hub\models--openai-community--gpt2\snapshots

config.json [..\..\blobs\10c66461e4c109db5a2196bff4bb59be30396ed8]
merges.txt [..\..\blobs\226b0752cac7789c48f0cb3ec53eda48b7be36cc]
model.safetensors [..\..\blobs\248dfc3911869ec493c76e65bf2fcf7f615828b0254c12b473182f0f81d3a707]
tokenizer.json [..\..\blobs\4b988bccc9dc5adacd403c00b4704976196548f8]
tokenizer_config.json [..\..\blobs\be4d21d94f3b4687e5a54d84bf6ab46ed0f8defd]
vocab.json [..\..\blobs\1f1d9aaca301414e7f6c9396df506798ff4eb9a6]

可以到这里下载

复制代码
链接:https://pan.baidu.com/s/1A8MLV_BxcJLEIr4_oOVsUQ 
提取码:0000

简单示例

py 复制代码
from transformers import AutoTokenizer, GPT2Model
import torch

tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
model = GPT2Model.from_pretrained("openai-community/gpt2")

inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
outputs = model(**inputs)

last_hidden_states = outputs.last_hidden_state

neuralforecast 的用法

py 复制代码
from neuralforecast import NeuralForecast
from neuralforecast.models import TimeLLM
from neuralforecast.utils import AirPassengersPanel, augment_calendar_df

from transformers import GPT2Config, GPT2Model, GPT2Tokenizer

AirPassengersPanel, calendar_cols = augment_calendar_df(df=AirPassengersPanel, freq='M')

Y_train_df = AirPassengersPanel[AirPassengersPanel.ds<AirPassengersPanel['ds'].values[-12]] # 132 train
Y_test_df = AirPassengersPanel[AirPassengersPanel.ds>=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test

gpt2_config = GPT2Config.from_pretrained('openai-community/gpt2')
gpt2 = GPT2Model.from_pretrained('openai-community/gpt2', config=gpt2_config)
gpt2_tokenizer = GPT2Tokenizer.from_pretrained('openai-community/gpt2')

prompt_prefix = "The dataset contains data on monthly air passengers. There is a yearly seasonality"

timellm = TimeLLM(h=12,
                 input_size=36,
                 llm=gpt2,
                 llm_config=gpt2_config,
                 llm_tokenizer=gpt2_tokenizer,
                 prompt_prefix=prompt_prefix,
                 batch_size=24,
                 windows_batch_size=24)

nf = NeuralForecast(
    models=[timellm],
    freq='M'
)

nf.fit(df=Y_train_df, val_size=12)
forecasts = nf.predict(futr_df=Y_test_df)
相关推荐
前端小盆友16 小时前
从零实现一个GPT 【React + Express】--- 【2】实现对话流和停止生成
前端·gpt·react.js
牛大了202311 天前
【LLM学习】2-简短学习BERT、GPT主流大模型
gpt·学习·bert
1213411 天前
LLM:重构数字世界的“智能操作系统”
gpt·aigc·ai编程·llama·gpu算力
叠叠乐17 天前
ROS2编译的理解,与GPT对话
gpt
蚂蚁数据AntData18 天前
DB-GPT V0.7.2 版本更新:图表组件可视化增强、支持混合搜索 、支持DeepSeek-R1-0528模型等
大数据·数据库·gpt·架构·数据库架构
PacosonSWJTU22 天前
加载GPT-2模型参数报错:TensorFlow不存在
人工智能·gpt·tensorflow
zm-v-1593043398623 天前
GPT-ArcGIS 在生态评价中的综合应用:多因子权重分析与适宜性制图
gpt·arcgis
激进小猪100224 天前
LLM基础5_从零开始实现 GPT 模型
gpt
阿部多瑞 ABU1 个月前
# 从底层架构到应用实践:为何部分大模型在越狱攻击下失守?
gpt·安全·ai·自然语言处理
阿部多瑞 ABU1 个月前
大模型安全测试报告:千问、GPT 全系列、豆包、Claude 表现优异,DeepSeek、Grok-3 与 Kimi 存在安全隐患
gpt·安全·ai