Skip to Content
基于scikit-learn和PyTorch的实践机器学习
book

基于scikit-learn和PyTorch的实践机器学习

by Aurélien Géron
October 2025
Intermediate to advanced
878 pages
12h 53m
Chinese
O'Reilly Media, Inc.
Content preview from 基于scikit-learn和PyTorch的实践机器学习

第 15 章 自然语言处理和聊天机器人的变形器

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在 2017 年发表的一篇具有里程碑意义的论文 "Attention Is All You Need "中。1谷歌研究人员团队提出了一种名为Transformer 的新型神经网络架构 ,该架构显著改善了神经机器翻译(NMT) 的技术水平。简而言之,Transformer 架构只是一个编码器-解码器模型,与我们在第 14 章中构建的英西互译模型非常相似,其使用方式也完全相同(见图 15-1):

  1. 源文本进入编码器,编码器输出上下文嵌入(每个标记一个)。

  2. 然后,编码器的输出与迄今为止翻译的文本(从序列起始标记开始)一起输入解码器。

  3. 解码器会预测每个输入标记的下一个标记。

  4. 解码器输出的最后一个标记被附加到译文中。

  5. 重复步骤 2 到步骤 4,一次一个额外的标记,直到生成一个序列结束标记,从而生成完整的译文。在训练过程中,我们已经有了完整的译文--它就是目标语--因此在步骤 2 中就将其输入解码器(从序列起始标记开始),而不需要步骤 4 和 5。

Diagram illustrating the Transformer model's process for translating English to Spanish, showing how the encoder generates contextual embeddings and the decoder predicts the next token in the translated sequence.
图 15-1. 使用转换器模型进行英西互译

那么有什么新变化呢?在这个黑盒子里,与我们之前的编码器-解码器有一些重要的区别。最重要的是,Transformer 架构不包含任何递归层或卷积层,只是将普通的密集层与一种名为多头注意力(MHA)的新型注意力机制相结合 ,另外还有一些附加功能。2由于该模型不是递归模型,它不会像 RNN 那样受到梯度消失或爆炸问题的困扰,训练步骤更少,更容易在多个 GPU 上并行,而且扩展性出奇地好。此外,得益于多头注意力,该模型能比 RNN 更好地捕捉长程模式。

Transformer 架构的用途也非常广泛。它最初是为 NMT 而设计的,但研究人员很快对该架构进行了调整,使其适用于许多其他语言任务。2018 年甚至被称为 "NLP 的 ImageNet 时刻"。2018 年 6 月,OpenAI 发布了首个 GPT 模型 ,它完全基于 Transformer 的解码器模块。它在大量文本语料库上进行了预训练,生成文本的能力前所未有,可以自动完成句子、编造故事,甚至回答一些问题。GPT 还可以进行微调,以执行各种语言任务。几个月后,谷歌发布了 BERT 模型,该模型完全基于 Transformer 的编码器模块。它在各种自然语言理解(NLU) ,如文本分类、文本嵌入、多选题回答或在某些文本中寻找问题答案等任务方面表现出色。

令人惊讶的是,变换器在计算机视觉、音频处理(如语音到文本)、机器人(使用传感器输入并将输出发送到执行器)等方面也表现出色。例如,如果将图像分割成小块,然后将它们送入变换器(而不是令牌嵌入),就能得到视觉变换器(ViT) 。事实上,有些转换器甚至可以同时处理多种模式(如文本 + 图像);这些 被称为多模式模型。

这种性能、灵活性和可扩展性的出色组合鼓励谷歌、OpenAI、Facebook(Meta)、微软、Anthropic 和许多其他组织训练越来越大的变换器模型。最初的变形金刚模型约有 6500 万个参数,这在当时被认为是相当大的参数,但新的变形金刚以令人难以置信的速度增长,到 2021 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

编写整洁的Python代码(第2版)

编写整洁的Python代码(第2版)

Posts & Telecom Press, Mariano Anaya
生成式人工智能可视化

生成式人工智能可视化

Priyanka Vergadia, Valliappa Lakshmanan
AI工程

AI工程

Chip Huyen

Publisher Resources

ISBN: 0642572270117