1. 程式人生 > >tensorflow儲存和載入模型

tensorflow儲存和載入模型

×

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">
阿o醒 2017.09.09 16:11*
字數 795 閱讀 2047評論 0
    <!-- 文章內容 -->
    <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 模型有兩個主要的檔案:

  1. meta graph This is a protocol buffer which saves the complete Tensorflow graph; i.e. all variables, operations, collections etc. This file has .meta extension. 這是一個 protocol buffer(不會翻譯)儲存了完整的 Tensorflow 圖,即所有變數、操作和集合等。擁有一個.meta的副檔名。
  2. checkpoint file This is a binary file which contains all the values of the weights, biases, gradients and all the other variables saved. This file has an extension .ckpt. However, Tensorflow has changed this from version 0.11. Now, instead of single .ckpt file, we have two files: 這是一個二進位制檔案包含了所有權重、偏置、梯度和其他變數的值。這個檔案有一個.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

所以,儲存的模型像下圖類似。

模型幾大要件 備註:可以沒有graph檔案,但是checkpoint一定要有,不然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_keepkeep_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,有兩件事情需要做。

  1. 創造網路 you can create the network by writing python code to create each and every layer manually as the original model. However, if you think about it, we had saved the network in .meta file which we can use to recreate the network using tf.train.import() function like this: saver = tf.train.import_meta_graph('my_test_model-1000.meta') 你可以通過python寫好和原來模型一樣的每一層程式碼來創造網路,可是,仔細一想,我們已經通過.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的網路匯入到當前圖中。所以,這會替你創造圖/網路,但我們仍然需要匯入在這張圖上訓練好的引數。

  1. 載入引數 We can restore the parameters of the network by calling restore on this saver which is an instance of tf.train.Saver() class. 我們可以恢復網路的引數,通過使用saver,它是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 &amp; 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">

阿o醒

寫了 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=['&amp;url=',e(u),'&amp;title=',e(t||d.title),'&amp;source=',e(r),'&amp;sourceUrl=',e(l),'&amp;content=',c||'gb2312','&amp;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&amp;utm_content=note&amp;utm_medium=reader_share&amp;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=&quot;share-list&quot;>
        <li><a href=&quot;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&amp;utm_content=note&amp;utm_medium=reader_share&amp;utm_source=qzone')+'&amp;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()}})();&quot;><i class=&quot;social-icon-sprite social-icon-zone&quot;></i><span>分享到QQ空間</span></a></li>
        <li><a href=&quot;javascript:void(function(){var d=document,e=encodeURIComponent,r='https://twitter.com/share?url='+e('https://www.jianshu.com/p/8850127ed25d?utm_campaign=maleskine&amp;utm_content=note&amp;utm_medium=reader_share&amp;utm_source=twitter')+'&amp;text='+e('推薦 阿o醒 的文章《TF 儲存和載入模型》( 分享自 @jianshucom )')+'&amp;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()}})();&quot;><i class=&quot;social-icon-sprite social-icon-twitter&quot;></i><span>分享到Twitter</span></a></li>
        <li><a href=&quot;javascript:void(function(){var d=document,e=encodeURIComponent,r='https://www.facebook.com/dialog/share?app_id=483126645039390&amp;display=popup&amp;href=https://www.jianshu.com/p/8850127ed25d?utm_campaign=maleskine&amp;utm_content=note&amp;utm_medium=reader_share&amp;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()}})();&quot;><i class=&quot;social-icon-sprite social-icon-facebook&quot;></i><span>分享到Facebook</span></a></li>
        <li><a href=&quot;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&amp;utm_content=note&amp;utm_medium=reader_share&amp;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()}})();&quot;><i class=&quot;social-icon-sprite social-icon-google&quot;></i><span>分享到Google+</span></a></li>
        <li><a href=&quot;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&amp;utm_content=note&amp;utm_medium=reader_share&amp;utm_source=douban')+'&amp;title='+e('TF 儲存和載入模型')+'&amp;sel='+e(s)+'&amp;v=1',x=function(){if(!window.open(r,'douban','toolbar=0,resizable=1,scrollbars=yes,status=1,width=450,height=330'))location.href=r+'&amp;r=1'};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})()&quot;><i class=&quot;social-icon-sprite social-icon-douban&quot;></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&amp;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&amp;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>