使用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)
相关推荐
源大模型4 小时前
OS-Genesis:基于逆向任务合成的 GUI 代理轨迹自动化生成
人工智能·gpt·智能体
少林码僧2 天前
1.5 企业级AI大模型四阶技术全景解析:从Prompt到Pre-training的进化路径
人工智能·gpt·chatgpt·langchain·prompt
人机与认知实验室2 天前
Deepseek与GPT都还是人机环境系统智能的初级产品
gpt
听吉米讲故事2 天前
2025年AI免费大战:从DeepSeek到GPT-5的商业逻辑与行业变革
人工智能·gpt·开源·deepseek
编程乐趣2 天前
微软官方出品GPT大模型编排工具:7个开源项目
gpt·开源
RamboPan3 天前
能用GPT解决的,就不用自己动手
gpt
O-A-A3 天前
LLM:GPT 系列
gpt·chatgpt·llm·gpt-3
xiao-chong4 天前
GPT和BERT
gpt·自然语言处理·bert
TIANE-Kimmy5 天前
GPT 系列模型发展史:从 GPT 到 ChatGPT 的演进与技术细节
gpt·chatgpt
技术程序猿华锋5 天前
OpenAI发布新模型及会员订阅计划:o3-mini、GPT-4.5与GPT-5的全新体验
人工智能·gpt·chatgpt