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.
推論ページと対になる形で、学習の主役 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.
コードと式の対応: このページでは、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.”
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.
推論ページと違い、ここでは 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
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.
同じモデルで 推論ページ の実行時間と比較すると、学習側が重いことを観測できます。理論ページの「学習は重い」を実測でつなぐ目的です。
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.