TF 保存和加载模型
<!-- 作者区域 -->
<div class="author">
<a class="avatar" href="/u/ff5ca6d0e88f">
<img src="//upload.jianshu.io/users/upload_avatars/1961389/907e515783a4.png?imageMogr2/auto-orient/strip|imageView2/1/w/96/h/96" alt="96">
https://www.jianshu.com/p/8850127ed25d
https://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/
<!-- 作者区域 -->
<div class="author">
<a class="avatar" href="/u/ff5ca6d0e88f">
<img src="//upload.jianshu.io/users/upload_avatars/1961389/907e515783a4.png?imageMogr2/auto-orient/strip|imageView2/1/w/96/h/96" alt="96">
<!-- 文章内容 -->
<div data-note-content="" class="show-content">
<div class="show-content-free">
<p><a href="https://link.jianshu.com?t=http://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/" target="_blank" rel="nofollow">A quick complete tutorial to save and restore Tensorflow models</a></p>
Tensorflow 模型有两个主要的文件:
.meta
的扩展名。.ckpt
的扩展名。在0.11版本以前只有一个文件,现在有两个。mymodel.data-00000-of-00001
mymodel.index
.data
file is the file that contains our training variables and we shall go after it.
.data
文件包含了我们所有训练的变量。
Along with this, Tensorflow also has a file named checkpoint which simply keeps a record of latest checkpoint files saved.
于此一起的,Tensorflow 还有一个文件叫做checkpoint只是单纯记录了最近的保存的ckeckpoint file
所以,保存的模型像下图类似。
tf.train.get_checkpoint_state
或者
tf.train.latest_checkpoint
会找不到文件。
import tensorflow as tf
w1 = tf.Variable(tf.random_normal(shape=[2]), name='w1')
w2 = tf.Variable(tf.random_normal(shape=[5]), name='w2')
saver = tf.train.Saver()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
saver.save(sess, 'my_test_model')
# This will save following files in Tensorflow v >= 0.11
# my_test_model.data-00000-of-00001
# my_test_model.index
# my_test_model.meta
# checkpoint
If we are saving the model after 1000 iterations, we shall call save by passing the step count:
如果我们在1000次迭代后保存模型,我们把迭代次数传给保存函数
saver.save(sess, 'my_test_model',global_step=1000)
This will just append ‘-1000’ to the model name and following files will be created:
这样会在保存文件后加入-1000
在模型名字后面。
my_test_model-1000.index
my_test_model-1000.meta
my_test_model-1000.data-00000-of-00001
checkpoint
if you want to keep only 4 latest models and want to save one model after every 2 hours during training you can use max_to_keep and keep_checkpoint_every_n_hours like this.
如果你只想保存四个模型并且两个小时保存模型一次,你可以使用max_to_keep
和keep_checkpoint_every_n_hours
#saves a model every 2 hours and maximum 4 latest models are saved.
saver = tf.train.Saver(max_to_keep=4, keep_checkpoint_every_n_hours=2)
If you want to use someone else’s pre-trained model for fine-tuning, there are two things you need to do:
如果你想用别人预训练好的模型进行fine-tuning,有两件事情需要做。
.metaa
把网络存储起来,我们可以用来再创造网络使用tf.train.import()
语句。 saver = tf.train.import_meta_graph('my_test_model-1000.meta')
Remember, import_meta_graph appends the network defined in .meta file to the current graph. So, this will create the graph/network for you but we still need to load the value of the parameters that we had trained on this graph.
记住,import_meta_graph
将定义在.meta
的网络导入到当前图中。所以,这会替你创造图/网络,但我们仍然需要导入在这张图上训练好的参数。
tf.train.Saver()
类的一个实例。with tf.Session() as sess:
new_saver = tf.train.import_meta_graph('my_test_model-1000.meta')
new_saver.restore(sess, tf.train.latest_checkpoint('./'))
</div>
</div>
</div>
<!-- 如果是付费文章,未购买,则显示购买按钮 -->
<!-- 连载目录项 -->
<!-- 如果是付费文章 -->
<!-- 如果是付费连载,已购买,且作者允许赞赏,则显示付费信息和赞赏 -->
<div id="free-reward-panel" class="support-author"><p>小礼物走一走,来简书关注我</p> <div class="btn btn-pay">赞赏支持</div> <div class="supporter"><ul class="support-list"></ul> <!----></div> <!----> <!----></div>
<div class="show-foot">
<a class="notebook" href="/nb/7917014">
<i class="iconfont ic-search-notebook"></i>
<span>Theano & TF</span>
<!-- 文章底部作者信息 -->
<div class="follow-detail">
<div class="info">
<a class="avatar" href="/u/ff5ca6d0e88f">
<img src="//upload.jianshu.io/users/upload_avatars/1961389/907e515783a4.png?imageMogr2/auto-orient/strip|imageView2/1/w/96/h/96" alt="96">
写了 16971 字,被 7 人关注,获得了 12 个喜欢
<div class="meta-bottom">
<div data-v-6ddd02c6="" class="like"><div data-v-6ddd02c6="" class="btn like-group"><div data-v-6ddd02c6="" class="btn-like"><a data-v-6ddd02c6="">喜欢</a></div> <div data-v-6ddd02c6="" class="modal-wrap"><a data-v-6ddd02c6="">1</a></div></div> <!----></div>
<div class="share-group">
<a class="share-circle" data-action="weixin-share" data-toggle="tooltip" data-original-title="分享到微信">
<i class="iconfont ic-wechat"></i>
</a>
<a class="share-circle" data-action="weibo-share" data-toggle="tooltip" href="javascript:void((function(s,d,e,r,l,p,t,z,c){var%20f='http://v.t.sina.com.cn/share/share.php?appkey=1881139527',u=z||d.location,p=['&url=',e(u),'&title=',e(t||d.title),'&source=',e(r),'&sourceUrl=',e(l),'&content=',c||'gb2312','&pic=',e(p||'')].join('');function%20a(){if(!window.open([f,p].join(''),'mb',['toolbar=0,status=0,resizable=1,width=440,height=430,left=',(s.width-440)/2,',top=',(s.height-430)/2].join('')))u.href=[f,p].join('');};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else%20a();})(screen,document,encodeURIComponent,'','','', '推荐 阿o醒 的文章《TF 保存和加载模型》( 分享自 @简书 )','https://www.jianshu.com/p/8850127ed25d?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=weibo','页面编码gb2312|utf-8默认gb2312'));" data-original-title="分享到微博">
<i class="iconfont ic-weibo"></i>
</a>
<a class="share-circle" data-toggle="tooltip" id="longshare" target="_blank" data-original-title="" title="">
<div class="qrcode" id="qrcode">
<img src="//cdn2.jianshu.io/assets/web/download-index-side-qrcode-cb13fc9106a478795f8d10f9f632fccf.png" alt="Download index side qrcode">
<p>下载app生成长微博图片</p>
</div>
<i class="iconfont ic-picture"></i>
</a>
<a class="share-circle more-share" tabindex="0" data-toggle="popover" data-placement="top" data-html="true" data-trigger="focus" href="javascript:void(0);" data-content="
<ul class="share-list">
<li><a href="javascript:void(function(){var d=document,e=encodeURIComponent,r='http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+e('https://www.jianshu.com/p/8850127ed25d?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=qzone')+'&title='+e('推荐 阿o醒 的文章《TF 保存和加载模型》'),x=function(){if(!window.open(r,'qzone','toolbar=0,resizable=1,scrollbars=yes,status=1,width=600,height=600'))location.href=r};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})();"><i class="social-icon-sprite social-icon-zone"></i><span>分享到QQ空间</span></a></li>
<li><a href="javascript:void(function(){var d=document,e=encodeURIComponent,r='https://twitter.com/share?url='+e('https://www.jianshu.com/p/8850127ed25d?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=twitter')+'&text='+e('推荐 阿o醒 的文章《TF 保存和加载模型》( 分享自 @jianshucom )')+'&related='+e('jianshucom'),x=function(){if(!window.open(r,'twitter','toolbar=0,resizable=1,scrollbars=yes,status=1,width=600,height=600'))location.href=r};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})();"><i class="social-icon-sprite social-icon-twitter"></i><span>分享到Twitter</span></a></li>
<li><a href="javascript:void(function(){var d=document,e=encodeURIComponent,r='https://www.facebook.com/dialog/share?app_id=483126645039390&display=popup&href=https://www.jianshu.com/p/8850127ed25d?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=facebook',x=function(){if(!window.open(r,'facebook','toolbar=0,resizable=1,scrollbars=yes,status=1,width=450,height=330'))location.href=r};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})();"><i class="social-icon-sprite social-icon-facebook"></i><span>分享到Facebook</span></a></li>
<li><a href="javascript:void(function(){var d=document,e=encodeURIComponent,r='https://plus.google.com/share?url='+e('https://www.jianshu.com/p/8850127ed25d?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=google_plus'),x=function(){if(!window.open(r,'google_plus','toolbar=0,resizable=1,scrollbars=yes,status=1,width=450,height=330'))location.href=r};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})();"><i class="social-icon-sprite social-icon-google"></i><span>分享到Google+</span></a></li>
<li><a href="javascript:void(function(){var d=document,e=encodeURIComponent,s1=window.getSelection,s2=d.getSelection,s3=d.selection,s=s1?s1():s2?s2():s3?s3.createRange().text:'',r='http://www.douban.com/recommend/?url='+e('https://www.jianshu.com/p/8850127ed25d?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=douban')+'&title='+e('TF 保存和加载模型')+'&sel='+e(s)+'&v=1',x=function(){if(!window.open(r,'douban','toolbar=0,resizable=1,scrollbars=yes,status=1,width=450,height=330'))location.href=r+'&r=1'};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})()"><i class="social-icon-sprite social-icon-douban"></i><span>分享到豆瓣</span></a></li>
</ul>
" data-original-title="" title="">更多分享</a>
</div>
</div>
<a id="web-note-ad-1" target="_blank" href="/apps/redirect?utm_source=note-bottom-click"><img src="//cdn2.jianshu.io/assets/web/web-note-ad-1-c2e1746859dbf03abe49248893c9bea4.png" alt="Web note ad 1"></a>
<div><div id="comment-list" class="comment-list"><div><form class="new-comment"><a class="avatar"><img src="//cdn2.jianshu.io/assets/default_avatar/avatar_default-78d4d1f68984cd6d4379508dd94b4210.png"></a> <div class="sign-container"><a href="/sign_in?utm_source=desktop&utm_medium=not-signed-in-comment-form" class="btn btn-sign">登录</a> <span>后发表评论</span></div></form> <!----></div> <!----> <div class="comments-placeholder" style="display: none;"><div class="author"><div class="avatar"></div> <div class="info"><div class="name"></div> <div class="meta"></div></div></div> <div class="text"></div> <div class="text animation-delay"></div> <div class="tool-group"><i class="iconfont ic-zan-active"></i><div class="zan"></div> <i class="iconfont ic-list-comments"></i><div class="zan"></div></div></div> <div id="normal-comment-list" class="normal-comment-list"><div><!----> <div><div class="top-title"><span>评论</span> <a class="close-btn" style="display: none;">关闭评论</a></div> <div class="no-comment"></div> <div class="text">
智慧如你,不想<a href="/sign_in?utm_source=desktop&utm_medium=not-signed-in-nocomments-text">发表一点想法</a>咩~
</div></div> <!----> <div class="comments-placeholder" style="display: none;"><div class="author"><div class="avatar"></div> <div class="info"><div class="name"></div> <div class="meta"></div></div></div> <div class="text"></div> <div class="text animation-delay"></div> <div class="tool-group"><i class="iconfont ic-zan-active"></i><div class="zan"></div> <i class="iconfont ic-list-comments"></i><div class="zan"></div></div></div> </div></div> <!----> <div><!----></div></div></div>