Skip to Content
PyTorch 中的 AI 和 ML 编码
book

PyTorch 中的 AI 和 ML 编码

by Laurence Moroney
July 2025
Beginner to intermediate
444 pages
6h 20m
Chinese
O'Reilly Media, Inc.
Content preview from PyTorch 中的 AI 和 ML 编码

第 5 章 自然语言处理入门 自然语言处理简介

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

自然语言处理(NLP)是人工智能 ,它涉及对语言的理解。它涉及编程技术,用于创建能够理解语言、对内容进行分类、甚至生成和创建新语言合成的模型。它也是 GPT、Gemini 和 Claude 等大型语言模型(LLMs)的底层基础。我们将在后面的章节中探讨 LLMs,但首先,我们将在接下来的几章中了解更基本的 NLP,以便为接下来的内容做好准备。

还有很多服务使用 NLP 来创建聊天机器人等应用,但这不在本书的讨论范围内--相反,我们将探讨 NLP 的基础以及如何对语言进行建模,从而训练神经网络来理解和分类文本。在后面的章节中,你还将学习如何使用 ML 模型的预测元素来写诗。这不仅仅是为了好玩--它也是学习如何使用基于变换器的模型的前奏,这种模型是生成式人工智能的基础!

本章一开始,我们将介绍如何将语言分解为数字,以及如何在神经网络中使用这些数字。

将语言编码为数字

归根结底,计算机处理的是数字, ,因此要处理语言,就需要在一个称为编码的过程中将其转换为数字

将语言编码成数字的方法有很多。最常见的是用字母编码,这是在程序中存储字符串时的自然做法。不过,在内存中存储的不是字母a,而是它的编码--可能是 ASCII 或 Unicode 值,也可能是其他编码。例如,请看单词listen。你可以用 ASCII 码将其编码为数字 76、73、83、84、69 和 78。这很好,因为您现在可以用数字来表示这个单词了。但再考虑一下silent 这个单词,它是listen 的变形。同样的数字代表这个单词,只是顺序不同,这可能会使建立一个理解文本的模型变得更加困难。

一个更好的选择可能是用数字来编码整个单词,而不是其中的字母。在这种情况下,silent可以是数字xlisten可以是数字y,而且它们不会相互重叠。

使用这种技术,可以考虑 "我爱我的狗 "这样的句子。你可以用数字 [1, 2, 3, 4] 对这句话进行编码。如果你想编码 "我爱我的猫",你可以用 [1, 2, 3, 5] 来编码。现在,你可能已经明白,这两个句子的意思相似,是因为它们的数字相似--换句话说,[1, 2, 3, 4]看起来很像[1, 2, 3, 5]。

代表单词的数字 也被称为标记,因此,这一过程被称为标记化。接下来我们将探讨如何用代码实现这一过程。

标记化入门

PyTorch 生态系统包含 许多用于标记化的库,标记化将单词转化为标记。你可能会在代码示例中看到一个常见的标记化器,那就是torchtext, ,但自 2023 年起,这个标记化器就已被弃用。因此,使用它时一定要小心,尤其是因为 PyTorch 版本在进步,但它却没有。因此,一些替代方案是使用自定义标记符、其他地方的预训练标记符,或者(令人惊讶的是)Keras 生态系统中的标记符。

使用自定义标记符

为了给大家提供一个简单的例子, 下面是我用来创建自定义标记符的一些代码,它可以将一个小型语料库(两个句子)中的单词转化为标记符:

import torch
 
sentences = [
    'Today is a sunny day',
    'Today is a rainy day'
]
 
# Tokenization function
def tokenize(text):
    return text.lower().split()
 
# Build the vocabulary ...
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

在企业中实施 MLOps

在企业中实施 MLOps

Yaron Haviv, Noah Gift
图解大模型 : 生成式AI 原理与实战

图解大模型 : 生成式AI 原理与实战

Jay Alammar, Maarten Grootendorst

Publisher Resources

ISBN: 9798341662599