0Pricing
Deep Learning Academy · Lesson

Save & Load with state_dict

Checkpoint weights to resume later.

Save & Load with state_dict is a free Deep Learning Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

Is the “Save & Load with state_dict” lesson free?

Yes — the full text of “Save & Load with state_dict” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.

What will I learn in “Save & Load with state_dict”?

Checkpoint weights to resume later. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Deep Learning Academy?

No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Save & Load with state_dict” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Deep Learning Academy lesson?

Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Split Train, Validation & Test
  2. An Epoch Loop with Validation
  3. Save & Load with state_dict
  4. Early Stopping on Val Loss
← Back to Deep Learning Academy