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:
    init_op = tf.initialize_all_variables()
    sess.run(init_op)
    saver.save(sess, "D:/python/ten_test/me/model.ckpt")

運(yùn)行后在本地的me文件夾中多了以下四個文件:


具體這幾個文件的作用不予贅述,網(wǎng)上都可以搜到。

讀取該會話

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, "D:/python/ten_test/me/model.ckpt")
    print(sess.run(result))

如此的運(yùn)行結(jié)果為:


可知,在這里,取出來的會話內(nèi)容為對v1和v2的初始化操作。

保存變量

在使用滑動平均值的時候,我們需要獲取影子變量的取值,這時就涉及到了經(jīng)過運(yùn)算以后保存一個變量,然后在需要的時候把該變量加載讀取出來的問題。
需要注意的時,由于我們需要獲取變量的值,故在保存sess之前需要讓sess執(zhí)行一次調(diào)用變量的操作。

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')
saver = tf.train.Saver()

with tf.Session() as sess:
    init_op = tf.initialize_all_variables()
    sess.run(init_op)
    sess.run(v1)
    sess.run(v2)
    saver.save(sess, "D:/python/ten_test/me/model.ckpt")

讀取變量

v1 = tf.Variable(tf.constant(0., shape=[1]), name="other_y")
v2 = tf.Variable(tf.constant(0., shape=[1]), name="other_x")
saver = tf.train.Saver({"v1": v1, "v2": v2})

with tf.Session() as sess:
    saver.restore(sess, "D:/python/ten_test/me/model.ckpt")
    print(sess.run([v1, v2]))

運(yùn)行結(jié)果:

可知,從加載后的sess中獲取變量類似于tf.assign的賦值操作,將加載出來的變量值賦值于一個已有變量。與該變量的對應(yīng)關(guān)系通過Saver字典的對應(yīng)關(guān)系得出。

保存整個圖

我們知道,在進(jìn)行模型的存儲時只需要獲得之前所需要的訓(xùn)練神經(jīng)網(wǎng)絡(luò)結(jié)果,所以需要把所有的神經(jīng)網(wǎng)絡(luò)訓(xùn)練過程和結(jié)果進(jìn)行封裝,調(diào)用時只使用訓(xùn)練結(jié)果即可,所以我么可以把數(shù)據(jù)操作等都進(jìn)行封裝。
如本例,我們需要知道2.0+1.0的結(jié)果是什么,所以也應(yīng)該把加法操作一起封裝,最后只加載運(yùn)算結(jié)果即可。

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:
    init_op = tf.initialize_all_variables()
    sess.run(init_op)
    sess.run(result)
    saver.save(sess, "D:/python/ten_test/me/model.ckpt")

獲取圖

import tensorflow as tf

saver = tf.train.import_meta_graph("D:/python/ten_test/me/model.ckpt.meta")

with tf.Session() as sess:
    saver.restore(sess, "D:/python/ten_test/me/model.ckpt")
    print(sess.run(tf.get_default_graph().get_tensor_by_name("add:0")))

運(yùn)行結(jié)果:


保存的內(nèi)容不只有變量,還有關(guān)于變量的操作,所以保存的是整個圖的內(nèi)容,獲取的時候也應(yīng)該用圖來初始化saver:
在初始化saver時的參數(shù)為.ckpt.meta,是因?yàn)樵撐募楸4嬗?jì)算圖的結(jié)構(gòu)。
在sess中獲取Tensor時的命名含義為該圖中第0個add操作,如果想要自定義張量名稱,可以在初始化result時為其加上名稱:

result = tf.Variable(v1 + v2, name='result')

此時獲取結(jié)果的相應(yīng)代碼就應(yīng)改為:

tf.get_default_graph().get_tensor_by_name("result:0")

保存結(jié)果

上述結(jié)果保存的為整個圖的內(nèi)容,包括一些不需要的變量和參數(shù),但是有時不需要一些額外的信息,我們只需要將計(jì)算的結(jié)果保存為常量然后載入文件即可。
所以我們使用convert_variables_to_constants函數(shù),將結(jié)果保存為常量后寫入二進(jìn)制文件,保存的原理為將圖中的一個節(jié)點(diǎn)保存,節(jié)點(diǎn)中的張量自然也會被一同保存

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:
    init_op = tf.initialize_all_variables()
    sess.run(init_op)
    graph_def = tf.get_default_graph().as_graph_def()
    output_graph_def = graph_util.convert_variables_to_constants(
        sess, graph_def, ['add']
    )

    #可以通過輸出查看節(jié)點(diǎn)內(nèi)容
    #print(output_graph_def)

    with tf.gfile.GFile("D:/python/ten_test/graphs/combined_model.pb", "wb") as f:
        f.write(output_graph_def.SerializeToString())

讀取變量

import tensorflow as tf

with tf.Session() as sess:
    with tf.gfile.GFile("D:/python/ten_test/graphs/combined_model.pb", "rb") as f:
         string_in = f.read()
         graph_def = tf.GraphDef()
         graph_def.ParseFromString(string_in)
         result = tf.import_graph_def(graph_def, return_elements=["add:0"])
         print(sess.run(result))

運(yùn)行結(jié)果


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • “這到底是怎么回事?”腦袋不但很痛,還很亂,似乎有無數(shù)的畫面在飄過,可沒有一片記憶是清楚的,我到底是怎么了!我想著...
    白紙先生閱讀 314評論 0 3
  • 《柳永別站》 春愁極至,抒情已是往日醉。再別伊人,夜半獨(dú)自情深處。 柳生河邊,暗淚成流,與君再無訣別日。強(qiáng)顏歡歌罷...
    覓長生中醫(yī)藥閱讀 368評論 1 0
  • 同事開會時說前期的審批工作沒做好,留了許多問題需要協(xié)調(diào),我聽了感覺很生氣,身體感覺很緊繃,火往上沖,此刻內(nèi)在的對話...
    梁耀之閱讀 195評論 1 0
  • “坦然”從高中起,我就經(jīng)常在日記本里寫:“無論發(fā)生什么事,我都會坦然的面對一切。”坦然到底是什么,是一種暗示?還是...
    詩悠912閱讀 310評論 0 0

友情鏈接更多精彩內(nèi)容