欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python使用tensorflow保存、加載和使用模型的方法

 更新時間:2018年01月31日 16:22:10   作者:LordofRobots  
本篇文章主要介紹了python使用tensorflow保存、加載和使用模型的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

使用Tensorflow進行深度學習訓練的時候,需要對訓練好的網(wǎng)絡模型和各種參數(shù)進行保存,以便在此基礎(chǔ)上繼續(xù)訓練或者使用。介紹這方面的博客有很多,我發(fā)現(xiàn)寫的最好的是這一篇官方英文介紹:

http://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/

我對這篇文章進行了整理和匯總。

首先是模型的保存。直接上代碼:

#!/usr/bin/env python 
#-*- coding:utf-8 -*- 
############################ 
#File Name: tut1_save.py 
#Author: Wang  
#Mail: wang19920419@hotmail.com 
#Created Time:2017-08-30 11:04:25 
############################ 
 
import tensorflow as tf 
 
# prepare to feed input, i.e. feed_dict and placeholders 
w1 = tf.Variable(tf.random_normal(shape = [2]), name = 'w1') # name is very important in restoration 
w2 = tf.Variable(tf.random_normal(shape = [2]), name = 'w2') 
b1 = tf.Variable(2.0, name = 'bias1') 
feed_dict = {w1:[10,3], w2:[5,5]} 
 
# define a test operation that will be restored 
w3 = tf.add(w1, w2) # without name, w3 will not be stored 
w4 = tf.multiply(w3, b1, name = "op_to_restore") 
 
#saver = tf.train.Saver() 
saver = tf.train.Saver(max_to_keep = 4, keep_checkpoint_every_n_hours = 1) 
sess = tf.Session() 
sess.run(tf.global_variables_initializer()) 
print sess.run(w4, feed_dict) 
#saver.save(sess, 'my_test_model', global_step = 100) 
saver.save(sess, 'my_test_model') 
#saver.save(sess, 'my_test_model', global_step = 100, write_meta_graph = False) 

需要說明的有以下幾點:

1. 創(chuàng)建saver的時候可以指明要存儲的tensor,如果不指明,就會全部存下來。在這里也可以指明最大存儲數(shù)量和checkpoint的記錄時間。具體細節(jié)看英文博客。

2. saver.save()函數(shù)里面可以設(shè)定global_step和write_meta_graph,meta存儲的是網(wǎng)絡結(jié)構(gòu),只在開始運行程序的時候存儲一次即可,后續(xù)可以通過設(shè)置write_meta_graph = False加以限制。

3. 這個程序執(zhí)行結(jié)束后,會在程序目錄下生成四個文件,分別是.meta(存儲網(wǎng)絡結(jié)構(gòu))、.data和.index(存儲訓練好的參數(shù))、checkpoint(記錄最新的模型)。

下面是如何加載已經(jīng)保存的網(wǎng)絡模型。這里有兩種方法,第一種是saver.restore(sess, 'aaaa.ckpt'),這種方法的本質(zhì)是讀取全部參數(shù),并加載到已經(jīng)定義好的網(wǎng)絡結(jié)構(gòu)上,因此相當于給網(wǎng)絡的weights和biases賦值并執(zhí)行tf.global_variables_initializer()。這種方法的缺點是使用前必須重寫網(wǎng)絡結(jié)構(gòu),而且網(wǎng)絡結(jié)構(gòu)要和保存的參數(shù)完全對上。第二種就比較高端了,直接把網(wǎng)絡結(jié)構(gòu)加載進來(.meta),上代碼:

#!/usr/bin/env python 
#-*- coding:utf-8 -*- 
############################ 
#File Name: tut2_import.py 
#Author: Wang  
#Mail: wang19920419@hotmail.com 
#Created Time:2017-08-30 14:16:38 
############################  
import tensorflow as tf 
sess = tf.Session() 
new_saver = tf.train.import_meta_graph('my_test_model.meta') 
new_saver.restore(sess, tf.train.latest_checkpoint('./')) 
print sess.run('w1:0') 

使用加載的模型,輸入新數(shù)據(jù),計算輸出,還是直接上代碼:

#!/usr/bin/env python 
#-*- coding:utf-8 -*- 
############################ 
#File Name: tut3_reuse.py 
#Author: Wang 
#Mail: wang19920419@hotmail.com 
#Created Time:2017-08-30 14:33:35 
############################ 
 
import tensorflow as tf 
 
sess = tf.Session() 
 
# First, load meta graph and restore weights 
saver = tf.train.import_meta_graph('my_test_model.meta') 
saver.restore(sess, tf.train.latest_checkpoint('./')) 
 
# Second, access and create placeholders variables and create feed_dict to feed new data 
graph = tf.get_default_graph() 
w1 = graph.get_tensor_by_name('w1:0') 
w2 = graph.get_tensor_by_name('w2:0') 
feed_dict = {w1:[-1,1], w2:[4,6]} 
 
# Access the op that want to run 
op_to_restore = graph.get_tensor_by_name('op_to_restore:0') 
 
print sess.run(op_to_restore, feed_dict)   # ouotput: [6. 14.] 

在已經(jīng)加載的網(wǎng)絡后繼續(xù)加入新的網(wǎng)絡層:

import tensorflow as tf 
sess=tf.Session()   
#First let's load meta graph and restore weights 
saver = tf.train.import_meta_graph('my_test_model-1000.meta') 
saver.restore(sess,tf.train.latest_checkpoint('./')) 

# Now, let's access and create placeholders variables and 
# create feed-dict to feed new data 
 
graph = tf.get_default_graph() 
w1 = graph.get_tensor_by_name("w1:0") 
w2 = graph.get_tensor_by_name("w2:0") 
feed_dict ={w1:13.0,w2:17.0} 
 
#Now, access the op that you want to run.  
op_to_restore = graph.get_tensor_by_name("op_to_restore:0") 
 
#Add more to the current graph 
add_on_op = tf.multiply(op_to_restore,2) 
 
print sess.run(add_on_op,feed_dict) 
#This will print 120. 

對加載的網(wǎng)絡進行局部修改和處理(這個最麻煩,我還沒搞太明白,后續(xù)會繼續(xù)補充):

...... 
...... 
saver = tf.train.import_meta_graph('vgg.meta') 
# Access the graph 
graph = tf.get_default_graph() 
## Prepare the feed_dict for feeding data for fine-tuning  
 
#Access the appropriate output for fine-tuning 
fc7= graph.get_tensor_by_name('fc7:0') 
 
#use this if you only want to change gradients of the last layer 
fc7 = tf.stop_gradient(fc7) # It's an identity function 
fc7_shape= fc7.get_shape().as_list() 
 
new_outputs=2 
weights = tf.Variable(tf.truncated_normal([fc7_shape[3], num_outputs], stddev=0.05)) 
biases = tf.Variable(tf.constant(0.05, shape=[num_outputs])) 
output = tf.matmul(fc7, weights) + biases 
pred = tf.nn.softmax(output) 
 
# Now, you run this with fine-tuning data in sess.run() 

有了這樣的方法,無論是自行訓練、加載模型繼續(xù)訓練、使用經(jīng)典模型還是finetune經(jīng)典模型抑或是加載網(wǎng)絡跑前項,效果都是杠杠的。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python屬性和內(nèi)建屬性實例解析

    Python屬性和內(nèi)建屬性實例解析

    這篇文章主要介紹了Python屬性和內(nèi)建屬性實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-01-01
  • python 圖片驗證碼代碼

    python 圖片驗證碼代碼

    在網(wǎng)絡應用中,驗證碼常常作為一個必備的手段,用來避免機器人惡意注冊,保證坐在瀏覽器前的是一個人。
    2008-12-12
  • 學習python 的while循環(huán)嵌套

    學習python 的while循環(huán)嵌套

    這篇文章主要為大家介紹了python 的while循環(huán)嵌套,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • Python Django 后臺管理之后臺模型屬性詳解

    Python Django 后臺管理之后臺模型屬性詳解

    這篇文章主要介紹了Python Django 后臺管理之后臺模型屬性,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • python關(guān)于多值參數(shù)的實例詳解

    python關(guān)于多值參數(shù)的實例詳解

    在本篇內(nèi)容里小編給大家整理了一篇關(guān)于python關(guān)于多值參數(shù)的實例詳解內(nèi)容,有興趣的朋友們可以學習下。
    2021-07-07
  • python獲取元素在數(shù)組中索引號的方法

    python獲取元素在數(shù)組中索引號的方法

    這篇文章主要介紹了python獲取元素在數(shù)組中索引號的方法,實例分析了Python中index方法的相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • Python matplotlib模塊及柱狀圖用法解析

    Python matplotlib模塊及柱狀圖用法解析

    這篇文章主要介紹了Python matplotlib模塊及柱狀圖用法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • python編程實現(xiàn)12306的一個小爬蟲實例

    python編程實現(xiàn)12306的一個小爬蟲實例

    這篇文章主要介紹了python編程實現(xiàn)12306的一個小爬蟲實例,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • 基于Python 中函數(shù)的 收集參數(shù) 機制

    基于Python 中函數(shù)的 收集參數(shù) 機制

    今天小編就為大家分享一篇基于Python 中函數(shù)的 收集參數(shù) 機制,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python線程下使用鎖的技巧分享

    Python線程下使用鎖的技巧分享

    本篇文章給大家分享了Python線程下使用鎖需要注意的地方,有興趣的朋友們可以學習參考下。
    2018-09-09

最新評論