在 Hugging Face 的 transformers
库中,BERT
模型的配置文件 config.json
定义了模型的架构、超参数及行为控制选项。这个配置文件是加载预训练模型的关键之一。以下是对 config.json
中所有常见参数的深度解释(基于 BertConfig
类),涵盖其作用和可能的调整影响。
🔧 BERT
配置参数详解
📌 基本结构参数
参数名 | 类型 | 默认值 | 说明 |
---|---|---|---|
hidden_size |
int | 768 | Transformer 中每层的隐藏状态维度。决定词向量维度。 |
num_attention_heads |
int | 12 | 每层中 multi-head attention 的头数,需能整除 hidden_size 。 |
num_hidden_layers |
int | 12 | Transformer Encoder 层的数量。 |
intermediate_size |
int | 3072 | Feedforward 中间层维度(一般为 hidden_size * 4 )。 |
hidden_act |
str | "gelu" |
前馈激活函数类型,可为 "gelu" , "relu" , "tanh" 等。 |
hidden_dropout_prob |
float | 0.1 | FFN 隐藏层的 Dropout 概率。 |
attention_probs_dropout_prob |
float | 0.1 | Attention 概率的 Dropout。 |
max_position_embeddings |
int | 512 | 最大支持的位置数。 |
type_vocab_size |
int | 2 | segment embeddings 的种类数(如 [CLS]A[SEP]B[SEP] 中 A、B)。 |
initializer_range |
float | 0.02 | 权重初始化时使用的正态分布标准差。 |
🧠 词表与输入相关参数
参数名 | 类型 | 默认值 | 说明 |
---|---|---|---|
vocab_size |
int | 30522 | 词表大小(决定输入 ID 的范围)。 |
pad_token_id |
int | 0 | Padding token 的 ID。 |
bos_token_id |
int | None | Beginning of sentence token(可选)。 |
eos_token_id |
int | None | End of sentence token(可选)。 |
🧮 位置和嵌入相关
参数名 | 类型 | 默认值 | 说明 |
---|---|---|---|
position_embedding_type |
str | "absolute" |
表示位置编码类型,可选:"absolute" 、"relative_key" 、"relative_key_query" 。 |
layer_norm_eps |
float | 1e-12 | LayerNorm 的 epsilon 值,避免除以 0。 |
⚙️ 编码器/解码器设置(与 EncoderDecoder 兼容)
参数名 | 类型 | 默认值 | 说明 |
---|---|---|---|
is_decoder |
bool | False | 是否作为解码器使用。 |
add_cross_attention |
bool | False | 是否添加 cross attention(当 is_decoder=True 时生效)。 |
📤 输出行为控制参数
参数名 | 类型 | 默认值 | 说明 |
---|---|---|---|
output_attentions |
bool | False | 是否输出每层的 attention 矩阵。 |
output_hidden_states |
bool | False | 是否输出每层的 hidden state。 |
use_cache |
bool | True | 解码器中是否缓存 key/value 以加速推理。 |
🧪 特殊用途参数(微调/蒸馏/调试)
参数名 | 类型 | 默认值 | 说明 |
---|---|---|---|
chunk_size_feed_forward |
int | 0 | 用于内存优化,将 FFN 分块处理。 |
tie_word_embeddings |
bool | True | 是否将输入嵌入与输出嵌入权重共享。 |
gradient_checkpointing |
bool | False | 启用后可节省显存但增加计算开销。 |
use_return_dict |
bool | True | 返回结构体还是 tuple。建议保持为 True。 |
torchscript |
bool | False | 是否用于 TorchScript 模型导出(已废弃)。 |
示例配置片段(BERT base)
python
{
"architectures": [
"BertForMaskedLM"
],
"attention_probs_dropout_prob": 0.1,
"directionality": "bidi",
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 768,
"initializer_range": 0.02,
"intermediate_size": 3072,
"layer_norm_eps": 1e-12,
"max_position_embeddings": 512,
"model_type": "bert",
"num_attention_heads": 12,
"num_hidden_layers": 12,
"pad_token_id": 0,
"pooler_fc_size": 768,
"pooler_num_attention_heads": 12,
"pooler_num_fc_layers": 3,
"pooler_size_per_head": 128,
"pooler_type": "first_token_transform",
"type_vocab_size": 2,
"vocab_size": 21128
}
总结
BERT 的 config.json
是其模型架构的详细描述,关键参数分为:
-
模型结构类参数(
hidden_size
,num_layers
,num_heads
) -
Dropout 与激活控制(
hidden_dropout_prob
,hidden_act
) -
嵌入与输入控制(
vocab_size
,max_position_embeddings
,type_vocab_size
) -
输出控制(
output_hidden_states
,use_cache
) -
实用增强(
gradient_checkpointing
,chunk_size_feed_forward
)