How to save and load models in Pytorch
Key Takeaways
This video demonstrates how to save and load models in PyTorch, specifically using the state dictionary and optimizer to create checkpoints.
Full Transcript
[Music] let's say we're at the point where we get a model to work and it's training and we want to be able to save the model and continuing the training at another point so for this we need to be to figure out how to save and load a check point of the model in this case I've got a very simple CNN network that's training on the M&S dataset what we want to do is we want to be able to let's say check point we're gonna store it as a dictionary it's called State dictionary and model dot state dictionary and also not only the model parameters we're also gonna store the optimizer you can store more things as well like the epoch the best accuracy and whatever information you want let's just keep it simple and we won't store the model parameters and the optimizer in this case then we're gonna do optimizer the state dictionary and then we want to do is let's say if epoch is equal to two you can do whatever here if maybe the fifth epoch or something like that then we want to [Music] then we want to call to some function save checkpoint with the checkpoint with the dictionary we created so let's create that function define nine checkpoint and the checkpoint is gonna take some state so the dictionary we created here and it's gonna also outputs to some file let's put it call it my checkpoint and use the standard convention or using dot PTH tatar so let's do some print saving checkpoint and then we're going to use torch dot save state at filing okay right so see checkpoint to checkpoint yeah so let's try run this undefined serger yeah okay it's gonna be a save checkpoint and this might take a while so I'll just continue when it's done yeah so now we're trying to epochs and we see saving checkpoint and let's actually see yeah so if I take up that folder it's going to show my checkpoint and that's the file now next point let's say we want to actually load it yeah we wanted create another function on the final load checkpoint from a checkpoint and we're gonna do print loading checkpoint and then we're just gonna do model dot load State dictionary quick point from state ticked and then pretty much the same thing but for the optimizer and again if you save more things in the checkpoint like accuracy or epoch or whatever you're gonna have to take this from the dictionary as well so for example if we would have checkpoint of best current accuracy or something you would call it like this but we only have the state dictionary and the optimizer then let's say we're gonna have another high parameter let's say we have load model is true so we're gonna do after we've initialized the model in optimizer we're gonna do load model of of torch dot load my chick points of the file that we created whatever you called that one and that's all so we can do if load model then load checkpoint that's what we call that function so now it should load model and if the epoch is two is gonna also save the a checkpoint but let's see let's say that we want to I don't know store it every third epoch so we can do epoch modulus three equals zero then it's going to create another checkpoint and save the checkpoint for example you could also do you could check the accuracy see if the accuracy is better than some best accuracy then you could save the model there are multiple ways of doing it let's say we just want to do it in this simple way I'm gonna let it rain for a while just so we can see all of it alright so it trained for 10 epochs and we can see that I in the beginning it loaded the check point and then it also saved the Check Point because epoch zero module three is zero so then we ran for three epochs or train for three up ox and we saved checkpoint another three save another three save so it seems to be working let's see so now it's 0.4 T three the mean loss for that epoch let's say that we now rerun it and we can see so it restarted with about the same lost value as the previous one right so this means that it's continuing from this point rather than restarting if for example we set load model to false then we see that it restarts right the loss is much higher now one thing to be careful of is now when we set load model to false it now when it's shaved this check point it actually over it over writes the previous file so you have to be cautious of that not to train for a long time and then rewrite overwrite your checkpoint file yeah if you have any questions about this leave them in the comment section thank you for watching the video and hope T in the next one
Original Description
Let's say you have a model that is working but now you want to be able to save a checkpoint and load it to continue training at a later point. In this video I walkthrough an example of how to do it!
❤️ Support the channel ❤️
https://www.youtube.com/channel/UCkzW5JSFwvKRjXABI-UTAkQ/join
Paid Courses I recommend for learning (affiliate links, no extra cost for you):
⭐ Machine Learning Specialization https://bit.ly/3hjTBBt
⭐ Deep Learning Specialization https://bit.ly/3YcUkoI
📘 MLOps Specialization http://bit.ly/3wibaWy
📘 GAN Specialization https://bit.ly/3FmnZDl
📘 NLP Specialization http://bit.ly/3GXoQuP
✨ Free Resources that are great:
NLP: https://web.stanford.edu/class/cs224n/
CV: http://cs231n.stanford.edu/
Deployment: https://fullstackdeeplearning.com/
FastAI: https://www.fast.ai/
💻 My Deep Learning Setup and Recording Setup:
https://www.amazon.com/shop/aladdinpersson
GitHub Repository:
https://github.com/aladdinpersson/Machine-Learning-Collection
✅ One-Time Donations:
Paypal: https://bit.ly/3buoRYH
▶️ You Can Connect with me on:
Twitter - https://twitter.com/aladdinpersson
LinkedIn - https://www.linkedin.com/in/aladdin-persson-a95384153/
Github - https://github.com/aladdinpersson
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Aladdin Persson · Aladdin Persson · 35 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
▶
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
computeCost.m Linear Regression Cost Function - Machine Learning
Aladdin Persson
gradientDescent.m Gradient Descent Implementation - Machine Learning
Aladdin Persson
Neural Network from scratch - Part 1 (Standard Notation)
Aladdin Persson
Neural Network from scratch - Part 2 (Forward Propagation)
Aladdin Persson
Neural Network from scratch - Part 3 (Backward Propagation)
Aladdin Persson
Neural Network from scratch - Part 4 (With Python)
Aladdin Persson
sigmoid.m - Programming Assignment 2 Machine Learning
Aladdin Persson
costFunction.m - Programming Assignment 2 Machine Learning
Aladdin Persson
predict.m - Programming Assignment 2 Machine Learning
Aladdin Persson
costFunctionReg.m - Programming Assignment 2 Machine Learning
Aladdin Persson
lrCostFunction.m - Programming Assignment 3 Machine Learning
Aladdin Persson
oneVsAll.m - Programming Assignment 3 Machine Learning
Aladdin Persson
predictOneVsAll.m - Programming Assignment 3 Machine Learning
Aladdin Persson
predict.m - Programming Assignment 3 Machine Learning
Aladdin Persson
Caesar Cipher Encryption and Decryption with example
Aladdin Persson
Cryptography: Caesar Cipher Python
Aladdin Persson
Vigenere Cipher Explained (with Example)
Aladdin Persson
Cryptography: Vigenere Cipher Python
Aladdin Persson
Hill Cipher Explained (with Example)
Aladdin Persson
Cryptography: Hill Cipher Python
Aladdin Persson
Interval Scheduling Greedy Algorithm: Python
Aladdin Persson
Weighted Interval Scheduling Algorithm Explained
Aladdin Persson
Weighted Interval Scheduling Python Code
Aladdin Persson
Sequence Alignment | Needleman Wunsch Algorithm
Aladdin Persson
Sequence Alignment | Needleman Wunsch in Python
Aladdin Persson
Codility BinaryGap Python
Aladdin Persson
Codility CyclicRotation Python
Aladdin Persson
Derivation Linear Regression with Gradient Descent
Aladdin Persson
Linear Regression Gradient Descent From Scratch in Python
Aladdin Persson
Pytorch Neural Network example
Aladdin Persson
Pytorch CNN example (Convolutional Neural Network)
Aladdin Persson
Pytorch LeNet implementation from scratch
Aladdin Persson
Pytorch VGG implementation from scratch
Aladdin Persson
Pytorch GoogLeNet / InceptionNet implementation from scratch
Aladdin Persson
How to save and load models in Pytorch
Aladdin Persson
How to build custom Datasets for Images in Pytorch
Aladdin Persson
Pytorch Transfer Learning and Fine Tuning Tutorial
Aladdin Persson
Pytorch Data Augmentation using Torchvision
Aladdin Persson
Pytorch Quick Tip: Weight Initialization
Aladdin Persson
Pytorch Quick Tip: Using a Learning Rate Scheduler
Aladdin Persson
Pytorch ResNet implementation from Scratch
Aladdin Persson
Pytorch TensorBoard Tutorial
Aladdin Persson
Pytorch DCGAN Tutorial (See description for updated video)
Aladdin Persson
Naive Bayes from Scratch - Machine Learning Python
Aladdin Persson
Spam Classifier using Naive Bayes in Python
Aladdin Persson
K-Nearest Neighbor from scratch - Machine Learning Python
Aladdin Persson
Linear Regression Normal Equation Python
Aladdin Persson
SVM from Scratch - Machine Learning Python (Support Vector Machine)
Aladdin Persson
Neural Network from Scratch - Machine Learning Python
Aladdin Persson
Pytorch RNN example (Recurrent Neural Network)
Aladdin Persson
Pytorch Bidirectional LSTM example
Aladdin Persson
Pytorch Text Generator with character level LSTM
Aladdin Persson
Logistic Regression from Scratch - Machine Learning Python
Aladdin Persson
K-Means Clustering from Scratch - Machine Learning Python
Aladdin Persson
Pytorch Torchtext Tutorial 1: Custom Datasets and loading JSON/CSV/TSV files
Aladdin Persson
Pytorch Torchtext Tutorial 2: Built in Datasets with Example
Aladdin Persson
Pytorch Torchtext Tutorial 3: From Textfiles to Dataset
Aladdin Persson
Paper Review: Sequence to Sequence Learning with Neural Networks
Aladdin Persson
Pytorch Seq2Seq Tutorial for Machine Translation
Aladdin Persson
Pytorch Seq2Seq with Attention for Machine Translation
Aladdin Persson
More on: ML Pipelines
View skill →Related Reads
📰
📰
📰
📰
Understanding Deep Learning Through Four Interactive Experiments
Medium · Data Science
Understanding Deep Learning Through Four Interactive Experiments
Medium · Deep Learning
Optimizers in Deep Learning: From Gradient Descent to Adam
Medium · Deep Learning
The Meta-Architecture of Interface Fracture: High-Dimensional Logical Stress and Systemic Collapse…
Medium · Deep Learning
🎓
Tutor Explanation
DeepCamp AI