TensorFLow用Saver保存和恢復(fù)變量
本文為大家分享了TensorFLow用Saver保存和恢復(fù)變量的具體代碼,供大家參考,具體內(nèi)容如下
建立文件tensor_save.py, 保存變量v1,v2的tensor到checkpoint files中,名稱分別設(shè)置為v3,v4。
import tensorflow as tf
# Create some variables.
v1 = tf.Variable(3, name="v1")
v2 = tf.Variable(4, name="v2")
# Create model
y=tf.add(v1,v2)
# Add an op to initialize the variables.
init_op = tf.initialize_all_variables()
# Add ops to save and restore all the variables.
saver = tf.train.Saver({'v3':v1,'v4':v2})
# Later, launch the model, initialize the variables, do some work, save the
# variables to disk.
with tf.Session() as sess:
sess.run(init_op)
print("v1 = ", v1.eval())
print("v2 = ", v2.eval())
# Save the variables to disk.
save_path = saver.save(sess, "f:/tmp/model.ckpt")
print ("Model saved in file: ", save_path)
建立文件tensor_restror.py, 將checkpoint files中名稱分別為v3,v4的tensor分別恢復(fù)到變量v3,v4中。
import tensorflow as tf
# Create some variables.
v3 = tf.Variable(0, name="v3")
v4 = tf.Variable(0, name="v4")
# Create model
y=tf.mul(v3,v4)
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
with tf.Session() as sess:
# Restore variables from disk.
saver.restore(sess, "f:/tmp/model.ckpt")
print ("Model restored.")
print ("v3 = ", v3.eval())
print ("v4 = ", v4.eval())
print ("y = ",sess.run(y))
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python日期與時(shí)間戳的各種轉(zhuǎn)換示例
這篇文章主要介紹了python日期與時(shí)間戳的各種轉(zhuǎn)換示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Python入門(mén)教程(三十七)Python中的刪除文件
這篇文章主要介紹了Python入門(mén)教程(三十七)Python中的刪除文件,在Python中,如果需要?jiǎng)h除文件,必須導(dǎo)入OS模塊,使用os模塊的remove()方法,需要的朋友可以參考下2023-05-05
python實(shí)現(xiàn)全盤(pán)掃描搜索功能的方法
今天小編就為大家分享一篇python實(shí)現(xiàn)全盤(pán)掃描搜索功能的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Python3+RIDE+RobotFramework自動(dòng)化測(cè)試框架搭建過(guò)程詳解
這篇文章主要介紹了Python3+RIDE+RobotFramework自動(dòng)化測(cè)試框架搭建過(guò)程詳解,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09

