您的位置:首页 > 其它

tensorflow中变量的保存和加载

2017-10-24 13:17 357 查看
TensorFlow中我们可以保存全部变量也可以保存部分变量

下面是保存全部变量的代码(保存变量的时候要全部初始化)

import tensorflow as tf
# Create some variables.
v1 = tf.Variable('v1', name="v1")
v2 = tf.Variable('v2', name="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()

# Later, launch the model, initialize the variables, do some work, save the
# variables to disk.
with tf.Session() as sess:
sess.run(init_op)
# Save the variables to disk.
save_path = saver.save(sess, "/tmp/model.ckpt")
print("Model saved in file: ", save_path)


加载保存的全部变量

import tensorflow as tf
# Create some variables.
v1 = tf.Variable("v2", name="v1")
v2 = tf.Variable("v1", name="v2")
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
# init=tf.global_variables_initializer()
# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
with tf.Session() as sess:
# sess.run(init)
# Restore variables from disk.
saver.restore(sess, "/tmp/model.ckpt")
print("Model restored.")
print(sess.run(v2))
# Do some work with the model


实验结果:

C:\python35\python.exe C:/Users/User/PycharmProjects/nlpdemo/tensorflowlearn/use_save_variables.py
2017-10-24 13:11:20.610000: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-10-24 13:11:20.610000: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
Model restored.
b'v2'

Process finished with exit code 0


保存一部分代码:

import tensorflow as tf
# Create some variables.
v1 = tf.Variable("v2", name="v1")
v2 = tf.Variable("v2", name="v2")
# Add ops to save and restore only 'v2' using the name "my_v2"
init_op = tf.initialize_all_variables()
saver = tf.train.Saver({"my_v2": v2})
# Use the saver object normally after that.
with tf.Session() as sess:
# Restore variables from disk.
sess.run(init_op)
# Save the variables to disk.
save_path = saver.save(sess, "/tmp/model_one.ckpt")
print("Model saved in file: ", save_path)


加载一部分变量(注意加载变量之前先保存变量,记住key的对应)

import tensorflow as tf
# Create some variables.
v1 = tf.Variable("v2", name="v1")
my_v2 = tf.Variable("v1", name="my_v2")
# Add ops to save and restore all the variables.
saver = tf.train.Saver({"my_v2": my_v2})
# init=tf.global_variables_initializer()
# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
with tf.Session() as sess:
# sess.run(init)
# Restore variables from disk.
saver.restore(sess, "/tmp/model_one.ckpt")
print("Model restored.")
print(sess.run(my_v2))
# Do some work with the model


实验结果:

C:\python35\python.exe C:/Users/User/PycharmProjects/nlpdemo/tensorflowlearn/use_some_save_variables.py
2017-10-24 13:15:06.539000: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-10-24 13:15:06.539000: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
Model restored.
b'v2'

Process finished with exit code 0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐