もくじIndex

ROCmで見る 学習の流れ

See Training Flow in ROCm

See Training Flow in ROCm

ROCm で見るシリーズ

forward → loss → backward → optimizer.step() の 1 エポックを PyTorch ROCm で実行し、重みが変わることを確認します。

Run one epoch of forward → loss → backward → optimizer.step() on PyTorch ROCm and confirm that weights change.

📘 対応する理論ページ: イメージでわかる学習の流れ — forward / backward / optimizer を図解で解説

📘 Theory page: Visual Training Flow — Forward / backward / optimizer explained with diagrams

このページの読み方: 1) まず model.training = True を見る 2) 次に loss が出るのを見る 3) 最後に weight changed = True を見れば、学習が起きたことがつかめます。

How to read this page: 1) First look at model.training = True 2) Then see that a loss appears 3) Finally check weight changed = True. That is enough to see that learning happened.

注記: このページの GPU 指定は "cuda" 表記ですが、ROCm 版 PyTorch の互換 API 名です。実行経路は HIP/ROCm です。

Note: GPU selection appears as "cuda" on this page, but that is the compatibility API name used by PyTorch ROCm. Execution still goes through HIP/ROCm.

1. なにをするか

1. What We'll Do

推論ページと対になる形で、学習の主役 4 つを見ます。① train() ② loss 計算 ③ backward() ④ optimizer.step()。最後に重みが変わることを確認します。

As a pair with the inference page, we focus on four training essentials: 1) train() 2) loss computation 3) backward() 4) optimizer.step(). We then verify that weights actually change.

困ったらこの 4 語だけ: train() は学習モード、loss はどれだけ外れたか、backward() はどこを直すか、optimizer.step() は実際に直す、です。

If you get stuck, keep just these four ideas: train() means training mode, loss means how wrong the answer was, backward() means what should be fixed, and optimizer.step() means actually fixing it.

2. 最小コード

2. Minimal Code

import torch import torch.nn as nn # 0) 実行前チェック print("torch.version.hip =", torch.version.hip) assert torch.version.hip is not None, "This example expects PyTorch built for ROCm" DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu") print("device =", DEVICE) # 1) 小さなモデルを作り、説明用に重みを固定 model = nn.Linear(3, 1).to(DEVICE) with torch.no_grad(): model.weight[:] = torch.tensor([[0.1, 0.2, 0.3]], device=DEVICE) model.bias[:] = torch.tensor([0.5], device=DEVICE) # 2) 学習モード model.train() print("model.training =", model.training) # True # 3) 入力と正解 x = torch.tensor([[1.0, 2.0, 3.0]], device=DEVICE) y_true = torch.tensor([[2.5]], device=DEVICE) loss_fn = nn.MSELoss() optimizer = torch.optim.SGD(model.parameters(), lr=0.1) w_before = model.weight.detach().clone() # 4) 学習 1 ステップ optimizer.zero_grad() pred = model(x) loss = loss_fn(pred, y_true) loss.backward() optimizer.step() w_after = model.weight.detach().clone() print("pred =", pred.detach().cpu()) print("loss =", loss.item()) print("pred.requires_grad =", pred.requires_grad) print("weight changed =", not torch.allclose(w_before, w_after)) print("weight.grad =", model.weight.grad.detach().cpu())

コードと式の対応: このページでは、forward の式と「そのずれを直す式」を 1 回だけ回しています。

Code-to-math link: This page runs one pass of the forward equation, then one pass of “the equation that fixes the error.”

$$\hat{y} = xw + b = 1.9,\quad y_{\text{true}} = 2.5,\quad L = (\hat{y} - y_{\text{true}})^2 = (1.9 - 2.5)^2 = 0.36$$
$$\hat{y} = xw + b = 1.9,\quad y_{\text{true}} = 2.5,\quad L = (\hat{y} - y_{\text{true}})^2 = (1.9 - 2.5)^2 = 0.36$$
$$\nabla_w L = \begin{bmatrix} -1.2 & -2.4 & -3.6 \end{bmatrix}, \quad w_{\text{new}} = w - \eta \nabla_w L$$
$$\nabla_w L = \begin{bmatrix} -1.2 & -2.4 & -3.6 \end{bmatrix}, \quad w_{\text{new}} = w - \eta \nabla_w L$$

loss.backward() がこの勾配 $\nabla_w L$ を出し、optimizer.step() が $w_{\text{new}}$ へ進めます。出力の weight.grad は、その途中の値です。

loss.backward() produces this gradient $\nabla_w L$, and optimizer.step() moves the weights to $w_{\text{new}}$. The printed weight.grad is that intermediate value.

コードは長く見えますが、最初は次の 4 行だけ追えば十分です。

The code may look long, but at first it is enough to follow these four lines.

model.train(): 学習モードへ切り替えます。

model.train(): This switches the model into training mode.

loss = loss_fn(pred, y_true): 予測と正解のずれを数にします。

loss = loss_fn(pred, y_true): This turns the gap between prediction and target into a number.

loss.backward(): どこをどう直すとよいかを計算します。

loss.backward(): This computes what should be changed and in which direction.

optimizer.step(): モデルの中の調整つまみを実際に少し動かします。

optimizer.step(): This actually moves the model’s internal adjustment knobs a little.

3. 実行するとどうなるか

3. What Happens When You Run It

torch.version.hip = 6.x.x device = cuda model.training = True pred = tensor([[1.9000]]) loss = 0.36 pred.requires_grad = True weight changed = True weight.grad = tensor([[-1.2000, -2.4000, -3.6000]])

推論ページと違い、ここでは pred.requires_grad=True で勾配を追跡し、backward()optimizer.step() によって重みが更新されます。これが「学習が起きた」状態です。

Unlike the inference page, this run tracks gradients with pred.requires_grad=True, then updates weights through backward() and optimizer.step(). That is what "learning happened" means here.

理論ページとの対応:
forward = model(x) — 入力をネットに通す
loss = 予測と正解のズレを数値化
backward = loss.backward() — 勾配を計算
optimizer.step() = 重みを少し修正

Theory page mapping:
forward = model(x) — pass input through the network
loss = quantify the gap between prediction and target
backward = loss.backward() — compute gradients
optimizer.step() = adjust weights a little

4. 裏では何が起きているか

4. Under the Hood

forward: まず答えを出します。
backward: 次に、どこをどう直すとよいかを逆向きに調べます。
optimizer.step(): 最後に、その情報を使って重みを少し直します。

forward: First the model produces an answer.
backward: Then it looks backward to find what should be changed.
optimizer.step(): Finally it uses that information to adjust the weights a little.

ROCm の見方では、学習は推論より仕事が増えます。答えを出すだけでなく、直し方を計算し、重みも更新するからです。

From a ROCm point of view, training has more work than inference. It not only produces an answer, but also computes corrections and updates the weights.

5. ROCmで観測するポイント

5. What to Observe on ROCm

# ROCm ビルド確認 python -c "import torch; print('torch.version.hip =', torch.version.hip)" # 学習ステップの HIP 呼び出し観測 rocprof --hip-trace python training_flow_example.py

同じモデルで 推論ページ の実行時間と比較すると、学習側が重いことを観測できます。理論ページの「学習は重い」を実測でつなぐ目的です。

Compare runtime with the same model on the inference page to observe why training is heavier. This connects the theory claim to actual measurement.

🎯 ポイント: 学習は forward + backward + update の繰り返しです。ここでは 1 回だけ実行しましたが、実際には数万〜数十万回繰り返して loss を下げ続けます。

🎯 Key point: Training is the repetition of forward + backward + update. We ran it once, but in practice this repeats thousands to millions of times to keep reducing loss.