openai-community/gpt2

Hugging Face Models Trending 模型

摘要

本页详细介绍了GPT-2,这是一个基于Transformer的语言模型,经过英语文本预训练用于文本生成,可在Hugging Face上获取,并附有使用说明和示例。

任务: 文本生成 标签: transformers, pytorch, tf, jax, tflite, rust, onnx, safetensors, gpt2, text-generation, exbert, en, doi:10.57967/hf/0039, license:mit, text-generation-inference, endpoints_compatible, deploy:azure, region:us
查看原文
查看缓存全文

缓存时间: 2026/09/02 17:46

openai-community/gpt2 · Hugging Face

来源:https://huggingface.co/openai-community/gpt2

https://huggingface.co/openai-community/gpt2#gpt-2GPT-2

在此测试完整的文本生成能力:https://transformer.huggingface.co/doc/gpt2-large

该模型是一个基于英语语言的预训练模型,采用因果语言建模(CLM)目标。该模型于本文中提出(https://d4mucfpksywv.cloudfront.net/better-language-models/language_models_are_unsupervised_multitask_learners.pdf),并首次发布于此页面(https://openai.com/blog/better-language-models/)。

声明:发布GPT-2的团队也为其模型编写了模型卡(https://github.com/openai/gpt-2/blob/master/model_card.md)。模型卡中的部分内容由Hugging Face团队撰写,以完善他们提供的信息并给出具体的偏见示例。

https://huggingface.co/openai-community/gpt2#model-description模型描述

GPT-2是一个Transformers模型,在非常大的英语语料库上以自监督方式进行了预训练。这意味着它仅在原始文本上进行了预训练,没有任何人工标注(因此它可以使用大量公开数据),并使用一个自动生成输入和标签的过程。更具体地说,它是通过训练来猜测句子中的下一个词。

更准确地说,输入是特定长度的连续文本序列,目标是相同的序列,向右移动一个标记(词或词的一部分)。模型内部使用掩码机制,确保对词i的预测仅使用从1i的输入,而不使用未来的标记。

通过这种方式,模型学习到了英语语言的内在表征,然后可用于提取对下游任务有用的特征。然而,该模型最擅长的是其预训练目标——根据提示生成文本。

这是GPT-2的最小版本,拥有1.24亿参数。

**相关模型:**GPT-Large(https://huggingface.co/gpt2-large)、GPT-Medium(https://huggingface.co/gpt2-medium)和GPT-XL(https://huggingface.co/gpt2-xl)

https://huggingface.co/openai-community/gpt2#intended-uses–limitations预期用途与限制

您可以使用原始模型进行文本生成,或将其微调以适应下游任务。请参阅模型中心(https://huggingface.co/models?filter=gpt2)查找您感兴趣的特定任务的微调版本。

https://huggingface.co/openai-community/gpt2#how-to-use使用方法

您可以直接通过文本生成pipeline使用此模型。由于生成过程依赖一定的随机性,我们设置了一个随机种子以保证可重复性:

``

from transformers import pipeline, set_seed generator = pipeline(‘text-generation’, model=‘gpt2’) set_seed(42) generator(“Hello, I’m a language model,”, max_length=30, num_return_sequences=5)

[{‘generated_text’: “Hello, I’m a language model, a language for thinking, a language for expressing thoughts.”}, {‘generated_text’: “Hello, I’m a language model, a compiler, a compiler library, I just want to know how I build this kind of stuff. I don”}, {‘generated_text’: “Hello, I’m a language model, and also have more than a few of your own, but I understand that they’re going to need some help”}, {‘generated_text’: “Hello, I’m a language model, a system model. I want to know my language so that it might be more interesting, more user-friendly”}, {‘generated_text’: ‘Hello, I'm a language model, not a language model“\n\nThe concept of “no-tricks” comes in handy later with new’}]


以下是如何在PyTorch中使用此模型获取给定文本特征的方法:

``
from transformers import GPT2Tokenizer, GPT2Model
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2Model.from_pretrained('gpt2')
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
``

以及在TensorFlow中的方法:

``
from transformers import GPT2Tokenizer, TFGPT2Model
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = TFGPT2Model.from_pretrained('gpt2')
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
``

### https://huggingface.co/openai-community/gpt2#limitations-and-bias局限性与偏见

用于训练此模型的训练数据并未作为可浏览的数据集发布。我们知道它包含了大量来自互联网的、未经筛选的内容,远非中立。正如OpenAI团队在其模型卡(https://github.com/openai/gpt-2/blob/master/model_card.md#out-of-scope-use-cases)中自己指出的那样:

> 因为像GPT\-2这样的大规模语言模型无法区分事实与虚构,我们不支持需要生成文本为真实情况的应用场景。此外,像GPT\-2这样的语言模型反映了其训练系统中固有的偏见,因此我们不建议将其部署到与人类交互的系统中,除非部署者首先对与预期用途相关的偏见进行研究。我们发现,在7.74亿和15亿参数版本之间,在性别、种族和宗教偏见探测方面没有统计学上的显著差异,这意味着所有版本的GPT\-2在涉及人类属性敏感偏见的应用场景中都应以相似的谨慎程度对待。

以下是模型可能产生偏见预测的一个示例:

``
>>> from transformers import pipeline, set_seed
>>> generator = pipeline('text-generation', model='gpt2')
>>> set_seed(42)
>>> generator("The White man worked as a", max_length=10, num_return_sequences=5)

[{'generated_text': 'The White man worked as a mannequin for'},
 {'generated_text': 'The White man worked as a maniser of the'},
 {'generated_text': 'The White man worked as a bus conductor by day'},
 {'generated_text': 'The White man worked as a plumber at the'},
 {'generated_text': 'The White man worked as a journalist. He had'}]

>>> set_seed(42)
>>> generator("The Black man worked as a", max_length=10, num_return_sequences=5)

[{'generated_text': 'The Black man worked as a man at a restaurant'},
 {'generated_text': 'The Black man worked as a car salesman in a'},
 {'generated_text': 'The Black man worked as a police sergeant at the'},
 {'generated_text': 'The Black man worked as a man-eating monster'},
 {'generated_text': 'The Black man worked as a slave, and was'}]
``

这种偏见也会影响此模型的所有微调版本。

## https://huggingface.co/openai-community/gpt2#training-data训练数据

OpenAI团队希望在这个模型上使用尽可能大的语料库进行训练。为此,他们抓取了Reddit上所有获得至少3个karma的出站链接所指向的所有网页。请注意,所有维基百科页面都已从该数据集中移除,因此该模型并未在维基百科的任何部分上进行训练。由此产生的数据集(称为WebText)包含40GB的文本,但尚未公开发布。您可以在[此处](https://github.com/openai/gpt-2/blob/master/domains.txt)找到WebText中前1000个域名的列表。

## https://huggingface.co/openai-community/gpt2#training-procedure训练过程

### https://huggingface.co/openai-community/gpt2#preprocessing预处理

文本使用字节对编码(BPE)的字节级版本进行分词(用于Unicode字符),词汇表大小为50,257。输入是1024个连续标记的序列。

更大的模型在256个云端TPU v3核心上进行了训练。训练时长未公开,训练的确切细节也未披露。

## https://huggingface.co/openai-community/gpt2#evaluation-results评估结果

该模型在没有进行任何微调(零样本)的情况下取得了以下结果:

数据集LAMBADALAMBADACBT\-CNCBT\-NEWikiText2PTBenwiki8text8WikiText1031BW(指标)(PPL)(ACC)(ACC)(ACC)(PPL)(PPL)(BPB)(BPC)(PPL)(PPL)35\.1345\.9987\.6583\.429\.4165\.851\.161,1737\.5075\.20
### https://huggingface.co/openai-community/gpt2#bibtex-entry-and-citation-infoBibTeX条目与引用信息

``
@article{radford2019language,
  title={Language Models are Unsupervised Multitask Learners},
  author={Radford, Alec and Wu, Jeff and Child, Rewon and Luan, David and Amodei, Dario and Sutskever, Ilya},
  year={2019}
}
``

[](https://huggingface.co/exbert/?model=gpt2)

相似文章

更好的语言模型及其影响

OpenAI Blog

OpenAI 推出 GPT-2,这是一个拥有 15 亿参数的基于 Transformer 的语言模型,在 40GB 的互联网文本上进行训练,在语言建模基准上达到了最先进的性能,并在阅读理解、翻译、问答和摘要生成等任务上展示了零样本学习能力。出于安全考虑,仅公开发布了较小的模型和技术论文,而非完整的训练模型。

用于自动定理证明的生成语言建模

OpenAI Blog

# 用于自动定理证明的生成语言建模 来源: [https://openai.com/index/generative-language-modeling-for-automated-theorem-proving/](https://openai.com/index/generative-language-modeling-for-automated-theorem-proving/) OpenAI## 摘要 我们探索了基于 Transformer 的语言模型在自动定理证明中的应用。这项工作的动力来自于一种可能性,即自动定理证明器与人类相比的一个主要局限——原始内容的生成

推出 gpt-oss

OpenAI Blog

OpenAI 发布 gpt-oss-120b 和 gpt-oss-20b,两款最先进的开放权重语言模型,采用 Apache 2.0 许可证,性能与专有模型相当,可针对消费级硬件和边缘设备进行优化。两款模型均展现出强大的推理和工具使用能力,并进行了全面的安全评估。