您的位置:首页 > 其它

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
即可。



啧啧,用得着做的这么好嘛。震撼到了。。







内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: