state_dictで保存と読み込みを行う
後で再開できるよう重みをチェックポイント保存します
「state_dictで保存と読み込みを行う」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Why Checkpoints Matter
Training can take hours, and crashes happen. Saving your progress as a checkpoint lets you stop, resume, or ship the model without retraining from zero.
What a state_dict Holds
A model's state_dict is a plain dictionary mapping each layer name to its learned tensors. It is everything the model knows, packed for storage.
model.state_dict()Save the Weights
Use torch.save on the state_dict to write the weights to disk. The .pt or .pth extension is the common convention for these files.
torch.save(model.state_dict(), 'model.pt')Load the Weights Back
To restore, read the file with torch.load and pour it into a model using load_state_dict. The architecture must match the saved one.
model.load_state_dict(torch.load('model.pt'))Recreate the Architecture First
A state_dict holds numbers, not the class itself. You must build the same model object in code before you can load weights into it.
model = MyNet()
model.load_state_dict(torch.load('model.pt'))Eval Mode After Loading
Right after loading for inference, call model.eval(). It switches dropout and batch norm into prediction behavior so outputs are correct.
model.eval()Save the Optimizer Too
To truly resume training, also save the optimizer's state_dict. It holds momentum and adaptive stats that would otherwise reset to zero.
torch.save(optimizer.state_dict(), 'opt.pt')Bundle a Full Checkpoint
Pack model, optimizer, and the current epoch into one checkpoint dict. Now a single file restores your entire training session.
ckpt = {'epoch': epoch, 'model': model.state_dict(), 'opt': optimizer.state_dict()}
torch.save(ckpt, 'ckpt.pt')Resume From a Checkpoint
Load the bundle and restore each piece in turn. Reading the saved epoch lets you continue the loop exactly where it left off.
ckpt = torch.load('ckpt.pt')
model.load_state_dict(ckpt['model'])
optimizer.load_state_dict(ckpt['opt'])Map to the Right Device
If you saved on GPU and load on CPU, pass map_location to torch.load. It moves the weights to a device your machine actually has.
torch.load('model.pt', map_location='cpu')Save the Best, Not the Last
Watch validation loss and overwrite your checkpoint only when it improves. That way you keep the best model, not whatever the final epoch produced.
Quick Check
You saved only model.state_dict(). What must exist before you can load it?
Recap
Save weights with state_dict and torch.save, rebuild the model to load them, and bundle the optimizer and epoch for a full resume. Keep the best one. 💾
よくある質問
「state_dictで保存と読み込みを行う」レッスンは無料ですか?
はい。「state_dictで保存と読み込みを行う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。
「state_dictで保存と読み込みを行う」で何を学びますか?
後で再開できるよう重みをチェックポイント保存します ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Deep Learning Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「state_dictで保存と読み込みを行う」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDeep Learning Academyレッスンでコードを書いて実行できますか?
はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 学習用・検証用・テスト用に分割する
- 検証を含むエポックループ
- state_dictで保存と読み込みを行う
- 検証損失による早期停止