huggingface笔记:gpt2

0 使用的tips

  • GPT-2是一个具有绝对位置嵌入的模型,因此通常建议在输入的右侧而不是左侧填充
  • GPT-2是通过因果语言建模(CLM)目标进行训练的,因此在预测序列中的下一个标记方面非常强大
    • 利用这一特性,GPT-2可以生成语法连贯的文本
  • GPT-2可以接受past_key_values(对于PyTorch)或past(对于TF)作为输入
    • 这些是先前计算的键/值注意力对。‘
    • 使用这个(past_key_values或past)值可以防止模型在文本生成过程中重新计算预计算的值
  • 启用scale_attn_by_inverse_layer_idx和reorder_and_upcast_attn标志将应用Mistral的训练稳定性改进(仅适用于PyTorch)

1 基本实例

from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained("gpt2")
tokenizer = AutoTokenizer.from_pretrained("gpt2")

prompt = "GPT2 is a model developed by OpenAI."

input_ids = tokenizer(prompt, return_tensors="pt").input_ids

gen_tokens = model.generate(
    input_ids,
    do_sample=True,
    temperature=0.9,
    max_length=100,
)
gen_text = tokenizer.batch_decode(gen_tokens)[0]


print(gen_text)
'''
GPT2 is a model developed by OpenAI. It helps to define the neural network of a person with Autism. The researchers have previously identified the basic network of neurons in the brain responsible for processing emotional information. They also found that the person with Autism has similar information processing abilities as other people with similar intelligence.

The researchers say that it's important to look beyond the normal limitations of the human brain. "This type of neuroimaging has been really important," explains Michael A. Be
'''

2 GPT2Config

  • 用于存储 GPT2Model配置的配置类。
  • 根据指定的参数实例化一个 GPT-2 模型,定义模型架构
  • 使用默认值实例化配置将产生类似于 GPT-2 openai-community/gpt2 架构的配置:

2.1 主要参数

vocab_size (int, 可选,默认值为 50257) — GPT-2 模型的词汇表大小
n_positions (int, 可选,默认值为 1024) — 该模型可能使用的最大序列长度。
n_embd (int, 可选,默认值为 768) — 嵌入和隐藏状态的维度
n_layer (int, 可选,默认值为 12) — Transformer 编码器中的隐藏层数量
n_head (int, 可选,默认值为 12) — Transformer 编码器中每个注意力层的注意力头数量
n_inner (int, 可选) — 内部前馈层的维度。设置为 None 将其设置为 4 * n_embd
activation_function

(str, 可选,默认值为 "gelu_new") — 激活函数

可在以下列表中选择 ["relu", "silu", "gelu", "tanh", "gelu_new"]

resid_pdrop

(float, 可选,默认值为 0.1) —

嵌入、编码器和池化器中所有全连接层的丢弃概率

embd_pdrop (float, 可选,默认值为 0.1) — 嵌入的丢弃率
attn_pdrop (float, 可选,默认值为 0.1) — 注意力的丢弃率
layer_norm_epsilon (float, 可选,默认值为 1e-05) — 层归一化层中使用的epsilon值
initializer_range (float, 可选,默认值为 0.02) — 初始化所有权重矩阵时截断正态初始化器的标准差
scale_attn_weights (bool, 可选,默认值为 True) — 通过除以 sqrt(hidden_size) 来缩放注意力权重
bos_token_id (int, 可选,默认值为 50256) — 词汇表中句子起始标记的 ID
eos_token_id (int, 可选,默认值为 50256) — 词汇表中句子结束标记的 ID
scale_attn_by_inverse_layer_idx (bool, 可选,默认值为 False) — 是否另外通过 1 / (layer_idx + 1) 缩放注意力权重

2.2 举例

from transformers import GPT2Config, GPT2Model

# 初始化 GPT2 配置
configuration = GPT2Config()


configuration
'''
GPT2Config {
  "activation_function": "gelu_new",
  "attn_pdrop": 0.1,
  "bos_token_id": 50256,
  "embd_pdrop": 0.1,
  "eos_token_id": 50256,
  "initializer_range": 0.02,
  "layer_norm_epsilon": 1e-05,
  "model_type": "gpt2",
  "n_embd"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UQI-LIUWJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值