tensorflow中允许将训练的variable存储起来,下次读取并接着训练。
存储saver:
import tensorflow as tf
import numpy as np #使用numpy代码会省事些
#Save to file
#remember to define the same dtype and shape when restore
W = tf.Variable([[1,2,3],[3,4,5]],dtype=tf.float32,name='weights') #最好定义下dtype
b = tf.Variable([[1,2,3]],dtype=tf.float32,name='biases')
init = tf.global_variables_initializer()
#定义tensorflow的saver,用它来存储各种变量
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init)
#开始保存的这一步了 这里保存后会返回一个值,保存在save_path中 保存为ckpt后缀
save_path = saver.save(sess,"my_net/save_net.ckpt") #restore的时候会把saver换为restore
print("Save to path:",save_path)
读取restore:
import tensorflow as tf
import numpy as np #使用numpy代码会省事些
#restore variables
#redefine the same shape and same type f