0Pricing
Deep Learning Academy · Lektion

Mit state_dict speichern und laden

Sichern Sie Gewichte, um später fortzufahren.

Mit state_dict speichern und laden ist eine kostenlose Deep Learning Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Deep Learning Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 💾

Häufig gestellte Fragen

Ist die Lektion „Mit state_dict speichern und laden“ kostenlos?

Ja — der vollständige Text von „Mit state_dict speichern und laden“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Deep Learning Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Mit state_dict speichern und laden“?

Sichern Sie Gewichte, um später fortzufahren. Du übst Deep Learning Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Deep Learning Academy zu starten?

Keine Vorkenntnisse erforderlich. Deep Learning Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Mit state_dict speichern und laden“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Deep Learning Academy-Lektion Code schreiben und ausführen?

Ja. Jede Deep Learning Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Training, Validierung und Test aufteilen
  2. Eine Epochenschleife mit Validierung
  3. Mit state_dict speichern und laden
  4. Early Stopping beim Validierungs-Loss
← Zurück zu Deep Learning Academy