Tensorflow学习精要版IV ---- 开始稍微深入了解一下
2017-02-12 22:48
796 查看
变量
创建
# Create two variables. weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35), name="weights") biases = tf.Variable(tf.zeros([200]), name="biases")
设备设置
# Pin a variable to CPU. with tf.device("/cpu:0"): v = tf.Variable(...) # Pin a variable to GPU. with tf.device("/gpu:0"): v = tf.Variable(...) # Pin a variable to a particular parameter server task. with tf.device("/job:ps/task:7"): v = tf.Variable(...)
初始化
# Create two variables. weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35), name="weights") biases = tf.Variable(tf.zeros([200]), name="biases")...
# Add an op to initialize the variables.
init_op = tf.global_variables_initializer()
# Later, when launching the model
with tf.Session() as sess:
# Run the init operation.
sess.run(init_op)
...
# Use the model
...
从另一个变量进行初始化
# Create a variable with a random value. weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35), name="weights") # Create another variable with the same value as 'weights'. w2 = tf.Variable(weights.initialized_value(), name="w2") # Create another variable with twice the value of 'weights' w_twice = tf.Variable(weights.initialized_value() * 2.0, name="w_twice")
保存与加载
一般使用tf.train.Saver
# Create some variables. v1 = tf.Variable(..., name="v1") v2 = tf.Variable(..., name="v2") ... # Add an op to initialize the variables. init_op = tf.initialize_all_variables() # Add ops to save and restore all the variables. saver = tf.train.Saver() # Later, launch the model, initialize the variables, do some work, save the # variables to disk. 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, "/tmp/model.ckpt") print "Model saved in file: ", save_path
加载时直接在tf.Session下面进saver.restore即可。不说预先初始化了,因为你初始化了还是会替换成checkpoints文件的变量的值
# Create some variables. 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, "/tmp/model.ckpt") print("Model restored.") # Do some work with the model ...
如果要只加载特定的变量,
# Add ops to save and restore only 'v2' using the name "my_v2" saver = tf.train.Saver({"my_v2": v2})
TensorBoard
可视化训练过程
scalar_summary用于学习率和loss等的可视化,而
histogram_summary针对权值或是梯度的可视化。那么我们需要将这些summary 节点进行汇总一下,可以用
tf.merge_all_summaries进行合并。执行合并命令时,会将产生的数据生成一个Summary protobuf对象,将这个protobuf传给tf.train.SummaryWriter写入磁盘。
SummaryWriter构造函数包括logdir。所有的事件都会写入该目录。
merged_summary_op = tf.merge_all_summaries() summary_writer = tf.train.SummaryWriter('/tmp/mnist_logs', sess.graph) total_step = 0 while training: total_step += 1 session.run(training_op) if total_step % 100 == 0: summary_str = session.run(merged_summary_op) summary_writer.add_summary(summary_str, total_step)
启动TensorBoard
tensorboard --logdir=/tmp/cifar10_train
然后浏览器输入
0.0.0.0:6006即可。
啧啧,用得着做的这么好嘛。震撼到了。。
相关文章推荐
- 想深入了解编程或系统内部,我想大家有必要学习一下汇编
- 想深入学习一下IoC,这两天试验hivemind1.0,没有成功!
- c++学习笔记-------《c++自学通》第七章 深入了解类
- 开始学习模拟器,会在这里留一下爪印
- 深入学习一下JavaScript的三种编解码方式
- 终于稍微完整的学习了一下Linux
- http协议学习系列--深入了解篇
- 深入了解GPU--学习教材 (摘自opengpu)
- 有空看完<Beginning Xml with C# 2008>这本书, 深入学习一下Xml.
- 从现在开始,把学习资料整理一下吧
- 从现在开始学习一下在 Office 平台上进行开发一些简单的应用程序,欢迎有从事这方面的高手和同道指点交流。
- 开始重新系统学习一下javascript
- 开始写点东西,记录一下自己学习 .net
- StringTemplate.Net 学习笔记(9):深入了解模板组文件
- 立志!开始Linux的深入学习!
- 开始深入学习了
- C/C++学习笔记1 - 深入了解scanf()/getchar()和gets()等函数(原创)
- 开始学习windows api了,写个hello,world纪念一下
- [学习jquery]深入了解jquery(1)-jquery对象