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

第12章 推論の概念 推論の概念

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

本書の前の章では、PyTorchを使ったモデルの学習と、画像(別名コンピュータビジョン)、テキストコンテンツ(別名NLP)、シーケンスモデリングを管理するモデルの作成方法に焦点を当てた。本書の残りの部分では、新しいデータから予測を行うために学習済みモデルを使用すること(別名推論)、特にテキストからテキストへ、テキストから画像への生成AIのために大規模な生成モデルを使用することについて、多くの内容をカバーする。

しかしその前に、基礎となるデータ転送技術を理解することが重要だ。トレーニングの章でも少し触れたが、トレーニングでも推論でも、MLに深く入っていくにつれて、テンソルの基礎となる概念を理解できるようになることが重要だ。

最終的には、どのようなデータ型を持っていても、それをテンソルに変換してモデルに渡すことになる。同様に、モデルからの答えをユーザに提示したいデータ型が何であれ、テンソルとして返すことになる!

多くの場合、第15章(LLMを扱う)で出てくるトランスフォーマーや第19章(画像生成を扱う)で出てくるディフューザーのようなヘルパー関数がある。これらの関数でテンソルを触ることはないだろうが、その裏ではテンソルを使うことになる。

テンソル

テンソルは 、任意の次元数を持つことができる配列である。テンソルは通常、Deep Learningアルゴリズムで数値データを表現するのに使われる。テンソルは、多次元の数値を保持できるコンテナだ。

テンソルは、単純なスカラー値(0次元)、ベクトル(1次元)、行列(2次元)、それ以上(3次元以上)になる。PyTorchでは、テンソルはすべての計算のための基本的なデータ構造である。

テンソルは、Googleの代替Deep LearningフレームワークであるTensorFlowの 名前の由来()でもある。

以下は、torch.​ten⁠sor を使って作成されたPyTorchのテンソルの例だ:

import torch
 
# Scalar (0D tensor)
scalar = torch.tensor(42)  # Single number
 
# Vector (1D tensor)
vector = torch.tensor([1, 2, 3, 4])  # Array of numbers
 
# Matrix (2D tensor)
matrix = torch.tensor([[1, 2, 3],
                      [4, 5, 6]])  # 2x3 grid of numbers
 
# 3D tensor
cube = torch.tensor([[[1, 2], [3, 4]],
                    [[5, 6], [7, 8]]])  # 2x2x2 cube of numbers

、テンソルがMLにとって非常に便利なのは、異なる値の型を保持できる柔軟性があるからだ。数値は0次元テンソル、テキストを表す埋め込みベクトルは1次元、画像は3次元で、高さ、幅、ピクセル値の次元を持つ。さらに、これらすべてに、バッチ用の追加次元を持たせることができる。つまり、例えば、1つの画像は3D行列になるが、100の3D行列の代わりに100の画像を1つの4Dテンソルにすることができ、4番目の次元は画像のインデックスになる!

torch.tensor 、GPU上で動作するように最適化するために多くの労力と投資が費やされており、ディープラーニングの計算に非常に効率的であることを覚えておいてほしい。 ...

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