Deep Learning Academy · レッスン

学習モードと評価モード

model.train()とmodel.eval()の違いを学びます

レッスン 4/413 ステップ

「学習モードと評価モード」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

A Model Has Two Faces

Your network behaves differently while learning than while being tested. PyTorch calls these two states train mode and eval mode. 🎭

Flip Into Train Mode

Before training you call model.train. This tells every layer to behave the way it should while the model is actively learning from data.

model.train()

Flip Into Eval Mode

Before validating or predicting you call model.eval. Now the network switches to its steady, deterministic behavior for honest measurement.

model.eval()

Dropout Cares

Dropout randomly zeros neurons during training to prevent overfitting. In eval mode it turns fully off so every neuron contributes to the prediction.

Batch Norm Cares Too

Batch norm uses each batch's statistics while training but switches to stored running averages at eval time for stable, consistent outputs.

It Is Only a Switch

These calls do not train or test anything by themselves. They simply flip a flag that tells dropout and norm layers which behavior to use.

Eval Is Not no_grad

A common mix-up: eval changes layer behavior, while no_grad stops gradient tracking. They solve different problems, so you usually use both together.

model.eval()
with torch.no_grad():
    pred = model(x)

Forgetting eval Hurts

If you validate without eval, dropout and batch norm keep their training behavior. Your scores turn noisy and look worse than the model really is.

Forgetting train Hurts

The reverse bites too: leave the model in eval and dropout never activates, so its regularization silently vanishes during training.

Build It Into the Loop

Make the switch a habit: set train before the training pass and eval before validation, every single epoch, so the model never stays in the wrong mode.

for epoch in range(epochs):
    model.train()
    # ... train ...
    model.eval()
    # ... validate ...

Small Call, Big Effect

Two tiny calls, yet they decide whether your metrics are trustworthy. Treat the mode switch as a non-negotiable part of every loop.

Quick Check

What does calling model.eval() actually change?

Recap

You learned the two modes: train activates dropout and batch stats, eval makes them deterministic. Switch every epoch and pair eval with no_grad. 🎉

無料で開始

AI チューターと学ぶ Python — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
30
レッスン
120

よくある質問

「学習モードと評価モード」レッスンは無料ですか?

はい。「学習モードと評価モード」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。

「学習モードと評価モード」で何を学びますか?

model.train()とmodel.eval()の違いを学びます ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Deep Learning Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「学習モードと評価モード」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このDeep Learning Academyレッスンでコードを書いて実行できますか?

はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 順伝播、損失、逆伝播、更新
  2. 最小限のトレーニングループを書く
  3. 学習中の正解率を追跡する
  4. 学習モードと評価モード
← Deep Learning Academyに戻る