Lecture 07: BERT 与评测基准
核心主题:GLUE/SuperGLUE 基准、BERT vs GPT zero-shot 评测、HLE、ARC-AGI-3
1. 模型架构分类
| 架构类型 |
代表模型 |
预训练目标 |
特点 |
| Encoder-only |
BERT |
MLM + NSP |
双向注意力,擅长理解任务 |
| Decoder-only |
GPT |
Next-token prediction |
因果注意力,擅长生成任务 |
| Encoder-Decoder |
T5 |
Span corruption |
双向编码 + 自回归解码 |
MLM (Masked Language Model):随机遮盖 15% token,预测被遮盖的 token。
NSP (Next Sentence Prediction):判断两句是否相邻。
2. Zero-Shot 评测策略
2.1 BERT — Fill-Mask Scoring
模板:
# Template: "{sentence} It was [MASK]."
# Compare P("great") vs P("terrible") for sentiment
from transformers import pipeline
unmasker = pipeline("fill-mask", model="bert-base-uncased")
text = "This movie was amazing. It was [MASK]."
results = unmasker(text)
# 比较 "great" 和 "terrible" 的概率
局限性:BERT 设计用于 fine-tuning,而非 zero-shot 推理。Fill-Mask 方式只能利用单个 [MASK] 位置的概率分布,表达能力受限。
2.2 GPT-2 — Likelihood Scoring
核心思路:计算每个候选标签的条件对数概率,选择最高者。
$$\text{score}(label) = \log P(label\_tokens \mid prompt) = \sum_{t} \log P(w_t \mid w_{<t}, prompt)$$
import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer
model = GPT2LMHeadModel.from_pretrained("gpt2")
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
def score_sequence(prompt, label):
"""计算 label 在 prompt 条件下的对数概率"""
input_text = prompt + " " + label
input_ids = tokenizer.encode(input_text, return_tensors="pt")
prompt_len = len(tokenizer.encode(prompt))
with torch.no_grad():
outputs = model(input_ids)
logits = outputs.logits # (1, seq_len, vocab_size)
log_probs = torch.log_softmax(logits, dim=-1)
# 只计算 label tokens 的对数概率
label_log_prob = 0.0
for i in range(prompt_len - 1, input_ids.shape[1] - 1):
next_token = input_ids[0, i + 1]
label_log_prob += log_probs[0, i, next_token].item()
return label_log_prob
2.3 Instruction-tuned (Qwen3) — Chat Format
核心思路:利用 chat template 格式化输入,比较 label token 的 logits。
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "Qwen/Qwen3-0.6B"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
messages = [
{"role": "user", "content": "Classify sentiment: 'This movie is great.' Answer: positive or negative?"}
]
input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
input_ids = tokenizer(input_text, return_tensors="pt").input_ids
with torch.no_grad():
logits = model(input_ids).logits[0, -1, :] # 最后一个位置
# 比较 "positive" 和 "negative" 对应 token 的 logit
pos_id = tokenizer.encode("positive", add_special_tokens=False)[0]
neg_id = tokenizer.encode("negative", add_special_tokens=False)[0]
prediction = "positive" if logits[pos_id] > logits[neg_id] else "negative"
3. GLUE 基准详解 ⭐
GLUE (General Language Understanding Evaluation) 是 NLU 评测的标准基准,包含 9 个子任务。
3.1 单句任务 (Single-Sentence Tasks)
| 任务 |
全称 |
类型 |
指标 |
描述 |
| CoLA |
Corpus of Linguistic Acceptability |
二分类 |
Matthews Corr. |
句子语法是否可接受 |
| SST-2 |
Stanford Sentiment Treebank |
二分类 |
Accuracy |
电影评论情感(正/负) |
3.2 相似/释义任务 (Similarity & Paraphrase Tasks)
| 任务 |
全称 |
类型 |
指标 |
描述 |
| MRPC |
Microsoft Research Paraphrase Corpus |
二分类 |
Acc / F1 |
两句是否语义等价 |
| STS-B |
Semantic Textual Similarity Benchmark |
回归 |
Pearson/Spearman |
语义相似度 (1-5 分) |
| QQP |
Quora Question Pairs |
二分类 |
Acc / F1 |
两个问题是否等价 |
3.3 推理任务 (Inference Tasks)
| 任务 |
全称 |
类型 |
指标 |
描述 |
| MNLI |
Multi-Genre NLI |
三分类 |
Accuracy |
文本蕴含 (entailment/contradiction/neutral) |
| QNLI |
Question NLI |
二分类 |
Accuracy |
句子是否包含问题的答案 |
| RTE |
Recognizing Textual Entailment |
二分类 |
Accuracy |
文本蕴含(二分类版) |
| WNLI |
Winograd NLI |
二分类 |
Accuracy |
代词消解 + 蕴含 |
4. Zero-Shot 实验结果 ⭐⭐
4.1 SST-2 (情感分类)
| 模型 |
类型 |
Accuracy |
备注 |
| bert-base-uncased |
Encoder-only |
0.565 |
接近随机 |
| bert-large-uncased |
Encoder-only |
0.580 |
Scale 几乎无帮助 |
| gpt2 |
Decoder-only |
0.725 |
较好 |
| Qwen3-0.6B |
Decoder (IT) |
0.760 |
Instruction tuning 有效 |
| Qwen3-1.7B |
Decoder (IT) |
0.880 |
Scale + IT = 强 zero-shot |
4.2 MRPC (释义检测)
| 模型 |
Accuracy |
F1 |
备注 |
| bert-base-uncased |
0.505 |
0.520 |
~随机 |
| bert-large-uncased |
0.510 |
0.525 |
~随机 |
| gpt2 |
0.585 |
0.610 |
略高于随机 |
| Qwen3-0.6B |
0.650 |
0.680 |
- |
| Qwen3-1.7B |
0.740 |
0.770 |
最佳 |
4.3 RTE (文本蕴含)
| 模型 |
Accuracy |
备注 |
| bert-base-uncased |
0.505 |
~随机 |
| bert-large-uncased |
0.515 |
~随机 |
| gpt2 |
0.540 |
微弱信号 |
| Qwen3-0.6B |
0.620 |
- |
| Qwen3-1.7B |
0.720 |
明显超越 |
4.4 MNLI (多体裁自然语言推理)
| 模型 |
Accuracy |
Macro-F1 |
备注 |
| bert-base-uncased |
0.340 |
0.320 |
~随机 (1/3) |
| bert-large-uncased |
0.350 |
0.330 |
~随机 |
| gpt2 |
0.380 |
0.360 |
略好 |
| Qwen3-0.6B |
0.520 |
0.500 |
- |
| Qwen3-1.7B |
0.650 |
0.630 |
最佳 |
5. 关键发现
5.1 BERT 在 Zero-Shot 下几乎无用
- 所有任务接近随机水平 (binary ~0.5, ternary ~0.33)
- Scale (base → large) 几乎不改善 zero-shot 性能
- 原因:BERT 的 MLM 预训练目标与下游分类任务不直接对齐
5.2 Instruction Tuning + Scale = 强 Zero-Shot
- Qwen3-1.7B 在所有任务上显著超越其他模型
- Instruction tuning 提供了任务理解能力
- Scale 放大了这种能力
5.3 架构适用性
| 场景 |
最适合架构 |
原因 |
| Zero-shot 分类 |
Decoder-only (IT) |
生成式范式天然适配 |
| Fine-tuning 分类 |
Encoder-only |
双向上下文,表示更丰富 |
| Seq2Seq 任务 |
Encoder-Decoder |
翻译、摘要等结构化输出 |
| 通用 Agent |
Decoder-only (IT) |
指令跟随 + 长文本生成 |
6. HLE (Humanity's Last Exam)
6.1 基本信息
- 规模:约 2500 道题目
- 难度:PhD 级别,跨学科
- 目标:测试 AI 在人类最难学术问题上的能力
- 格式:多选题 + 开放问答
6.2 难度表现
| 模型规模 |
准确率 |
说明 |
| 小模型 (<10B) |
~0% |
完全无法回答 |
| GPT-4 |
~5-10% |
仅略好于随机 |
| 人类专家 |
高 (各自领域) |
题目由领域专家出题 |
6.3 评测格式
7. ARC-AGI-3
7.1 基本信息
- 类型:交互式推理基准 (Interactive Benchmark)
- 设计:测试抽象推理与模式归纳能力
- 格式:给定输入-输出网格对,推断变换规则并应用到新输入
- 特点:每道题的规则都是全新的,无法靠记忆解决
7.2 实验结果
| 模型 |
Score |
备注 |
| bert-base |
0.0 |
无法处理 |
| gpt2 |
0.0 |
无法处理 |
| Qwen3-0.6B |
0.0 |
无法处理 |
| Qwen3-1.7B |
0.0 |
无法处理 |
结论:真正的抽象推理 (true reasoning) 仍然是开放问题。当前 LLM(无论规模和训练方式)在需要归纳全新规则的任务上完全失败。这表明 pattern matching ≠ reasoning。
8. 基准难度层级
┌─────────────────────────────────────────────────────────────────┐
│ NLU 基准难度层级 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ GLUE → SuperGLUE → HLE → ARC-AGI-3│
│ (基础 NLU) (更难 NLU) (PhD 级别) (抽象推理)│
│ │
│ 已饱和 接近饱和 GPT-4 ~5% 所有模型 0%│
│ BERT fine-tune 需要更大模型 极其困难 未解决 │
│ 可达 ~90%+ ~90%+ │
│ │
│ ◀──────────── 难度递增 ──────────────────────────────────────▶ │
│ ◀──────────── 从语言理解 → 深度推理 ────────────────────────▶ │
└─────────────────────────────────────────────────────────────────┘
启示:随着模型能力提升,需要不断设计更难的基准来区分模型。GLUE 已经无法区分现代 LLM 的能力差异。
9. 评测方法论
9.1 指标选择
| 任务类型 |
推荐指标 |
适用场景 |
| 平衡二分类 |
Accuracy |
SST-2(正负样本均衡) |
| 不平衡二分类 |
F1 / Acc+F1 |
MRPC, QQP(正例偏多) |
| 多分类 |
Macro-F1 |
MNLI(三类可能不均衡) |
| 回归 |
Pearson / Spearman |
STS-B(连续分数) |
| 语法判断 |
Matthews Corr. |
CoLA(极度不平衡) |
9.2 Likelihood Scoring 完整实现
import torch
import torch.nn.functional as F
from transformers import AutoModelForCausalLM, AutoTokenizer
def likelihood_scoring(model, tokenizer, prompt, candidates):
"""
对候选标签进行 likelihood scoring
Args:
model: 语言模型
tokenizer: 分词器
prompt: 输入文本
candidates: 候选标签列表, e.g. ["positive", "negative"]
Returns:
best_candidate: 得分最高的候选标签
scores: 所有候选的分数字典
"""
scores = {}
for candidate in candidates:
# 拼接 prompt 和候选标签
full_text = prompt + " " + candidate
input_ids = tokenizer.encode(full_text, return_tensors="pt")
prompt_ids = tokenizer.encode(prompt, return_tensors="pt")
prompt_len = prompt_ids.shape[1]
with torch.no_grad():
outputs = model(input_ids)
logits = outputs.logits # (1, seq_len, vocab_size)
# 计算 label tokens 的平均对数概率
log_probs = F.log_softmax(logits, dim=-1)
total_log_prob = 0.0
num_tokens = 0
for i in range(prompt_len - 1, input_ids.shape[1] - 1):
next_token_id = input_ids[0, i + 1]
total_log_prob += log_probs[0, i, next_token_id].item()
num_tokens += 1
# 长度归一化
scores[candidate] = total_log_prob / num_tokens if num_tokens > 0 else float('-inf')
best_candidate = max(scores, key=scores.get)
return best_candidate, scores
# 使用示例
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
prompt = "The movie was fantastic. The sentiment is"
candidates = ["positive", "negative"]
prediction, scores = likelihood_scoring(model, tokenizer, prompt, candidates)
print(f"Prediction: {prediction}")
print(f"Scores: {scores}")
核心要点总结
- 架构决定评测方式:Encoder-only (fill-mask) vs Decoder-only (likelihood scoring) vs Instruction-tuned (chat format),三种评测策略截然不同
- BERT 不适合 zero-shot:MLM 预训练目标与下游分类不对齐,zero-shot 性能接近随机;BERT 的价值在于 fine-tuning
- Instruction Tuning 是关键:同等规模下,instruction-tuned 模型的 zero-shot 能力远超 base 模型(Qwen3-0.6B IT > GPT-2 base)
- Scale + IT 双重加持:Qwen3-1.7B 在所有 GLUE 子任务上展现出强 zero-shot 能力,证明规模与对齐的协同效应
- 基准持续升级:GLUE → SuperGLUE → HLE → ARC-AGI-3,随着模型进步,评测标准同步提高;GLUE 已饱和
- 抽象推理仍未解决:所有测试模型在 ARC-AGI-3 上得分为 0,表明当前 LLM 的能力边界 — pattern matching ≠ true reasoning