当前位置: 首页 > ds >正文

【NLP】27. 语言模型训练以及模型选择:从预训练到下游任务

语言模型训练:从预训练到下游任务

本文详细讲解大型语言模型(LLMs)是如何训练的,包括不同的模型类型(Encoder、Decoder、Encoder-Decoder),以及各类预训练任务的原理、对比、适用场景,帮助你构建完整的语言建模理解体系。


一、三种主流语言模型结构

语言模型(LLMs)主要分为三种结构,每种结构的训练方式、能力边界、应用场景均有所不同:

类型代表模型输入处理输出形式典型用途
编码器(Encoder)BERT输入整句(遮掩词)词级表示向量NER、分类、匹配等
解码器(Decoder)GPT输入前文自回归生成后文生成、续写、对话等
编码-解码结构T5、BART编码整句 → 解码目标文本对到文本对翻译、问答、摘要等

**注:**目前主流大模型如 GPT-4、Claude 多为 decoder-only 结构。


二、训练语言模型的基本思想

无论是哪类模型,训练过程都遵循一条核心路径:

给定原始文本,稍作修改(如遮盖、替换、删除),训练模型去“恢复或识别修改处”。

这种方式不仅可以学习词与词之间的语义关系,还能促使模型理解上下文和结构。


三、常见的训练任务详解与对比

1️⃣ Next Token Prediction(下一个词预测)

  • 用于: Decoder 模型(如 GPT)

  • 机制: 给定文本开头,预测下一个词

  • 建模目标:

y ^ t = arg ⁡ max ⁡ P ( y t ∣ y < t ) \hat{y}_{t} = \arg\max P(y_t \mid y_{<t}) y^t=argmaxP(yty<t)

  • 例子:
    输入:The weather is → 输出:sunny

  • 优点: 能处理文本生成任务

  • 缺点: 单向上下文,只能看到前文


2️⃣ Masked Token Prediction(遮盖词预测)

  • 用于: Encoder 模型(如 BERT)

  • 机制: 输入句子中随机遮盖若干 token,模型预测遮盖位置的原始词

  • 例子:

    输入:The capital of France is [MASK]
    输出:Paris
    
  • 注意: 非遮盖词位置不参与 loss 计算

  • 对比 GPT: 能看到前后文(双向上下文),但不能用于生成任务


3️⃣ Span Prediction(SpanBERT)

  • 区别: 遮盖连续多个词(而不是单词)

  • 目的: 强化模型处理片段(span)的能力,更适合问答等任务

  • 例子:

    输入:Chocolate is [MASK] [MASK] [MASK] good
    输出:incredibly irresistibly tasty
    
  • 难度更高 → 更强能力


4️⃣ Random Token Correction(BERT)

  • 机制: 随机替换句子中的词,模型需判断哪个词错了

  • 例子:

    I like MLP (原来是NLP)
    输出:发现 MLP 是错的
    
  • 挑战: 模型要全局理解文本含义,避免仅依靠表层词频


5️⃣ Token Edit Detection(ELECTRA)

  • 流程:

    • 由小型 generator 替换部分 token(伪造)
    • 判别器判断每个 token 是否被替换
  • 优点: 所有 token 都参与训练(比 BERT 更高效)

  • 训练目标:

    Output i = { S , token 是原始 E , token 是生成 \text{Output}_i = \begin{cases} S, & \text{token 是原始} \\ E, & \text{token 是生成} \end{cases} Outputi={S,E,token 是原始token 是生成


6️⃣ Combination(BERT 扩展任务)

  • 整合多种任务:Masked + Replacement + 原文保留
  • 效果: 模型能更全面学习语义结构、对抗扰动

7️⃣ Next Sentence Prediction(BERT)

  • 机制: 判断两句话是否为上下文连续

  • 例子:

    A: I like NLP
    B: I like MLP	
    输出:是否 Next Sentence?
    
  • 后续研究发现: 该任务效果有限,RoBERTa 移除该任务后表现反而更好


四、Encoder-Decoder 专属训练任务(如 T5、BART)

✅ 1. Masked Sequence Prediction(遮盖词预测)

  • 输入

    I attended [MASK] at [MASK]
    
  • 输出目标

    I attended a workshop at Google
    

✅ 2. Deleted Sequence Prediction(删除预测)

  • 输入

    I watched yesterday
    
  • 输出目标

    I watched a movie on Netflix yesterday
    
  • 错误答案示例(模型需要避免)

    I watched a presentation at work yesterday
    

✅ 3. Deleted Span Prediction(删除片段预测)

  • 输入

    She submitted the assignment
    
  • 输出目标(分段补全)

    <X>: yesterday evening, <Y>: on Canvas
    

✅ 4. Permuted Sequence Prediction(打乱顺序重构)

  • 输入

    Netflix the on movie watched I
    
  • 输出目标

    I watched the movie on Netflix
    

✅ 5. Rotated Sequence Prediction(旋转预测)

  • 输入

    the conference in I presented paper a
    
  • 输出目标

    I presented a paper in the conference
    

✅ 6. Infilling Prediction(间隙填空)

  • 输入

    She [MASK] the [MASK] before [MASK]
    
  • 输出目标

    She completed the report before midnight
    

Encoder-Decoder 架构能更灵活地处理“输入 → 输出”任务,适合做结构性转换。其训练任务也更具多样性:


任务类型模型示例输入输出(目标)
Masked SequenceBARTI submitted [MASK] to [MASK]I submitted the report to my supervisor
Deleted SequenceBARTI submitted to my supervisorI submitted the report to my supervisor
Span MaskT5I submitted to : the report, : my supervisor
Permuted SequenceBARTMy supervisor to the report submitted II submitted the report to my supervisor
Rotated SequenceBARTTo my supervisor I submitted the reportI submitted the report to my supervisor
Infilling PredictionBARTI [MASK] the [MASK] to [MASK]I submitted the report to my supervisor

这些任务强化了模型处理不定结构输入的能力,提升其在翻译、摘要等任务中的泛化表现。


五、预训练 vs 微调:对比分析

对比维度预训练(Pre-training)微调(Fine-tuning)
执行频率只做一次每个任务可单独训练一次
训练时间较长(几周/月)可长可短
计算资源通常需大规模 GPU(集群)可在小规模 GPU 运行
数据来源原始文本(如 Wikipedia, BooksCorpus)任务特定数据(分类、问答等)
学习目标通用语言理解(语义、上下文、关系)针对具体任务性能最优

六、如何使用语言模型做任务?

模式一:使用语言模型作为特征提取器

  • 提取词向量或句向量 → 输入到后续任务模型
  • 类似于早期使用 Word2Vec 或 GloVe 向量
  • BERT 特别适合这种方式

模式二:将任务直接表述为语言建模

  • 直接将任务转为“生成”问题

  • GPT 类型模型常用此方式

  • 示例:

    问答:Q: Who discovered gravity? A: → Isaac Newton
    翻译:Translate: Hello → Bonjour
    

任务结构对比分析

📘 情感分析任务(模拟课程评价)

模型结构输入内容输出内容
EncoderThe student submitted the assignment on time. Rating: [MASK]5
DecoderThe student submitted the assignment on time. Rating:5
Enc-DecThe student submitted the assignment on time. Rating: : 5

📘 命名实体识别(NER)

模型结构输入内容输出内容
EncoderThe student submitted the assignment on time[O, O, O, O, B-TASK, O, B-TIME]
DecoderThe student submitted the assignment on timeassignment: Task, on time: Time
Enc-DecThe student submitted the assignment on timeassignment → Task on time → Time

📘 共指消解(改写句子以引入代词)

例句:The student told the lecturer that he was late.

模型结构输入内容输出内容
EncoderThe student told the lecturer that he was latehe → student / lecturer(需结构化解码)
DecoderThe student told the lecturer that he was late“he” refers to the student
Enc-DecThe student told the lecturer that he was latehe → student

📘 文本摘要(扩展长句 → 摘要)

长句:The student, after days of hard work and late nights, finally submitted the assignment well before the deadline.

模型结构输入内容输出内容
DecoderThe student, after days of hard work…The student submitted early
Enc-DecSame as aboveSubmitted the assignment

📘 翻译任务(英 → 法)

模型结构输入内容输出内容
DecoderTranslate: The student submitted the assignment on time.L’étudiant a rendu le devoir à temps.
Enc-DecTranslate English to French: The student submitted…L’étudiant a rendu le devoir à temps.

模型结构与任务适配总结

任务类型Encoder (如 BERT)Decoder (如 GPT)Encoder-Decoder (如 T5, BART)
分类/回归✅ 非常适合✅ 也可建模✅ 灵活,适合文本→标签结构
实体识别✅ 标准做法(token 分类)⚠️ 需序列生成✅ 可做 span 生成
共指消解⚠️ 通常需外部处理⚠️ 表达模糊✅ 可结构化生成
摘要❌ 不能生成✅ 具备能力✅ 最适合(输入输出解耦)
翻译❌ 无法实现✅ 可做(Prompt式)✅ 最佳(标准 Encoder-Decoder 应用)

七、总结与延伸

  1. 训练语言模型的核心在于构造“预测式任务”:不论是预测下一个词、恢复遮盖词,还是判断伪造 token,本质上都在训练模型理解语言结构与上下文。
  2. 模型结构决定其能力边界:Encoder 适合理解类任务,Decoder 擅长生成任务,而 Encoder-Decoder 灵活兼容。
  3. 预训练提供通用语义能力,微调实现任务定制:这是现代 NLP 模型最核心的工作流。
http://www.xdnf.cn/news/4226.html

相关文章:

  • RAG知识库只是表面简单!
  • Kubernetes排错(七)-节点排错
  • 除了java.nio.file.StandardCopyOption,还有哪些类可以实现文件的复制和移动?
  • C++动态库和静态库的生成和使用
  • linux crash工具详解
  • android-ndk开发(1): 搭建环境
  • 星途-(4)
  • 关于Python:9. 深入理解Python运行机制
  • DeepSeek技术发展详细时间轴与技术核心解析
  • ARM子程序调用与返回
  • vscode运行python的快捷键
  • VirtualBox调整虚拟机内存和CPU
  • 信息系统项目管理师-软考高级(软考高项)​​​​​​​​​​​2025最新(八)
  • 智能体四项关键技术:MCP、A2A、ANP与函数调用的深度解析
  • 判断字符是否唯一 --- 位运算
  • 《冰雪三职业》:战士玩法攻略!
  • 精益数据分析(39/126):SaaS与移动应用商业模式的关键要点剖析
  • P6822 [PA 2012 Finals] Tax 题解
  • 【项目】基于ArkTS的网吧会员应用开发(2)
  • Qt天气预报系统更新UI界面
  • ansible基础-优化
  • 代码随想录算法训练营day9:字符串part02
  • 英伟达开源英语自动语音识别模型:nvidia/parakeet-tdt-0.6b-v2
  • android zxing QrCode 库集成转竖屏适配问题
  • 餐具瓷器品牌十大排名
  • Linux安装RTL8215网卡驱动
  • FreeRTOS系统CPU使用率统计
  • AutoGPT
  • GESP2024年3月认证C++八级( 第二部分判断题(6-10))
  • 柯西乘积定理(Cauchy Product Theorem)