首頁  >  文章  >  後端開發  >  TensorFlow模型保存和提取方法範例

TensorFlow模型保存和提取方法範例

不言
不言原創
2018-04-26 16:34:112352瀏覽

本篇文章主要介紹了TensorFlow模型保存和提取方法範例,現在分享給大家,也給大家做個參考。一起來看看吧

一、TensorFlow模型保存和提取方法

1. TensorFlow透過tf.train.Saver類別實作神經網絡模型的保存與提取。 tf.train.Saver物件saver的save方法將TensorFlow模型儲存到指定路徑中,saver.save(sess,"Model/model.ckpt"),實際上在這個檔案目錄下會產生4個人檔案:

checkpoint檔案保存了一個錄下多有的模型檔案列表,model.ckpt.meta保存了TensorFlow計算圖的結構信息,model.ckpt保存每個變數的取值,此處檔案名稱的寫入方式會因不同參數的設定而不同,但載入restore時的檔案路徑名稱是以checkpoint檔案中的「model_checkpoint_path」值決定的。

2. 載入這個已儲存的TensorFlow模型的方法是saver.restore(sess,"./Model/model.ckpt"),載入模型的程式碼也要定義TensorFlow計算圖上的所有運算並且宣告一個tf.train.Saver類,不同的是載入模型時不需要進行變數的初始化,而是將變數的取值透過保存的模型載入進來,注意載入路徑的寫法。若不希望重複定義計算圖上的運算,可直接載入已經持久化的圖,saver =tf.train.import_meta_graph("Model/model.ckpt.meta")。

3.tf.train.Saver類別也支援在儲存和載入時給變數重新命名,宣告Saver類別物件的時候使用一個字典dict重命名變數即可,{"已儲存的變數的名稱name": 重新命名變數名稱},saver = tf.train.Saver({"v1":u1, "v2": u2})即原來名稱name為v1的變數現在載入到變數u1(名稱name為other- v1)中。

4. 上一條做的目的之一就是方便使用變數的滑動平均值。如果在載入模型時直接將影子變數對應到變數自身,則在使用訓練好的模型時就不需要再呼叫函數來取得變數的滑動平均值了。載入時,宣告Saver類別物件時透過一個字典將滑動平均值直接載入到新的變數中,saver = tf.train.Saver({"v/ExponentialMovingAverage": v}),另透過tf.train.ExponentialMovingAverage的variables_to_restore()函數取得變數重新命名字典。

此外,透過convert_variables_to_constants函數將計算圖中的變數及其取值透過常數的方式保存於一個檔案中。

二、TensorFlow程式實作

#
# 本文件程序为配合教材及学习进度渐进进行,请按照注释分段执行 
# 执行时要注意IDE的当前工作过路径,最好每段重启控制器一次,输出结果更准确  
# Part1: 通过tf.train.Saver类实现保存和载入神经网络模型  
# 执行本段程序时注意当前的工作路径 
import tensorflow as tf  
v1 = tf.Variable(tf.constant(1.0, shape=[1]), name="v1") 
v2 = tf.Variable(tf.constant(2.0, shape=[1]), name="v2") 
result = v1 + v2  
saver = tf.train.Saver()  
with tf.Session() as sess: 
  sess.run(tf.global_variables_initializer()) 
  saver.save(sess, "Model/model.ckpt")  
 
# Part2: 加载TensorFlow模型的方法  
import tensorflow as tf  
v1 = tf.Variable(tf.constant(1.0, shape=[1]), name="v1") 
v2 = tf.Variable(tf.constant(2.0, shape=[1]), name="v2") 
result = v1 + v2  
saver = tf.train.Saver()  
with tf.Session() as sess: 
  saver.restore(sess, "./Model/model.ckpt") # 注意此处路径前添加"./" 
  print(sess.run(result)) # [ 3.] 
  
# Part3: 若不希望重复定义计算图上的运算,可直接加载已经持久化的图  
import tensorflow as tf  
saver = tf.train.import_meta_graph("Model/model.ckpt.meta")  
with tf.Session() as sess: 
  saver.restore(sess, "./Model/model.ckpt") # 注意路径写法 
  print(sess.run(tf.get_default_graph().get_tensor_by_name("add:0"))) # [ 3.] 
  
# Part4: tf.train.Saver类也支持在保存和加载时给变量重命名  
import tensorflow as tf  
# 声明的变量名称name与已保存的模型中的变量名称name不一致 
u1 = tf.Variable(tf.constant(1.0, shape=[1]), name="other-v1") 
u2 = tf.Variable(tf.constant(2.0, shape=[1]), name="other-v2") 
result = u1 + u2  
# 若直接生命Saver类对象,会报错变量找不到 
# 使用一个字典dict重命名变量即可,{"已保存的变量的名称name": 重命名变量名} 
# 原来名称name为v1的变量现在加载到变量u1(名称name为other-v1)中 
saver = tf.train.Saver({"v1": u1, "v2": u2})  
with tf.Session() as sess: 
  saver.restore(sess, "./Model/model.ckpt") 
  print(sess.run(result)) # [ 3.] 
  
# Part5: 保存滑动平均模型  
import tensorflow as tf  
v = tf.Variable(0, dtype=tf.float32, name="v") 
for variables in tf.global_variables(): 
  print(variables.name) # v:0  
ema = tf.train.ExponentialMovingAverage(0.99) 
maintain_averages_op = ema.apply(tf.global_variables()) 
for variables in tf.global_variables(): 
  print(variables.name) # v:0 
             # v/ExponentialMovingAverage:0  
saver = tf.train.Saver()  
with tf.Session() as sess: 
  sess.run(tf.global_variables_initializer()) 
  sess.run(tf.assign(v, 10)) 
  sess.run(maintain_averages_op) 
  saver.save(sess, "Model/model_ema.ckpt") 
  print(sess.run([v, ema.average(v)])) # [10.0, 0.099999905]  
 
# Part6: 通过变量重命名直接读取变量的滑动平均值  
import tensorflow as tf  
v = tf.Variable(0, dtype=tf.float32, name="v") 
saver = tf.train.Saver({"v/ExponentialMovingAverage": v}) 
 with tf.Session() as sess: 
  saver.restore(sess, "./Model/model_ema.ckpt") 
  print(sess.run(v)) # 0.0999999 
  
# Part7: 通过tf.train.ExponentialMovingAverage的variables_to_restore()函数获取变量重命名字典  
import tensorflow as tf  
v = tf.Variable(0, dtype=tf.float32, name="v") 
# 注意此处的变量名称name一定要与已保存的变量名称一致 
ema = tf.train.ExponentialMovingAverage(0.99) 
print(ema.variables_to_restore()) 
# {&#39;v/ExponentialMovingAverage&#39;: <tf.Variable &#39;v:0&#39; shape=() dtype=float32_ref>} 
# 此处的v取自上面变量v的名称name="v"  
saver = tf.train.Saver(ema.variables_to_restore()) 
 with tf.Session() as sess: 
  saver.restore(sess, "./Model/model_ema.ckpt") 
  print(sess.run(v)) # 0.0999999 
 
# Part8: 通过convert_variables_to_constants函数将计算图中的变量及其取值通过常量的方式保存于一个文件中  
import tensorflow as tf 
from tensorflow.python.framework import graph_util  
v1 = tf.Variable(tf.constant(1.0, shape=[1]), name="v1") 
v2 = tf.Variable(tf.constant(2.0, shape=[1]), name="v2") 
result = v1 + v2  
with tf.Session() as sess: 
  sess.run(tf.global_variables_initializer()) 
  # 导出当前计算图的GraphDef部分,即从输入层到输出层的计算过程部分 
  graph_def = tf.get_default_graph().as_graph_def() 
  output_graph_def = graph_util.convert_variables_to_constants(sess, 
                            graph_def, [&#39;add&#39;])  
  with tf.gfile.GFile("Model/combined_model.pb", &#39;wb&#39;) as f: 
    f.write(output_graph_def.SerializeToString()) 
  
# Part9: 载入包含变量及其取值的模型  
import tensorflow as tf 
from tensorflow.python.platform import gfile  
with tf.Session() as sess: 
  model_filename = "Model/combined_model.pb" 
  with gfile.FastGFile(model_filename, &#39;rb&#39;) as f: 
    graph_def = tf.GraphDef() 
    graph_def.ParseFromString(f.read())  
  result = tf.import_graph_def(graph_def, return_elements=["add:0"]) 
  print(sess.run(result)) # [array([ 3.], dtype=float32)]

相關推薦:

詳解tensorflow載入資料的三種方式

tensorflow 使用flags定義指令列參數的方法

以上是TensorFlow模型保存和提取方法範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn