もくじIndex

PyTorch特急便

PyTorch Express

PyTorch ってなあに?を 1 ページで

What is PyTorch? in one page

このページは「PyTorch が何者か」を最短でつかむための導入です。深層学習・学習フロー・推論フロー・ROCm 実例ページに入る前の橋渡しとして使えます。

This page is a fast introduction to what PyTorch is. Use it as a bridge before deep learning, training flow, inference flow, and ROCm example pages.

1. PyTorch は何?

1. What Is PyTorch?

まずは一言でつかみます。

Let's start with the shortest useful picture.

PyTorch は、AI を作ったり動かしたりするための道具箱です。

PyTorch is a toolbox for building and running AI models.

数字をまとめて持ち、部品をつなぎ、必要なら学習や推論まで進められます。ここでいう「モデル」は、入力を受けて出力を返す仕組みのことです。

It lets you hold numbers, connect parts into a model, and then run training or inference.

大事な一文: PyTorch は「AIそのもの」ではなく、AI モデルを作って動かすための道具です。

One important sentence: PyTorch is not "the AI itself." It is the tool used to build and run the model.

このサイトでの見え方: PyTorch は「モデルを書く場所」で、ROCm は「GPU で動かす土台」として出てきます。

How this site uses the idea: PyTorch is where we write the model, and ROCm is the foundation that runs heavy work on the GPU.

2. まず覚える 4 語

2. Four Words to Learn First

この 4 つだけ押さえれば、次のページにかなり入りやすくなります。

If you know these four words, the next pages become much easier.

Tensor

数字の入れ物です。1個の数字でも、表でも、画像でも、PyTorch ではまず Tensor として持ちます。

A container for numbers. A single value, a table, or even an image is first represented as a tensor in PyTorch.

Module

モデルを作る部品です。「数字を変換する部品」や「数字の通り方を少し変える部品」を組み合わせてモデルを作ります。

A building block for a model. You combine modules such as layers and activations to build the whole model.

autograd

自動で「どこを直せばよいか」を計算する仕組みです。学習で重要になります。

The system that automatically computes "what should be fixed." This becomes important during training.

optimizer

autograd が出した情報を使って、実際に重みを少し直す係です。

The part that actually updates the weights a little, using the information from autograd.

ひとまずは、Tensor = 数字の入れ物Module = 部品 と覚えるだけでも十分です。

At first, it is already enough to remember Tensor = box of numbers and Module = part.

3. 10 行コードを 1 行ずつ読む

3. Reading a 10-Line Example One Step at a Time

いきなり全部わからなくても大丈夫です。何をしているかだけ拾います。

You do not need to understand everything at once. Just follow what each line is doing.

import torch import torch.nn as nn x = torch.randn(1, 4) model = nn.Sequential( nn.Linear(4, 8), nn.ReLU(), nn.Linear(8, 2), ) y = model(x) print(x.shape, y.shape)

コードと式の対応: この 10 行は、教科書ふうに読むと「小さい行列計算を 2 回つなぐ」形です。

Code-to-math link: In textbook style, these 10 lines are “two small matrix steps connected in sequence.”

$$x \in \mathbb{R}^{1 \times 4},\quad h_1 = xW_1 + b_1,\quad h_2 = \mathrm{ReLU}(h_1),\quad y = h_2W_2 + b_2$$
$$x \in \mathbb{R}^{1 \times 4},\quad h_1 = xW_1 + b_1,\quad h_2 = \mathrm{ReLU}(h_1),\quad y = h_2W_2 + b_2$$

nn.Linear(4, 8) は $x \mapsto h_1$、nn.ReLU() は $h_1 \mapsto h_2$、nn.Linear(8, 2) は $h_2 \mapsto y$ に対応します。まずは「4 個の数字が 8 個になり、最後に 2 個になる」と読めれば十分です。

nn.Linear(4, 8) corresponds to $x \mapsto h_1$, nn.ReLU() to $h_1 \mapsto h_2$, and nn.Linear(8, 2) to $h_2 \mapsto y$. At first, it is enough to read this as “4 numbers become 8, then finally 2.”

この 10 行でやっていることは、入力を作る → 部品を並べる → 通してみる の 3 つです。

These 10 lines really do three things: make an input → line up parts → run the input through them.

4. 学習と推論でどう使う?

4. How Is It Used in Training and Inference?

ここで出てくる言葉を先にやさしくつかみます。

Let's make the common terms gentle before they get heavy.

推論

Inference

すでにあるモデルで答えを出す場面です。

This is when you use an existing model to produce an answer.

  • model(x) で答えを出します。
  • You produce an answer with model(x).
  • eval() は推論向けの設定です。
  • eval() switches the model into inference mode.
  • no_grad() は、学習のための記録を取らないようにするので、少し軽くなります。
  • no_grad() skips training-related bookkeeping, which makes inference lighter.

学習

Training

答えのずれを見て、モデルを少しずつ直す場面です。

This is when you look at the error and gradually fix the model.

  • loss で「どれだけ外れたか」を見ます。loss は「間違いの大きさ」です。
  • loss measures how far off the answer was.
  • backward() で「どこを直すか」を計算します。
  • backward() computes "what should be fixed."
  • optimizer.step() で、実際に重みを少し直します。重みは、モデルの中にある「調整つまみ」のようなものです。
  • optimizer.step() actually updates the weights a little.

言葉の対応: forward は「前向きに答えを出す流れ」、backward は「どこを直すか逆向きに調べる流れ」です。

Meaning of the words: forward is the path that produces an answer, and backward is the path that figures out what to fix.

もう 3 語だけ: loss は「どれだけ外したか」、weight は「モデルの中の調整つまみ」、gradient は「どちらへどれくらい直すとよさそうか」を表す手がかりです。

Three more words: loss means how wrong the answer was, weight is an adjustable value inside the model, and gradient is the hint about how to change it.

学習では backward()optimizer.step() が増えます。推論では多くの場合、そこまではやりません。

Training adds backward() and optimizer.step(). In inference, you usually stop before those steps.

5. ROCm ではどうつながる?

5. How Does This Connect to ROCm?

Python で書いたものが、どう GPU まで届くかをざっくり見ます。

Here is the rough path from Python code down to the GPU.

このサイトでは、PyTorch はモデルを書く場所として出てきます。ROCm は、その下でGPU で重い計算を動かす土台として出てきます。

On this site, PyTorch appears as the place where you write the model. ROCm appears underneath it as the foundation that runs heavy computation on the GPU.

イメージとしては、PyTorch が窓口で、下のほうでは rocBLAS(大きな表どうしの計算)や MIOpen(ニューラルネットでよく出る定番処理)が実際の計算を担当します。

You can think of PyTorch as the front desk. Underneath it, libraries such as rocBLAS (matrix math) and MIOpen (AI operations such as convolution) perform the actual work.

このサイトの場面 Where on This Site PyTorch の見え方 How PyTorch Appears
深層学習 / 学習 / 推論 Deep learning / Training / Inference モデルを書いて、入力を通して、必要なら学習まで進める場所 Where you write the model, run inputs through it, and train if needed
ROCm で見る系ページ "See in ROCm" pages PyTorch のコードが、下でどの ROCm ライブラリにつながるかを見る場所 Where you see which ROCm libraries are reached under PyTorch code
実例ページ Example pages 短いコードと GPU 実行のつながりを確認する場所 Where short code examples are connected to GPU execution

PyTorch は、同じ書き方で CPU や GPU を扱いやすくするように作られています。

PyTorch is designed so that CPU and GPU use feel as similar as possible from the user's side.

インストールで大事な注意: ROCm 環境なら、pip install torch だけで済ませないほうが安全です。PyTorch 公式の Start LocallyLinux / Pip / Python / ROCm を選び、表示された ROCm 用コマンドで入れるのが基本です。

Important install note: On a ROCm system, it is safer not to stop at pip install torch. Use the command shown by the official PyTorch Start Locally selector with Linux / Pip / Python / ROCm chosen.

このリポジトリの説明や例は ROCm 7.2 系の環境でも読むことを想定しています。ただし、PyTorch の wheel index 名は release によって変わりうるので、このページでは rocm6.3 を必須条件としてではなく、「ROCm 向け index を明示して入れる」形の例として読んでください。

The explanations and examples in this repository are also meant to be readable on ROCm 7.2-class systems. However, the PyTorch wheel index name can change across releases, so do not read rocm6.3 here as a hard requirement. Read it as an example of explicitly choosing a ROCm-targeted index.

ROCm 7.2 環境でも考え方は同じで、まずは公式ページがその時点で出す ROCm 用コマンドを使うのが安全です。

The same idea applies on ROCm 7.2 systems: the safest path is still to use the ROCm command currently shown by the official selector.

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm<official-major.minor>

もし間違えて入れたら: いったんアンインストールしてから、公式 selector が出す ROCm 用コマンドで入れ直します。ここでも major.minor は固定ではなく、ROCm 6.x でも 7.2 系でも、その時点の公式表示に合わせます。

If you installed the wrong build: uninstall first, then reinstall with the ROCm command shown by the official selector. Here too, major.minor is not fixed: whether you are on ROCm 6.x or a ROCm 7.2-class system, follow the current official output.

pip uninstall -y torch torchvision torchaudio pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm<official-major.minor>

補足: ROCm 版 PyTorch でも、歴史的な理由で "cuda" という名前の API が残る場所があります。つまり、画面に "cuda" と出ても、すぐに「ROCm ではない」とは限りません。名前はそうでも、実行経路は HIP/ROCm 側です。

Note: Even in ROCm builds of PyTorch, some APIs still use the name "cuda" for historical compatibility reasons. The actual execution path is still HIP/ROCm.

import torch print("torch.version.hip:", torch.version.hip) print("cuda available:", torch.cuda.is_available()) if torch.cuda.is_available(): print("device:", torch.cuda.get_device_name(0))

インストールや入れ直しのあとで、ここで見たいのは torch.version.hip が出るか、GPU が見えているか、の 2 点です。

After installing or reinstalling, the main things to check here are whether torch.version.hip is present and whether a GPU is visible.

よくある混乱: Python は言語、PyTorch は道具です。学習は「重みを直す」、推論は「答えを出す」です。この 2 組を分けて考えるとかなり楽になります。

Common confusion: Python is the language, and PyTorch is the tool. Training means "changing weights," while inference means "producing answers." Keeping these pairs separate helps a lot.

6. 次に読むなら

6. Read Next

PyTorch の次は、部品の意味と計算の流れを見るとつながりやすいです。

After this page, it helps to learn the parts and the flow of computation.