TensorFlow利用saver保存和提取参数
tensorflow
一个面向所有人的开源机器学习框架
项目地址:https://gitcode.com/gh_mirrors/te/tensorflow

·
在训练循环中,定期调用 saver.save() 方法,向文件夹中写入包含了当前模型中所有可训练变量的 checkpoint 文件。
saver.save(sess, FLAGS.train_dir, global_step=step)
global_step是训练的第几步
保存参数:
import tensorflow as tf
W = tf.Variable([[1, 2, 3]], dtype=tf.float32)
b = tf.Variable([[1]], dtype=tf.float32)
saver = tf.train.Saver()
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
# 必须要指定文件夹,保存到ckpt文件
save_path = saver.save(sess, "winycg/1.ckpt")
print(save_path)
一次 saver.save() 后可以在文件夹中看到新增的四个文件,实际上每调用一次保存操作会创建后3个数据文件并创建一个检查点(checkpoint)文件,简单理解就是权重等参数被保存到 .chkp.data 文件中,以字典的形式;图和元数据被保存到 .chkp.meta 文件中,可以被 tf.train.import_meta_graph 加载到当前默认的图。
读取参数:
import tensorflow as tf
import numpy as np
W = tf.Variable(np.arange(3).reshape(1, 3), dtype=tf.float32)
b = tf.Variable(np.arange(1).reshape(1, 1), dtype=tf.float32)
saver = tf.train.Saver()
sess = tf.InteractiveSession()
# 读取参数时不需要global_variables_initializer()
save_path = saver.restore(sess, "parameter/1.ckpt")
print("weights:", sess.run(W))
print("bias:", sess.run(b))
weights: [[ 1. 2. 3.]]
bias: [[ 1.]]




一个面向所有人的开源机器学习框架
最近提交(Master分支:1 个月前 )
4f64a3d5
Instead, check for this case in `ResolveUsers` and `ResolveOperand`, by querying whether the `fused_expression_root` is part of the `HloFusionAdaptor`.
This prevents us from stepping into nested fusions.
PiperOrigin-RevId: 724311958
1 个月前
aa7e952e
Fix a bug in handling negative strides, and add a test case that exposes it.
We can have negative strides that are not just -1, e.g. with a combining
reshape.
PiperOrigin-RevId: 724293790
1 个月前
更多推荐
所有评论(0)