最小限のトレーニングループを書く
データをループ処理して重みを更新します
「最小限のトレーニングループを書く」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
From Steps to a Loop
You know the four moves of one iteration. Now you wrap them in a loop that walks over your data again and again until the model is trained. 🔁
Gather the Ingredients
Before looping you need three things: a model, a loss function, and an optimizer that holds your model's parameters. Set them up once, up front.
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters())Loop Over Epochs
The outer loop counts epochs, one full pass through your training data. A few epochs may be plenty for a small problem, more for a hard one.
for epoch in range(epochs):
...Loop Over Batches
Inside each epoch you iterate the dataloader, which hands you one batch of inputs and labels at a time instead of the whole dataset at once.
for x, y in dataloader:
...Zero, Then Forward
Start each batch by clearing old gradients, then run the forward pass to get predictions. Order matters: zero first, then predict.
optimizer.zero_grad()
pred = model(x)Measure, Then Learn
Compute the loss, call backward for gradients, then step the optimizer. These three lines are where every weight actually improves.
loss = loss_fn(pred, y)
loss.backward()
optimizer.step()The Full Skeleton
Stitch it together and you have a complete training loop. It is short on purpose: this same shape powers projects of every size.
for epoch in range(epochs):
for x, y in dataloader:
optimizer.zero_grad()
loss = loss_fn(model(x), y)
loss.backward()
optimizer.step()Watch the Loss
Print the loss each epoch so you can see learning happen. A number that trends down means your loop is working as intended.
print(epoch, loss.item())Why item Matters
Use loss.item to pull out a plain Python float. Logging the raw tensor instead keeps the whole computation graph alive and wastes memory.
If Loss Stalls
If the loss refuses to drop, suspect the basics first: a learning rate that is too high or too low, or a forgotten zero_grad each step.
Small but Complete
This loop is tiny yet it is genuinely complete. Everything fancier you meet later just adds validation, logging, or speed on top of these few lines.
Quick Check
What does the inner loop iterate over in a minimal training loop?
Recap
You built a real training loop: loop epochs, loop batches, then zero, forward, loss, backward, step. Print the loss to watch it learn. 🎉
よくある質問
「最小限のトレーニングループを書く」レッスンは無料ですか?
はい。「最小限のトレーニングループを書く」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。
「最小限のトレーニングループを書く」で何を学びますか?
データをループ処理して重みを更新します ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Deep Learning Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「最小限のトレーニングループを書く」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDeep Learning Academyレッスンでコードを書いて実行できますか?
はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 順伝播、損失、逆伝播、更新
- 最小限のトレーニングループを書く
- 学習中の正解率を追跡する
- 学習モードと評価モード