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

第 10 章 创建 ML 模型预测序列

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

第 9 章介绍了序列数据和时间序列的属性,包括季节性、趋势、自相关性和噪声。你创建了一个用于预测的合成序列,并探索了如何进行基本的统计预测。

在接下来的几章中,你将学习如何使用 ML 进行预测。但在开始创建模型之前,您需要了解如何通过创建我们称之为Windows 的数据集来构建用于训练预测模型的时间序列 数据。

要理解为什么需要这样做,请参考第 9 章中创建的时间序列。您可以在图 10-1 中看到它的曲线图。

例如,您想预测时间序列在时间步长 1,200 时的值,并将其作为时间步长t 之前 30 个值的函数。在这种情况下,时间步骤 1170 至 1199 的值将决定时间步骤 1200 的值(见图 10-2)。

图 10-1. 合成时间序列
图 10-2. 影响预测的前值

现在,这看起来很熟悉了:您可以将 1,170 至 1,199 的值视为特征,将 1,200 的值视为标签。如果能将数据集设置为有一定数量的值作为特征,下面的值作为标签,并对数据集中的每个已知值都这样做,那么最终就会得到一组相当不错的特征和标签,可以用来训练模型。

在对第 9 章中的时间序列数据集进行训练之前,让我们先创建一个非常简单的数据集,它具有相同的属性,但数据量要小得多。

创建 Windows 数据集

PyTorch 有很多对操作数据很有用的 API。例如,您可以使用torch.arange(10) 创建一个包含 0-9 数字的基本数据集,从而模拟时间序列。然后,您可以将该数据集转化为窗口数据集的雏形。代码如下:

import torch
 
def create_sliding_windows(data, window_size, shift=1):
    # Convert input to tensor if it isn't already
    if not isinstance(data, torch.Tensor):
        data = torch.tensor(data)
 
    # Calculate number of valid windows
    n = len(data)
    num_windows = max(0, (n  window_size) // shift + 1)
 
    # Create strided view of data
    windows = data.unfold(0, window_size, shift)
 
    return windows
 
# Example usage:
data = torch.arange(10)
windows = create_sliding_windows(data, window_size=5, shift=1)
 
# Print each window
for window in windows:
    print(window.numpy())
 

首先,使用范围创建数据集,简单来说就是让数据集包含 ...

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