TensorFlow可视化之TensorBoard快速上手

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

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

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

writer = tf.train.SummaryWriter(logs_path, graph=tf.get_default_graph())

然后启动tensorboard服务:

[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.

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

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):

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

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

summary_op = tf.summary.merge_all()

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

# 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轴的值:

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

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/

原文发布于微信公众号 - 进击的Coder(FightingCoder)

原文发表时间:2018-04-22

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏奇点大数据

【干货】Pytorch中的DataLoader的相关记录

DataLoader简单介绍 DataLoader是Pytorch中用来处理模型输入数据的一个工具类。通过使用DataLoader,我们可以方便地对数据进行...

7035
来自专栏人工智能LeadAI

机器学习实战 | 第五章:模型保存(持久化)

一、工具 sklearn官方给出了两种保存模型的方式:3.4. Model persistence 其中一种是pickle的方式,还有一种就是joblib包的...

2998
来自专栏生信小驿站

机器学习实战(2)之预测房价

数据预处理要点: 1.使用log(x+1)来转换偏斜的数字特征 -,这将使我们的数据更加正常 2.为分类要素创建虚拟变量 3.将数字缺失值(NaN)替换为...

873
来自专栏Deep Learning 笔记

CNN+MNIST+INPUT_DATA数字识别

TALK IS CHEAP,SHOW ME THE CODE,先从MNIST数据集下载脚本Input_data开始

1333
来自专栏机器之心

令人困惑的TensorFlow!

我叫 Jacob,是 Google AI Resident 项目的研究学者。我是在 2017 年夏天加入该项目的,尽管已经拥有了丰富的编程经验,并且对机器学习的...

1143
来自专栏华章科技

令人困惑的TensorFlow!谷歌大脑工程师帮你解决麻烦

导读:虽然对于大多数人来说 TensorFlow 的开发语言是 Python,但它并不是一个标准的 Python 库。这个神经网络框架通过构建「计算图」来运行,...

1273
来自专栏zingpLiu

机器学习之线性代数

  完整内容已上传到github:https://github.com/ZingP/machine-learning/tree/master/linear_al...

781
来自专栏Petrichor的专栏

tensorflow: tf.assign探究

  Defined in tensorflow/python/ops/state_ops.py.

723
来自专栏云时之间

深度学习与神经网络:制作数据集,完成应用(1)

1464
来自专栏锦小年的博客

Nilearn学习笔记3-提取时间序列建立功能连接体

在nilearn库中,提供了两种从fmri数据中提取时间序列的方法,一种基于脑分区(Time-series from a brain parcellation ...

2145

扫码关注云+社区