使用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)
相关推荐
未来智慧谷1 天前
大模型工业化元年:GPT-5开启通用AI新纪元,中国技术如何破局?
人工智能·gpt
樱花穿过千岛湖2 天前
第六章:Multi-Backend Configuration
人工智能·python·gpt·学习·ai
量子位2 天前
北大团队引领 3D 生成与对齐革新:OctGPT 打破扩散模型垄断
人工智能·gpt·aigc
web守墓人3 天前
【gpt生成-其二】以go语言为例,详细讲解 并发模型:线程/协程/ Actor 实现
java·gpt·golang
剑客的茶馆4 天前
GPT,Genini, Claude Llama, DeepSeek,Qwen,Grok,选对LLM大模型真的可以事半功倍!
gpt·llm·llama·选择大模型
web守墓人4 天前
【gpt生成-其一】以go语言为例,详细描述一下 :语法规范BNF/EBNF形式化描述
前端·gpt·golang
ai_大师5 天前
Cursor怎么使用,3分钟上手Cursor:比ChatGPT更懂需求,用聊天的方式写代码,GPT4、Claude 3.5等先进LLM辅助编程
gpt·claude·cursor·apikey·中转apikey·免费apikey
移动安全星球5 天前
从零开始:CherryStudio 打造专属本地 AI 知识库全攻略
人工智能·gpt·claude·本地知识库·deepseek
bingbingyihao5 天前
GPT对话UI--通义千问API
gpt·ui
陈奕昆6 天前
论文降重GPT指令-实侧有效从98%降低到8%
人工智能·gpt·论文·降重