[Deep Learning] TensorFlow模型、参数的保存与读取
2017-04-23 14:41
716 查看
大部分情况,我们会把训练的网络保存下来,用于后面的使用。或者,在当前网络下对参数进行一定程度的微调。
TensorFlow中的tensorflow.train.Saver()提供了保存tf.Variable的方法。
存储变量
Weights:[[-0.97990924 1.03016174 0.74119264][-0.82581609 -0.07361362 -0.86653847]]Bias:[ 1.62978125 -0.37812829 0.64723819]
读取变量
TensorFlow中的tensorflow.train.Saver()提供了保存tf.Variable的方法。
存储变量
import tensorflow as tf # The file path to save the data save_file = './model.ckpt' # Two Tensor Variables: weights and bias weights = tf.Variable(tf.truncated_normal([2, 3])) bias = tf.Variable(tf.truncated_normal([3])) # Class used to save and/or restore Tensor Variables saver = tf.train.Saver() with tf.Session() as sess: # Initialize all the Variables sess.run(tf.global_variables_initializer()) # Show the values of weights and bias print('Weights:') print(sess.run(weights)) print('Bias:') print(sess.run(bias)) # Save the model saver.save(sess, save_file)
Weights:[[-0.97990924 1.03016174 0.74119264][-0.82581609 -0.07361362 -0.86653847]]Bias:[ 1.62978125 -0.37812829 0.64723819]
读取变量
# Remove the previous weights and bias tf.reset_default_graph() # Two Variables: weights and bias weights = tf.Variable(tf.truncated_normal([2, 3])) bias = tf.Variable(tf.truncated_normal([3])) # Class used to save and/or restore Tensor Variables saver = tf.train.Saver() with tf.Session() as sess: # Load the weights and bias saver.restore(sess, save_file) # Show the values of weights and bias print('Weight:') print(sess.run(weights)) print('Bias:') print(sess.run(bias))
相关文章推荐
- 在tensorflow中保存模型参数
- 保存和读取 TensorFlow 模型
- tensorflow模型参数与结构的保存-----二
- Tensorflow保存和读取模型
- tensorflow 4——模型的保存、读取
- tensorflow学习(4):保存模型Saver.save()的参数命名机制以及restore并创建手写字体识别引擎
- 保存及读取keras模型参数
- TensorFlow训练的模型参数(W和B)利用np.savez()保存和用cnpy库C++读取
- TensorFlow基础教程:模型持久化(模型保存与读取)
- tensorflow-模型保存与读取
- Tensorflow学习教程------模型参数和网络结构保存且载入,输入一张手写数字图片判断是几
- TensorFlow_MNIST 保存、恢复模型及参数
- tensorflow:保存与读取网络结构,参数等
- 10 Tensorflow模型保存与读取
- Tensorflow使用笔记(1) 怎么保存和使用训练好的参数和模型
- sklearn、TensorFlow、keras模型保存与读取
- 6.TensorFlow模型的保存和读取
- tensorflow: 保存和加载模型, 参数;以及使用预训练参数方法
- TensorFlow模型文件保存和读取
- 利用numpy数组保存TensorFlow模型的参数