「TensorFlow 1.0」 Tensorboard

TensorFlow Tensorboard

本文主要介紹 TensorFlow 的 Tensorboard 模塊。

Tensorboard 可以看做是我們構建的 Graph 的可視化工具,對于我們初學者理解網絡架構、每層網絡的細節都是很有幫助的。由于前幾天剛接觸 TensorFlow,所以在嘗試學習 Tensorboard 的過程中,遇到了一些問題。在此基礎上,參考了 TensorFlow 官方的 Tensorboard Tutorials 以及網上的一些文章。由于前不久 TensorFlow 1.0 剛發布,網上的一些學習資源或者是 tensorboard 代碼在新的版本中并不適用,所以自己改寫并實現了官方網站上提及的三個實例的 Tensorboard 版本:

  1. 最基礎簡單的「linear model」
  2. 基于 MNIST 手寫體數據集的 「softmax regression」模型
  3. 基于 MNIST 手寫體數據集的「CNN」模型

文章不會詳細介紹 TensorFlow 以及 Tensorboard 的知識,主要是模型的代碼以及部分模型實驗截圖。

注意:文章前提默認讀者們知曉 TensorFlow,知曉 Tensorboard,以及 TensorFlow 的一些主要概念「Variables」、「placeholder」。還有,默認你已經將需要用到的 MNIST 數據集下載到了你代碼當前所在文件夾。

Environment

OS: macOS Sierra 10.12.x

Python Version: 3.4.x

TensorFlow: 1.0

Tensorboard

Tensorboard有幾大模塊:

  • SCALARS:記錄單一變量的,使用 tf.summary.scalar() 收集構建。
  • IMAGES:收集的圖片數據,當我們使用的數據為圖片時(選用)。
  • AUDIO:收集的音頻數據,當我們使用數據為音頻時(選用)。
  • GRAPHS:構件圖,效果圖類似流程圖一樣,我們可以看到數據的流向,使用tf.name_scope()收集構建。
  • DISTRIBUTIONS:用于查看變量的分布值,比如 W(Weights)變化的過程中,主要是在 0.5 附近徘徊。
  • HISTOGRAMS:用于記錄變量的歷史值(比如 weights 值,平均值等),并使用折線圖的方式展現,使用tf.summary.histogram()進行收集構建。

Examples

  • 最簡單的線性回歸模型(tensorboard 繪圖)
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

def add_layer(layoutname, inputs, in_size, out_size, act = None):
    with tf.name_scope(layoutname):
        with tf.name_scope('weights'):
            weights = tf.Variable(tf.random_normal([in_size, out_size]), name = 'weights')
            w_hist = tf.summary.histogram('weights', weights)
        with tf.name_scope('biases'):
            biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name = 'biases')
            b_hist = tf.summary.histogram('biases', biases)
        with tf.name_scope('Wx_plus_b'):
            Wx_plus_b = tf.add(tf.matmul(inputs, weights), biases)

        if act is None:
            outputs = Wx_plus_b
        else :
            outputs = act(Wx_plus_b)
        return outputs

x_data = np.linspace(-1, 1, 300)[:,np.newaxis]
noise = np.random.normal(0,0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

with tf.name_scope('Input'):
    xs = tf.placeholder(tf.float32, [None, 1], name = "input_x")
    ys = tf.placeholder(tf.float32, [None, 1], name = "target_y")


l1 = add_layer("first_layer", xs, 1, 10, act = tf.nn.relu)
l1_hist = tf.summary.histogram('l1', l1)

y = add_layer("second_layout", l1, 10, 1, act = None)
y_hist = tf.summary.histogram('y', y)

with tf.name_scope('loss'): 
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - y), 
                            reduction_indices = [1]))
    tf.summary.histogram('loss ', loss)
    tf.summary.scalar('loss', loss)

with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

init = tf.global_variables_initializer()
merged = tf.summary.merge_all()

with tf.Session() as sess:
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.scatter(x_data, y_data)
    plt.ion()
    plt.show()
    
    writer = tf.summary.FileWriter('logs/', sess.graph)
    sess.run(init)
    
    for train in range(1000):
        sess.run(train_step, feed_dict = {xs: x_data, ys: y_data})
        if train % 50 == 0:
            try:
                ax.lines.remove(lines[0])
            except Exception:
                pass
            summary_str = sess.run(merged, feed_dict = {xs: x_data, ys: y_data})
            writer.add_summary(summary_str, train)

            print(train, sess.run(loss, feed_dict = {xs: x_data, ys: y_data}))
            
            prediction_value = sess.run(y, feed_dict = {xs: x_data})
            lines = ax.plot(x_data, prediction_value, 'r-', lw = 5)
            plt.pause(1)
  • 基於 Softmax Regressions 的 MNIST 數據集(tensorboard 繪圖)
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np

def add_layer(layoutname, inputs, in_size, out_size, act = None):
    with tf.name_scope(layoutname):
        with tf.name_scope('weights'):
            weights = tf.Variable(tf.zeros([in_size, out_size]), name = 'weights')
            w_hist = tf.summary.histogram("weights", weights)
        with tf.name_scope('biases'):
            biases = tf.Variable(tf.zeros(out_size), name = 'biases')
            b_hist = tf.summary.histogram("biases", biases)
        with tf.name_scope('Wx_plus_b'):
            Wx_plus_b = tf.add(tf.matmul(inputs, weights), biases)
        
        if act is None:
            outputs = Wx_plus_b
        else:
            outputs = act(Wx_plus_b)
        return outputs
        
# Import data
mnist_data_path = 'MNIST_data/'
mnist = input_data.read_data_sets(mnist_data_path, one_hot = True)

with tf.name_scope('Input'):
    x = tf.placeholder(tf.float32, [None, 28 * 28], name = 'input_x')
    y_ = tf.placeholder(tf.float32, [None, 10], name = 'target_y')

y = add_layer("hidden_layout", x, 28*28, 10, act = tf.nn.softmax)
y_hist = tf.summary.histogram('y', y)

# labels 真實值 logits 預測值
with tf.name_scope('loss'):
    cross_entroy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_,
                    logits = y))
    tf.summary.histogram('cross entropy', cross_entroy)
    tf.summary.scalar('cross entropy', cross_entroy)

with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entroy)

# Test trained model
with tf.name_scope('test'):
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar('accuracy', accuracy)

init = tf.global_variables_initializer()
merged = tf.summary.merge_all()

with tf.Session() as sess:
    #logpath = r'/Users/randolph/PycharmProjects/TensorFlow/logs'
    writer = tf.summary.FileWriter('logs/', sess.graph)
    sess.run(init)

    for i in range(1000):
        if i % 10 == 0:
            feed = {x: mnist.test.images, y_: mnist.test.labels}
            result = sess.run([merged, accuracy], feed_dict = feed)
            summary_str = result[0]
            acc = result[1]
            writer.add_summary(summary_str, i)
            print(i, acc)
        else:
            batch_xs, batch_ys = mnist.train.next_batch(100)
            feed = {x: batch_xs, y_: batch_ys}
            sess.run(train_step, feed_dict = feed)

    print('final result: ', sess.run(accuracy, feed_dict = {x: mnist.test.images, y_: mnist.test.labels}))
  • 基於 CNN 的 MNIST 數據集(tensorboard 繪圖)
# 基于 MNIST 數據集 的 「CNN」(tensorboard 繪圖)
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev = 0.1)
    return tf.Variable(initial)
    
def bias_variable(shape):
    initial = tf.constant(0.1, shape = shape)
    return tf.Variable(initial)

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    
def variable_summaries(var):
    """Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""
    with tf.name_scope('summaries'):
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean', mean)
        with tf.name_scope('stddev'):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
        tf.summary.scalar('stddev', stddev)
        tf.summary.scalar('max', tf.reduce_max(var))
        tf.summary.scalar('min', tf.reduce_min(var))
        tf.summary.histogram('histogram', var)

def add_layer(input_tensor, weights_shape, biases_shape, layer_name, act = tf.nn.relu, flag = 1):
    """Reusable code for making a simple neural net layer.

    It does a matrix multiply, bias add, and then uses relu to nonlinearize.
    It also sets up name scoping so that the resultant graph is easy to read,
    and adds a number of summary ops.
    """
    with tf.name_scope(layer_name):
        with tf.name_scope('weights'):
            weights = weight_variable(weights_shape)
            variable_summaries(weights)
        with tf.name_scope('biases'):
            biases = bias_variable(biases_shape)
            variable_summaries(biases)
        with tf.name_scope('Wx_plus_b'):
            if flag == 1:
                preactivate = tf.add(conv2d(input_tensor, weights), biases)
            else:
                preactivate = tf.add(tf.matmul(input_tensor, weights), biases)
            tf.summary.histogram('pre_activations', preactivate)
        if act == None:
            outputs = preactivate
        else:
            outputs = act(preactivate, name = 'activation')
            tf.summary.histogram('activation', outputs)
        return outputs

def main():
    # Import data
    mnist_data_path = 'MNIST_data/'
    mnist = input_data.read_data_sets(mnist_data_path, one_hot = True)
    
    with tf.name_scope('Input'):
        x = tf.placeholder(tf.float32, [None, 28*28], name = 'input_x')
        y_ = tf.placeholder(tf.float32, [None, 10], name = 'target_y')

    # First Convolutional Layer
    x_image = tf.reshape(x, [-1, 28, 28 ,1])
    conv_1 = add_layer(x_image, [5, 5, 1, 32], [32], 'First_Convolutional_Layer', flag = 1)
    
    # First Pooling Layer
    pool_1 = max_pool_2x2(conv_1)
    
    # Second Convolutional Layer 
    conv_2 = add_layer(pool_1, [5, 5, 32, 64], [64], 'Second_Convolutional_Layer', flag = 1)

    # Second Pooling Layer 
    pool_2 = max_pool_2x2(conv_2)

    # Densely Connected Layer
    pool_2_flat = tf.reshape(pool_2, [-1, 7*7*64])
    dc_1 = add_layer(pool_2_flat, [7*7*64, 1024], [1024], 'Densely_Connected_Layer', flag = 0) 
    
    # Dropout
    keep_prob = tf.placeholder(tf.float32)
    dc_1_drop = tf.nn.dropout(dc_1, keep_prob)
    
    # Readout Layer
    y = add_layer(dc_1_drop, [1024, 10], [10], 'Readout_Layer', flag = 0)
    
    # Optimizer
    with tf.name_scope('cross_entroy'):
        cross_entroy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_,
                        logits = y))
        tf.summary.scalar('cross_entropy', cross_entroy)
        tf.summary.histogram('cross_entropy', cross_entroy)
    
    # Train
    with tf.name_scope('train'):
        train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entroy)
    
    # Test
    with tf.name_scope('accuracy'):
        with tf.name_scope('correct_prediction'):
            correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        with tf.name_scope('accuracy'):
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        tf.summary.scalar('accuracy', accuracy)
        
    sess = tf.InteractiveSession()
    merged = tf.summary.merge_all()
    train_writer = tf.summary.FileWriter('train/', sess.graph)
    test_writer = tf.summary.FileWriter('test/')
    tf.global_variables_initializer().run()

    def feed_dict(train):
        if train:
            batch_xs, batch_ys = mnist.train.next_batch(100)
            k = 0.5
        else:
            batch_xs, batch_ys = mnist.test.images, mnist.test.labels
            k = 1.0
        return {x: batch_xs, y_: batch_ys, keep_prob: k}
    
    # Train the model, and also write summaries.
    # Every 10th step, measure test-set accuracy, and write test summaries
    # All other steps, run train_step on training data, & add training summaries
    for i in range(10000):
        if i % 10 == 0: # Record summaries and test-set accuracy
            summary, acc = sess.run([merged, accuracy], feed_dict = feed_dict(False))
            test_writer.add_summary(summary, i)
            print("step %d, training accuracy %g" %(i, acc))
        else:   # Record train set summaries, and train
            if i % 100 == 99:   # Record execution stats
                run_options = tf.RunOptions(trace_level = tf.RunOptions.FULL_TRACE)
                run_metadata = tf.RunMetadata()
                summary, _ = sess.run([merged, train_step], feed_dict = feed_dict(True), 
                                        options = run_options, run_metadata = run_metadata)
                train_writer.add_run_metadata(run_metadata, 'step %d ' % i)
                train_writer.add_summary(summary, i)
                print('Adding run metadata for', i)
            else:
                summary, _ = sess.run([merged, train_step], feed_dict = feed_dict(True))
                train_writer.add_summary(summary, i)
main()

可能對于最后一個模型 CNN 的代碼,需要一些 CNN 卷積神經網絡的一些知識。例如什么是卷積、池化,還需要了解 TensorFlow 中用到的相應函數,例如tf.nn.conv2d()tf.nn.max_pool(),這里不再贅述。

貼上最后一個模型的部分截圖:

  • 代碼部分:

說明:上圖右側是 CNN 網絡訓練的步數以及對應的結果,程序需要運行挺久時間的,CPU 占用率也很高,建議掛在晚上跑,人去休息睡覺。總之,你們可以修改那個 range(10000),請量力而為


上述代碼運行完成之后,命令行中跳轉到代碼生成的「train」文件夾中(其和代碼文件存在于同一文件夾中),然后輸入 tensorboard --logdir .,等待程序反應之后,瀏覽器訪問localhost:6006(當然你也可以自己定義端口)。如果不出意外,你會得到以下內容:

  • Scalars:

  • Graphs:

  • Distributions:

  • Histograms:

關于各個模塊的作用,以及各個變量的意義,我在此就不再贅述了。

如果有讀者對于 CNN 卷積神經網絡有些陌生或者是遺忘,可以參考我的另外一篇文章 CNN on TensorFlow

另外,如果讀者們想在模型訓練期間,做些「有趣的」事情,可以參考我的另一篇文章 Use WeChat to Monitor Your Network

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,565評論 6 539
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,115評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,577評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,514評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,234評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,621評論 1 326
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,641評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,822評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,380評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,128評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,319評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,879評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,548評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,970評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,229評論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,048評論 3 397
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,285評論 2 376

推薦閱讀更多精彩內容