前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TensorFlow可视化之TensorBoard快速上手

TensorFlow可视化之TensorBoard快速上手

作者头像
崔庆才
发布2018-06-25 12:06:56
5160
发布2018-06-25 12:06:56
举报
文章被收录于专栏:进击的Coder进击的Coder

本文转载自:David 9的博客 — 不怕"过拟合"

我们都知道tensorflow训练一般分两步走:第一步构建流图graph,第二步让流图真正“流”起来(即进行流图训练)

tensorboard会对这两步都进行跟踪,启动这种跟踪你必须先初始化一个tensorflow的log文件writer对象

代码语言:javascript
复制
writer = tf.train.SummaryWriter(logs_path, graph=tf.get_default_graph())

然后启动tensorboard服务:

代码语言:javascript
复制
[root@c031 mnist]# tensorboard --logdir=/tmp/mnist/2
TensorBoard 1.5.1 at http://c031:6006 (Press CTRL+C to quit)

即可看到你定义的流图:

GRAPHS中的每个方框代表tensorflow代码中的scope作用域,比如上图就定义了7个作用域:train, cross_entropy, Accuracy, softmax, input, weights, biases. 每个作用域下都可能有一些Variable或者计算操作的Tensor,可以对方框双击鼠标放大:

上图可见,input的scope下有两个placeholder:x-input和y-input.

对应的流图定义代码如下:

代码语言:javascript
复制
with tf.name_scope('input'):
    # None -> batch size can be any size, 784 -> flattened mnist image
    x = tf.placeholder(tf.float32, shape=[None, 784], name="x-input") 
    # target 10 output classes
    y_ = tf.placeholder(tf.float32, shape=[None, 10], name="y-input")

当然,截止目前,我们看到的可视化都是静态的流图可视化。

我们更多需要的是流图训练过程中动态log,现在还没有动态scalars(标量值)数据。所以我们可以定义一些log summary的操作(下面是对cost和accuracy标量打log):

代码语言:javascript
复制
# create a summary for our cost and accuracy
tf.summary.scalar("cost", cross_entropy)
tf.summary.scalar("accuracy", accuracy)

定义完成后,我们不需要逐条执行上述操作,只需用merge操作一并执行:

代码语言:javascript
复制
summary_op = tf.summary.merge_all()

最后在流图真正流动训练的时候,记得执行,并写入上述操作到log中:

代码语言:javascript
复制
# perform the operations we defined earlier on batch
_, summary = sess.run([train_op, summary_op], feed_dict={x: batch_x, y_: batch_y})

# write log
writer.add_summary(summary, epoch * batch_count + i)

add_summary 的第二项是scalar图标坐标中的x轴的值,summary对象计算出的标量是y轴的值:

如果需要复现,可以使用以下完整代码:

代码语言:javascript
复制
import tensorflow as tf

# reset everything to rerun in jupyter
tf.reset_default_graph()

# config
batch_size = 100
learning_rate = 0.5
training_epochs = 5
logs_path = "/tmp/mnist/2"

# load mnist data set
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

# input images
with tf.name_scope('input'):
    # None -> batch size can be any size, 784 -> flattened mnist image
    x = tf.placeholder(tf.float32, shape=[None, 784], name="x-input") 
    # target 10 output classes
    y_ = tf.placeholder(tf.float32, shape=[None, 10], name="y-input")

# model parameters will change during training so we use tf.Variable
with tf.name_scope("weights"):
    W = tf.Variable(tf.zeros([784, 10]))

# bias
with tf.name_scope("biases"):
    b = tf.Variable(tf.zeros([10]))

# implement model
with tf.name_scope("softmax"):
    # y is our prediction
    y = tf.nn.softmax(tf.matmul(x,W) + b)

# specify cost function
with tf.name_scope('cross_entropy'):
    # this is our cost
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

# specify optimizer
with tf.name_scope('train'):
    # optimizer is an "operation" which we can execute in a session
    train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)

with tf.name_scope('Accuracy'):
    # Accuracy
    correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# create a summary for our cost and accuracy
tf.summary.scalar("cost", cross_entropy)
tf.summary.scalar("accuracy", accuracy)

# merge all summaries into a single "operation" which we can execute in a session 
summary_op = tf.summary.merge_all()

with tf.Session() as sess:
    # variables need to be initialized before we can use them
    sess.run(tf.initialize_all_variables())

    # create log writer object
    writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph())

    # perform training cycles
    for epoch in range(training_epochs):

        # number of batches in one epoch
        batch_count = int(mnist.train.num_examples/batch_size)

        for i in range(batch_count):
            batch_x, batch_y = mnist.train.next_batch(batch_size)

            # perform the operations we defined earlier on batch
            _, summary = sess.run([train_op, summary_op], feed_dict={x: batch_x, y_: batch_y})

            # write log
            writer.add_summary(summary, epoch * batch_count + i)

        if epoch % 5 == 0: 
            print "Epoch: ", epoch 
    print "Accuracy: ", accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels})
    print "done"

参考文献

http://ischlag.github.io/2016/06/04/how-to-use-tensorboard/

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-04-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 进击的Coder 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 参考文献
相关产品与服务
灰盒安全测试
腾讯知识图谱(Tencent Knowledge Graph,TKG)是一个集成图数据库、图计算引擎和图可视化分析的一站式平台。支持抽取和融合异构数据,支持千亿级节点关系的存储和计算,支持规则匹配、机器学习、图嵌入等图数据挖掘算法,拥有丰富的图数据渲染和展现的可视化方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档