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

第 11 章 在序列模型中使用卷积和递归方法

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

前面几章向大家介绍了序列数据。你看到了如何预测数据,首先是使用统计方法,然后是使用深度神经网络的基本 ML 方法。你还探索了如何调整模型的超参数以获得更好的性能。

在本章中,你将学习使用卷积神经网络和递归神经网络来进一步提高序列数据预测能力的其他技术。

序列数据的卷积

第 3 章 中,我们向你介绍了卷积,即在图像上通过二维(2D)滤波器来修改图像并提取潜在特征。随着时间的推移,神经网络学会了哪些滤波器值能够有效地将像素所做的修改与其标签相匹配,从而有效地从图像中提取特征。同样的技术也可应用于数值时间序列数据,但有一点需要修改:卷积将是一维(1D)而非二维。

例如,考虑图 11-1 中的数字序列。

图 11-1. 数字序列

一维卷积的操作方法如下。将卷积视为 1 × 3 滤波器,滤波器值分别为 -0.5、1 和 -0.5。在这种情况下,序列中的第一个值将丢失,第二个值将从 8 变为-1.5(见图 11-2)。

图 11-2. 使用数字序列卷积

然后,滤波器将跨过这些值,一边计算新值,一边跨过这些值。例如,在下一次跨步中,15 将转换为 3(见图 11-3)。

图 11-3. 一维卷积中的附加跨距

使用这种方法,我们可以提取数值之间的模式,并学习成功提取这些模式的滤波器,这与对图像中的像素进行卷积提取特征的方法基本相同。在这种情况下,虽然没有标签,但可以学习到使整体损失最小的卷积。

卷积编码

在编码卷积之前, ,你需要使用滑动窗口技术创建一个数据集,如第 10 章所示。本书的 GitHub 页面提供了相关代码。

有了数据集之后,你就可以在之前的密集层之前添加一个卷积层。下面是代码,我们将逐行查看:

class CNN1D(nn.Module):
    def __init__(self, input_size):
        super(CNN1D, self).__init__()
        self.conv1 = nn.Conv1d(in_channels=1,
                              out_channels=128,
                              kernel_size=3,
                              padding=1)
 
        conv_output_size = input_size  # Same padding maintains input size
 
        self.relu = nn.ReLU()
        self.flatten = nn.Flatten()
        self.dense1 = nn.Linear(128 * conv_output_size, 28)
        self.dense2 = nn.Linear(28, 10)
        self.dense3 = nn.Linear(10, 
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