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 编码

第 16 章 使用自定义数据的 LLM 将 LLMs 与自定义数据结合使用

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

第 15 章中,我们了解了变换器及其编码器、解码器和编码器-解码器架构的工作原理。它们为 NLP 带来的革命性成果毋庸置疑!然后,我们看了 Transformers,它构成了来自 Hugging Face 的 Python 库,旨在使 Transformers 的使用更加方便。

基于 Transformer 的大型模型是在海量文本中训练出来的,非常强大,但它们并不总是特定任务或领域的理想选择。在本章中,我们将介绍如何使用变换器和其他 API 来调整这些模型,以满足您的特定需求。

通过微调,您可以使用特定数据自定义预训练模型。您可以使用这种方法创建聊天机器人,提高分类准确性,或为更具体的领域开发文本生成功能。

有几种技术可以做到这一点,包括传统的微调,以及使用 LoRA 和参数效率微调 (PEFT) 等方法进行参数效率微调。您还可以通过检索增强生成(RAG)从 LLMs 中获取更多信息,我们将在第 18 章对此进行探讨。

在本章中,我们将从传统的微调开始,探讨一些实践案例。

微调 LLM

让我们一步步来看看 如何微调像 BERT 这样的 LLM。我们将利用 IMDb 数据库,对模型进行微调,以便更好地检测电影评论中的情感。这其中有许多步骤,我们将逐一详细介绍。

设置和依赖关系

我们将首先设置使用 PyTorch 进行微调所需的一切 。除了基本的东西,你还需要包含三样新东西:

数据集

我们在第 4 章中介绍了数据集。我们将使用这些数据集加载 IMDb 数据集和内置的拆分数据集,用于训练和测试。

评估

该库提供了衡量加载性能的指标。

转换器

正如我们在第 14章和第 15 章中介绍的那样,变压器拥抱脸库旨在让 LLMs 的使用变得更加简单。

在本章的微调练习中,我们将使用 Hugging Face 变换器库中的一些类。这些类包括以下内容:

序列分类自动模型

该类可加载分类任务的预训练模型,并在基础模型的顶部添加分类头。然后,这个分类头会针对你要微调的特定分类场景进行优化,而不是一个通用模型。如果我们指定了检查点名称,它就会自动为我们处理模型架构。因此,要使用带有线性分类器层的 BERT 模型,我们将使用bert-base-uncased

自动代词生成器

该类会自动初始化相应的标记化器。它会将文本转换为适当的标记,并添加适当的特殊标记、填充、截断等。

训练参数

通过该类,我们可以配置训练设置和所有超参数,并设置要使用的设备等。

训练器

该类代表你管理训练循环,处理批处理、优化、损失、反向传播以及重新训练模型所需的一切。

带边框的数据整理器

数据集中的记录数并不总是与批量大小一致。因此,该类可以根据适当的批次大小有效地批处理示例,同时还能处理注意力掩码和其他模型特定输入等细节。

我们可以在代码中看到这一点:

# 1. Setup and Dependencies
import torch
from datasets import load_dataset
from transformers import (
    AutoModelForSequenceClassification,
    AutoTokenizer,
    TrainingArguments,
    Trainer,
    DataCollatorWithPadding ...
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