Saving and Loading Models (Coding TensorFlow)
Skills:
ML Pipelines90%
Key Takeaways
Saving and loading models in TensorFlow to prevent loss of valuable training time, using SavedModel and Colab.
Full Transcript
[Music] [Applause] hi there everybody what's up my name is Magnus and you're watching cohdon intensify the show where you learn how to code intensive flow alright in this episode we'll talk about saving and loading models so why do we want to talk about this well first of all whenever you train a model of any significant complexity the training can take a long time most of the models in this getting started series will just take a minute or so to train but real-life models can take days or even weeks to train so if you were to hit ctrl C on your training job after it's been running for a day or so all your model weights values will be lost and you would have to restart training from the beginning and be a very sad camper but if you save your model every so often you can always resume training from that point making you a happy camper another benefit is that you can take your model and transfer it to another computer where you can continue training but I'm pretty sure you already guessed that I was going to bring that up that's enough talking for now check out the links below to locate the code because that's what we're gonna do now check out the cold Oh finally we get to check out the code that's awesome let's go and check out the code all right let's start by checking out the awesome licenses here at the top then install packages for hdf5 and general support and here we do some imports and print the tensorflow version it's totally okay if you have a later version than me here we used to M this dataset to demonstrate model loading and saving then reshape the images to batches of 28 by 28 arrays which is the pixel size of M NIST images and normalize all pixel values to be between 0 & 1 next is the model definition which is defined in the create underscore model function this is a very basic model which is totally ok because in this screencast we're interested learning how to load and save models not creating the best model for the emne Stata set and here we finally get to see how a model can be saved checkpoint underscore path will be the path of the save model a model checkpoint callback object is created with this path we also specify that only the weights of the model should be saved and that we want debug output when saving is performed finally we perform the model training by calling the fit method and providing this callback as you can see this will cost the model to be saved once every epoch has been completed and if we look at the check points directory we can now see three files the CP dot ck apt dot data file contains all the weight values this file has a range sequence because multiple partitions could potentially be used if we have a lot of weights the CP dot c k PT dot index file specifies which partition file contains which weights and finally the checkpoint file is a text file that points to the latest model in our case we only have one data file but shortly we'll see an example where we have saved multiple versions of the model all right so now when we have our saved model let's try out loading it first let's just create a model from scratch and try it out since it hasn't been trained you can see that the accuracy really sucks and now for the magic if we call the method load underscore weights with our checkpoint path our model gets initialized with the previous training state and has much better accuracy ok so that's the basics to save and load models let's look at some more options we have one option is to provide the period parameter when creating the model checkpoint object in this case we used a value 5 which as you can see saves a new model every 5 epochs observe in this case we also use a parameterised file name based on the epoch this means a unique file is saved every time that's also why we can see multiple files from looking at the checkpoint directory we can also use a function called TF train dot latest underscore checkpoint that will return the latest model which was saved in our case the one with index 50 this function actually looks into the file with the name checkpoint to find the latest checkpoint remember that the checkpoint file is a text file so you can actually check the file content yourself and now we can load the model using the load underscore weights function like we did before providing the value returned by TF the train that latest underscore checkpoint another way of saving models is to call the save method on the model this will create an hdf5 formatted file remember that we specified safe underscore weights underscore only to true last time we saved a model in addition to only saving variables the save method says additional data like the models configuration and even the state of the optimizer a model that was saved using the save method can be loaded with the function Kira's thought models that load underscore model and as you can see we have the accuracy of a trained model in addition to everything we've looked at tensorflow also has a very important file format called saved model this is a file format that allows to exchange models between many different parts of tensorflow like tens of your Python ten Sephirot Jess and also tells the full light we are currently building out first hand support for saved modeling Kira's and you can check out the links below to read more about it and that's it for this episode of the coding tensorflow make sure to subscribe to the channel to get more videos like this now it's your turn to go out there and create some great Manos don't forget to tell us all about it [Music]
Original Description
Training models can take a very long time, and you definitely don’t want to have to retrain everything over a single mishap. Make sure you listen to Magnus as he explains the importance of saving and loading models so you won’t end up losing your valuable training time. Follow along in this episode of Coding TensorFlow with the Colab provided below. And as always, let us know what you think in the comments below!
Colab Save and Restore Models → http://bit.ly/save-and-restore
For more information on SavedModel, check out this link → http://bit.ly/2yXmIQS
Want to watch more? → http://bit.ly/Coding-TensorFlow
Subscribe to the TensorFlow YouTube channel → http://bit.ly/TensorFlow1
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from TensorFlow · TensorFlow · 0 of 60
← Previous
Next →
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
35
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
The TensorFlow YouTube Channel is Here!
TensorFlow
Answering Your TF Questions #AskTensorFlow
TensorFlow
Chatting With the TensorFlow Community (TensorFlow Meets)
TensorFlow
All About TensorFlow Code (Coding TensorFlow)
TensorFlow
TensorFlow: an ML platform for solving impactful and challenging problems
TensorFlow
Keynote (TensorFlow Dev Summit 2018)
TensorFlow
tf.data: Fast, flexible, and easy-to-use input pipelines (TensorFlow Dev Summit 2018)
TensorFlow
Eager Execution (TensorFlow Dev Summit 2018)
TensorFlow
Machine Learning in JavaScript (TensorFlow Dev Summit 2018)
TensorFlow
Training Performance: A user’s guide to converge faster (TensorFlow Dev Summit 2018)
TensorFlow
The Practitioner's Guide with TF High Level APIs (TensorFlow Dev Summit 2018)
TensorFlow
Distributed TensorFlow (TensorFlow Dev Summit 2018)
TensorFlow
Debugging TensorFlow with TensorBoard plugins (TensorFlow Dev Summit 2018)
TensorFlow
TensorFlow Lite (TensorFlow Dev Summit 2018)
TensorFlow
Searching Over Ideas (TensorFlow Dev Summit 2018)
TensorFlow
Reconstructing Fusion Plasmas (TensorFlow Dev Summit 2018)
TensorFlow
Nucleus: TensorFlow toolkit for Genomics (TensorFlow Dev Summit 2018)
TensorFlow
Open Source Collaboration (TensorFlow Dev Summit 2018)
TensorFlow
Swift for TensorFlow - TFiwS (TensorFlow Dev Summit 2018)
TensorFlow
TensorFlow Hub (TensorFlow Dev Summit 2018)
TensorFlow
Applied AI at The Coca-Cola Company (TensorFlow Dev Summit 2018)
TensorFlow
Real-World Robot Learning (TensorFlow Dev Summit 2018)
TensorFlow
TensorFlow Extended (TFX) (TensorFlow Dev Summit 2018)
TensorFlow
Project Magenta (TensorFlow Dev Summit 2018)
TensorFlow
TensorFlow Dev Summit 2018 - Livestream
TensorFlow
Introducing TensorFlow Lite (Coding TensorFlow)
TensorFlow
TensorFlow Dev Summit 2018 Highlights
TensorFlow
Jeff Dean, Head of AI at Google discusses the impact of ML (TensorFlow Meets)
TensorFlow
TensorFlow Mobile vs. TF Lite and More! #AskTensorFlow
TensorFlow
Using TensorFlow to enable research & production across many fields (TensorFlow Meets)
TensorFlow
Teaching TensorFlow for Deep Learning at Stanford University (TensorFlow Meets)
TensorFlow
TensorFlow Lite for Android (Coding TensorFlow)
TensorFlow
Using the tf.data API to build input pipelines (TensorFlow Meets)
TensorFlow
Training Models in the Cloud & the Benefits of AI Toolkits #AskTensorFlow
TensorFlow
Execute operations immediately with TensorFlow's Eager Execution (TensorFlow Meets)
TensorFlow
TensorFlow Lite for iOS (Coding TensorFlow)
TensorFlow
Get started with TensorFlow's High-Level APIs (Google I/O '18)
TensorFlow
TensorFlow for JavaScript (Google I/O '18)
TensorFlow
TensorFlow in production: TF Extended, TF Hub, and TF Serving (Google I/O '18)
TensorFlow
Get started with TensorFlow's High-Level APIs in 5 mins | Google I/O 2018
TensorFlow
TensorFlow and deep reinforcement learning, without a PhD (Google I/O '18)
TensorFlow
TensorFlow Lite for mobile developers (Google I/O '18)
TensorFlow
Advances in machine learning and TensorFlow (Google I/O '18)
TensorFlow
Distributed TensorFlow training (Google I/O '18)
TensorFlow
Classification using neural networks & ML regression models #AskTensorFlow
TensorFlow
TensorFlow and Keras in R - Josh Gordon meets with J.J. Allaire (TensorFlow Meets)
TensorFlow
Focus on your experiment with TensorFlow Estimators (TensorFlow Meets)
TensorFlow
How to get started with AI/ML, retraining models, & more! #AskTensorFlow
TensorFlow
TensorFlow - the deep learning solution for mobile platforms (TensorFlow Meets)
TensorFlow
MiniGo: TensorFlow Meets Andrew Jackson (TensorFlow Meets)
TensorFlow
The growth of TensorFlow with added support for JS & Swift (TensorFlow Meets)
TensorFlow
At the intersection of TensorFlow & nuclear physics (TensorFlow Meets)
TensorFlow
NVidia TensorRT: high-performance deep learning inference accelerator (TensorFlow Meets)
TensorFlow
Try TensorFlow.js in your browser (Coding TensorFlow)
TensorFlow
TensorFlow Hub: reusing machine learning modules (TensorFlow Meets)
TensorFlow
How to use TensorFlow in PyCharm (TensorFlow Tip of the Week)
TensorFlow
Training models faster with TensorFlow Hub (TensorFlow Meets)
TensorFlow
Prepare your dataset for machine learning (Coding TensorFlow)
TensorFlow
Using ML to predict insulin use for Type 1 Diabetes (TensorFlow Meets)
TensorFlow
TFX: an end-to-end machine learning platform for TensorFlow (TensorFlow Meets)
TensorFlow
More on: ML Pipelines
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Want to get started with deep learning
Reddit r/deeplearning
Building a Deepfake Detector From Scratch — What Nobody Tells You
Medium · Deep Learning
Unfolding the Meandering Path: High-Dimensional Invariance and the Flat 2D Plane of Neural…
Medium · Deep Learning
Implementing Neural Style Transfer from Scratch: The Project That Started It All
Medium · Deep Learning
🎓
Tutor Explanation
DeepCamp AI