Checkpoints in Google Colab

半腔热情 提交于 2020-01-03 02:23:11

问题


How do I store my trained model on Google Colab and retrieve further on my local disk? Will checkpoints work? How do I store them and retrieve them after some time? Can you please mention code for that. It would be great.


回答1:


Google Colab instances are created when you open the notebook and are deleted later on so you can't access data on different runs. If you want to download the trained model to your local machine you can use:

from google.colab import files
files.download(<filename>)

And similarly if you want to upload the model from your local machine you can do:

from google.colab import files
files.upload(<filename>)

Another possible (and better in my opinion) solution is to use a github repo to store your models and simply commit and push your models to github and clone the repo later to get the models back.




回答2:


Ok thats works for me

> import os 
> checkpoint_path = "training_1\cp.ckpt" 

> checkpoint_dir = os.path.dirname(checkpoint_path)

 # Create checkpoint  callback 
> cp_callback =ModelCheckpoint(checkpoint_path, 
     monitor='val_acc',save_best_only=True,save_weights_only=True,verbose=1)

> network_fit = myModel.fit(x, y, batch_size=25, epochs=20,
                                  ,callbacks = [cp_callback] )

By this code you can monitor val_acc and save weights on that epoch if it decrease. Now you can access this wights and load that in model by this code

myModel.load_weights(checkpoint_path)

You can check how to use it here https://colab.research.google.com/github/tensorflow/models/blob/master/samples/core/tutorials/keras/save_and_restore_models.ipynb#scrollTo=gXG5FVKFOVQ3



来源:https://stackoverflow.com/questions/49322072/checkpoints-in-google-colab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!