https://www.cnblogs.com/denny402/p/6940134.html
https://blog.csdn.net/mieleizhi0522/article/details/80535189
https://blog.csdn.net/u012436149/article/details/56484572
_______________________________________________________
轉:https://blog.csdn.net/yushensyc/article/details/79638115
TensorFlow的文件保存與讀取——variables_to_restore函數
轉,原創詳見: http://blog.csdn.net/sinat_29957455/article/details/78508793
variables_to_restore函數,是TensorFlow為滑動平均值提供。之前,也介紹過通過使用滑動平均值可以讓神經網絡模型更加的健壯。我們也知道,其實在TensorFlow中,變量的滑動平均值都是由影子變量所維護的,如果你想要獲取變量的滑動平均值需要獲取的是影子變量而不是變量本身。
1、滑動平均值模型文件的保存
- import tensorflow as tf
- if __name__ == "__main__":
- v = tf.Variable(0.,name="v")
- #設置滑動平均模型的系數
- ema = tf.train.ExponentialMovingAverage(0.99)
- #設置變量v使用滑動平均模型,tf.all_variables()設置所有變量
- op = ema.apply([v])
- #獲取變量v的名字
- print(v.name)
- #v:0
- #創建一個保存模型的對象
- save = tf.train.Saver()
- sess = tf.Session()
- #初始化所有變量
- init = tf.initialize_all_variables()
- sess.run(init)
- #給變量v重新賦值
- sess.run(tf.assign(v,10))
- #應用平均滑動設置
- sess.run(op)
- #保存模型文件
- save.save(sess,"./model.ckpt")
- #輸出變量v之前的值和使用滑動平均模型之后的值
- print(sess.run([v,ema.average(v)]))
- #[10.0, 0.099999905]
2、滑動平均值模型文件的讀取
- v = tf.Variable(1.,name="v")
- #定義模型對象
- saver = tf.train.Saver({"v/ExponentialMovingAverage":v})
- sess = tf.Session()
- saver.restore(sess,"./model.ckpt")
- print(sess.run(v))
- #0.0999999
3、variables_to_restore函數的使用
- v = tf.Variable(1.,name="v")
- #滑動模型的參數的大小並不會影響v的值
- ema = tf.train.ExponentialMovingAverage(0.99)
- print(ema.variables_to_restore())
- #{'v/ExponentialMovingAverage': <tf.Variable 'v:0' shape=() dtype=float32_ref>}
- sess = tf.Session()
- saver = tf.train.Saver(ema.variables_to_restore())
- saver.restore(sess,"./model.ckpt")
- print(sess.run(v))
- #0.0999999
________________________________________________________