0Pricing
Deep Learning Academy · Lekcja

Zapisywanie i wczytywanie za pomocą state_dict

Zapisywać wagi w checkpointach, aby później wznowić pracę

Zapisywanie i wczytywanie za pomocą state_dict to bezpłatna lekcja Deep Learning Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Deep Learning Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Deep Learning Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Zapisywanie i wczytywanie za pomocą state_dict” jest bezpłatna?

Tak — pełny tekst „Zapisywanie i wczytywanie za pomocą state_dict” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Deep Learning Academy, przejdź na CoddyKit PRO. Kurs Deep Learning Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Zapisywanie i wczytywanie za pomocą state_dict”?

Zapisywać wagi w checkpointach, aby później wznowić pracę Ćwiczysz Deep Learning Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Deep Learning Academy?

Nie wymagamy żadnego doświadczenia. Deep Learning Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Zapisywanie i wczytywanie za pomocą state_dict”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Deep Learning Academy?

Tak. Każda lekcja Deep Learning Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Podział na zbiory treningowy, walidacyjny i testowy
  2. Pętla epok z walidacją
  3. Zapisywanie i wczytywanie za pomocą state_dict
  4. Wczesne zatrzymywanie na podstawie val loss
← Powrót do Deep Learning Academy