Skip to Content
PyTorch 口袋参考手册
book

PyTorch 口袋参考手册

by Joe Papa
May 2025
Intermediate to advanced
310 pages
3h 16m
Chinese
O'Reilly Media, Inc.
Content preview from PyTorch 口袋参考手册

第 4 章 神经网络开发参考设计 神经网络开发参考设计

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

在上一章中,我们从高层次上介绍了 NN 的开发过程,您也学会了如何在 PyTorch 中实现每个阶段。该章中的示例侧重于使用 CIFAR-10 数据集和一个简单的全连接网络解决图像分类问题。CIFAR-10 图像分类是说明 NN 开发过程的一个很好的学术例子,但用 PyTorch 开发深度学习模型还有很多内容。

本章将介绍一些使用 PyTorch 进行 NN 开发的附加参考设计。参考设计是代码示例,您可以将其作为解决类似问题的参考。

事实上,本章中的一组参考设计仅仅触及了 Deep Learning 可能性的表面;不过,我将尝试为你提供足够的多样性,以帮助你开发自己的解决方案。我们将使用三个示例来处理各种数据,设计不同的模型架构,并探索学习过程中的其他方法。

第一个例子使用 PyTorch 进行迁移学习,用一个小数据集和一个预训练网络对蜜蜂和蚂蚁的图像进行分类。第二个示例使用 PyTorch 利用文本数据进行情感分析,训练一个 NLP 模型,预测电影评论的正面或负面情感。第三个示例使用 PyTorch 演示生成式学习,训练一个生成式对抗网络(GAN)来生成衣物图像。

在每个示例中,我都会提供 PyTorch 代码,这样你在为自己的设计编写代码时,就可以将本章作为快速参考。首先,让我们来看看 PyTorch 如何利用迁移学习解决计算机视觉问题。

利用迁移学习进行图像分类

图像分类这一主题已被深入研究,许多著名的模型,如我们之前看到的 AlexNet 和 VGG 模型,都可以通过 PyTorch 轻松获得。不过,这些模型都是用 ImageNet 数据集训练的。虽然 ImageNet 包含 1000 种不同的图像类别,但它可能并不包含你解决图像分类问题所需的类别。

在这种情况下,您可以应用迁移学习,在这个过程中,我们会使用小得多的新图像数据集对预先训练好的模型进行微调。在下一个示例中,我们将训练一个模型来对 ImageNet 中未包含的蜜蜂和蚂蚁图像进行分类。蜜蜂和蚂蚁看起来非常相似,很难区分。

为了训练我们的新分类器,我们将微调另一个著名的模型 ResNet18,方法是加载预训练模型,并用 120 张蜜蜂和蚂蚁的新训练图像对其进行训练--与ImageNet中的数百万张图像相比,这组图像要小得多。

数据处理

让我们从加载数据、定义转换和配置批量采样的数据加载器开始。与之前一样,我们将利用 Torchvision 库中的函数来创建数据集、加载数据和应用数据转换。

首先,让我们导入本示例所需的库:

import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import torchvision
from torchvision import datasets, models
from torchvision import transforms

然后,我们将下载用于训练和验证的数据:

from io import BytesIO
from urllib.request import urlopen
from zipfile import ZipFile

zipurl = 'https://pytorch.tips/bee-zip'
with urlopen(zipurl) as zipresp ...
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实现AI量化交易

金融人工智能:用Python实现AI量化交易

Yves Hilpisch
容器安全

容器安全

Liz Rice

Publisher Resources

ISBN: 9798341658790