tensorflow 保存模型变量

最近在学习tensorflow官方文档,地址:http://www.tensorfly.cn

不得不说,坑很多,这里记录一下。

通常我们使用 tf.train.Saver() 保存全部变量,然后在sess(会话)里使用saver.save(sess,"/...")设置路径

实例如下:

# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")


init_op = tf.initialize_all_variables()

# Add ops to save and restore all the variables.
saver = tf.train.Saver()


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, "C:\Users\ASUS\model.ckpt")
  print "Model saved in file: ", save_path

就可以成功保存了!

接下来如何使用已经保存的模型那


v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# 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, "C:\Users\ASUS\model.ckpt")
  print "Model restored."
  # Do some work with the model
  ...

注意别加入

init_op = tf.initialize_all_variables()

进行初始化!!!否则失效

  • 你可能会遇到

InvalidArgumentError (see above for traceback): Failed to create a directory

你注意检查目录,因为很可能你的目录含有中文

猜你喜欢

转载自blog.csdn.net/qq_39575835/article/details/81916160
今日推荐