Skip to Content
PyTorch によるコーダーのための AI および ML
book

PyTorch によるコーダーのための AI および ML

by Laurence Moroney
July 2025
Intermediate to advanced
444 pages
7h 8m
Japanese
O'Reilly Media, Inc.
Content preview from PyTorch によるコーダーのための AI および ML

第10章 配列を予測するMLモデルの作成 シーケンスを予測するMLモデルの作成

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

第9章では、シーケンスデータと、季節性、トレンド、自己相関、ノイズを含む時系列の属性を紹介した。予測に使用する合成系列を作成し、基本的な統計的予測の方法を探った。

次の数章では、予測のためのMLの使い方を学ぶ。しかし、モデルの作成を始める前に、予測モデルをトレーニングするための時系列データをどのように構成するかを理解する必要がある

なぜそうする必要があるのかを理解するために、第9章で作成した時系列を考えてみよう。図10-1にそのプロットがある。

例えば、タイム・ステップ1,200における時系列の値を、それに先立つ30個の値の関数として予測したいとする。この場合、時間ステップ1,170~1,199の値が時間ステップ1,200の値を決定することになる(図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 ...
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

生成 Deep Learning ―絵を描き、物語や音楽を作り、ゲームをプレイする

生成 Deep Learning ―絵を描き、物語や音楽を作り、ゲームをプレイする

David Foster, 松田 晃一, 小沼 千絵
ダイナミックリチーミング 第2版 ―5つのパターンによる効果的なチーム編成

ダイナミックリチーミング 第2版 ―5つのパターンによる効果的なチーム編成

Heidi Helfand, 永瀬 美穂, 吉羽 龍太郎, 原田 騎郎, 細澤 あゆみ

Publisher Resources

ISBN: 9798341662636