tensorflow保存与提取变量

# Create some variables.
import  tensorflow as tf
v1 = tf.Variable(5, name="v1")
v2 = tf.Variable(10, 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({'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)
  # Do some work with the model.
  # Save the variables to disk.
  save_path = saver.save(sess, "./tmp/model.ckpt")

print("Model saved in file: ", save_path)

# 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())

猜你喜欢

转载自blog.csdn.net/m0_37598149/article/details/80747041