TensorFlow實現(xiàn)保存訓(xùn)練模型為pd文件并恢復(fù)
更新時間:2020年02月06日 14:26:29 作者:CrazyStoneZw
今天小編就為大家分享一篇TensorFlow實現(xiàn)保存訓(xùn)練模型為pd文件并恢復(fù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
TensorFlow保存模型代碼
import tensorflow as tf from tensorflow.python.framework import graph_util var1 = tf.Variable(1.0, dtype=tf.float32, name='v1') var2 = tf.Variable(2.0, dtype=tf.float32, name='v2') var3 = tf.Variable(2.0, dtype=tf.float32, name='v3') x = tf.placeholder(dtype=tf.float32, shape=None, name='x') x2 = tf.placeholder(dtype=tf.float32, shape=None, name='x2') addop = tf.add(x, x2, name='add') addop2 = tf.add(var1, var2, name='add2') addop3 = tf.add(var3, var2, name='add3') initop = tf.global_variables_initializer() model_path = './Test/model.pb' with tf.Session() as sess: sess.run(initop) print(sess.run(addop, feed_dict={x: 12, x2: 23})) output_graph_def = graph_util.convert_variables_to_constants(sess, sess.graph_def, ['add', 'add2', 'add3']) # 將計算圖寫入到模型文件中 model_f = tf.gfile.FastGFile(model_path, mode="wb") model_f.write(output_graph_def.SerializeToString())
讀取模型代碼
import tensorflow as tf with tf.Session() as sess: model_f = tf.gfile.FastGFile("./Test/model.pb", mode='rb') graph_def = tf.GraphDef() graph_def.ParseFromString(model_f.read()) c = tf.import_graph_def(graph_def, return_elements=["add2:0"]) c2 = tf.import_graph_def(graph_def, return_elements=["add3:0"]) x, x2, c3 = tf.import_graph_def(graph_def, return_elements=["x:0", "x2:0", "add:0"]) print(sess.run(c)) print(sess.run(c2)) print(sess.run(c3, feed_dict={x: 23, x2: 2}))
以上這篇TensorFlow實現(xiàn)保存訓(xùn)練模型為pd文件并恢復(fù)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Tensorflow訓(xùn)練MNIST手寫數(shù)字識別模型
- tensorflow實現(xiàn)訓(xùn)練變量checkpoint的保存與讀取
- Tensorflow訓(xùn)練模型越來越慢的2種解決方案
- 解決TensorFlow訓(xùn)練內(nèi)存不斷增長,進程被殺死問題
- tensorflow獲取預(yù)訓(xùn)練模型某層參數(shù)并賦值到當前網(wǎng)絡(luò)指定層方式
- tensorflow模型繼續(xù)訓(xùn)練 fineturn實例
- tensorflow如何繼續(xù)訓(xùn)練之前保存的模型實例
- Tensorflow實現(xiàn)在訓(xùn)練好的模型上進行測試
- tensorflow保持每次訓(xùn)練結(jié)果一致的簡單實現(xiàn)
相關(guān)文章
python判斷設(shè)備是否聯(lián)網(wǎng)的方法
這篇文章主要為大家詳細介紹了python判斷設(shè)備是否聯(lián)網(wǎng)的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06pandas實現(xiàn)將dataframe滿足某一條件的值選出
今天小編就為大家分享一篇pandas實現(xiàn)將dataframe滿足某一條件的值選出,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python實現(xiàn)的DES加密算法和3DES加密算法實例
這篇文章主要介紹了python實現(xiàn)的DES加密算法和3DES加密算法,以實例形式較為詳細的分析了DES加密算法和3DES加密算法的原理與實現(xiàn)技巧,需要的朋友可以參考下2015-06-06Python?matplotlib之折線圖的各種樣式與畫法總結(jié)
matplotlib是Python中的一個第三方庫,主要用于開發(fā)2D圖表,以漸進式、交互式的方式實現(xiàn)數(shù)據(jù)可視化,可以更直觀的呈現(xiàn)數(shù)據(jù),使數(shù)據(jù)更具說服力,下面這篇文章主要給大家介紹了關(guān)于Python?matplotlib之折線圖的各種樣式與畫法的相關(guān)資料,需要的朋友可以參考下2022-12-12Python實現(xiàn)查找二叉搜索樹第k大的節(jié)點功能示例
這篇文章主要介紹了Python實現(xiàn)查找二叉搜索樹第k大的節(jié)點功能,結(jié)合實例形式分析了Python二叉搜索樹的定義、查找、遍歷等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01ubuntu?20.04系統(tǒng)下如何切換gcc/g++/python的版本
這篇文章主要給大家介紹了關(guān)于ubuntu?20.04系統(tǒng)下如何切換gcc/g++/python版本的相關(guān)資料,文中通過代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用ubuntu具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12