使用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 小时前
GPT-1 技术报告
gpt
电脑小白技术1 天前
u盘安装系统提示“windows无法安装到这个磁盘,选中的磁盘具有gpt分区表”解决方法
windows·gpt·windows无法安装到磁盘
余衫马1 天前
GPT结构剖析:从“词袋”到“思想”的魔法之旅
人工智能·gpt·大模型
青春不败 177-3266-05202 天前
GPT、DeepSeek等大语言模型应用
人工智能·gpt·深度学习·机器学习·语言模型·科研绘图
王国强20092 天前
拆解Transformer之Embedding
gpt
蚂蚁数据AntData3 天前
DB-GPT 0.7.4 版本更新|开源蚂蚁集团Text2SQL数据集:Falcon、支持GLM-4.5大模型
数据库·gpt·语言模型·开源
Lethehong3 天前
搭建AI智能翻译器:快速部署Dify,接入AiOnly平台GPT-5模型
人工智能·gpt·dify·maas·aionly
CaracalTiger5 天前
告别云端依赖!ComfyUI本地化视频生成实战教程+cpolar实战
python·gpt·开源·aigc·ai编程·1024程序员节·ai-native
mit6.8246 天前
[nanoGPT] 数据预处理与分词 | BPE配合`tiktoken`
gpt
七宝大爷6 天前
大模型是什么?从 GPT 到 LLaMA 的核心概念解析
gpt·llama