遵循:BY-SA
作者:譚東
時間:2017年6月10日
環境:Windows 7
TensorBoard是TensorFlow自帶的可視化結構管理和調試優化網絡的工具。在我們學習深度學習網絡框架時,我們需要更直觀的看到各層網絡結構和參數,也可以更好的進行調試優化網絡。TensorBoard可以實現網絡結構的顯示,也可以進行顯示訓練及測試過程中各層參數的變化情況。
我們先看下TensorBoard的大致界面。
我們可以看到頂部有幾個功能分類:SCALARS、IMAGES、AUDIO、GRAPHS等。
SCALARS是訓練參數統計顯示,可以看到整個訓練過程中,各個參數的變換情況。
官方英文翻譯:
TensorBoard的標量儀表板可視化隨時間變化的標量統計; 例如,您可能需要跟蹤模型的損失或學習率。 如關鍵概念所述,您可以比較多個運行,數據按標籤組織。 折線圖具有以下交互作用:
- 點擊每個圖表左下角的小藍色圖標將展開圖表
- 在圖表上拖動矩形區域將放大
- 雙擊圖表將縮小
- 鼠標在圖表上會產生十字準線,數據值記錄在左側的運行選擇器中。
IMAGES輸入和輸出標籤。
官方翻譯:
圖像儀表板可以顯示通過tf.image_summary保存的png。 設置儀表板,使每行對應一個不同的標籤,每列對應一個運行。 由於圖像顯示板支持任意的png,您可以使用它將自定義可視化(例如,matplotlib散點圖)嵌入到TensorBoard中。 此儀表板總是顯示每個標籤的最新圖像。
AUDIO官方英文翻譯:
音頻儀表板可以嵌入通過tf.audio_summary保存的音頻的可播放音頻小部件。 設置儀表板,使每行對應一個不同的標籤,每列對應一個運行。 此儀表板將爲每個標籤嵌入最新的音頻。
GRAPH是網絡結構顯示。
官方英文翻譯:
圖形瀏覽器可以顯示TensorBoard圖形,從而可以檢查TensorFlow模型。 爲了最好地利用圖形可視化程序,您應該使用名稱範圍來對圖形中的op進行分層分組,否則圖形可能難以破譯。 有關更多信息,包括示例,請參閱圖形可視化程序教程。
HISTOGRAM是訓練過程參數分佈情況顯示。
官方英文翻譯:
直方圖儀表板用於可視化Tensor的統計分佈隨時間變化。它可視化通過tf.histogram_summary記錄的數據。現在,它的名字有點不正確,因爲它不顯示直方圖;相反,它顯示了一些關於分配的高級統計數據。圖表上的每一行表示數據分佈中的百分位數:例如,底線顯示了最小值隨時間變化的方式,中間的行顯示了中位數的變化。從上到下,行具有以下含義:[最大,93%,84%,69%,50%,31%,16%,7%,最低]。
這些百分位數也可以視爲正態分佈的標準偏差邊界:[最大值,μ+1.5σ,μ+σ,μ+0.5σ,μ,μ-0.5σ,μ-σ,μ-1.5σ,最小值]使得從內到外讀取的着色區域分別具有寬度[σ,2σ,3σ]。
這種直方圖可視化有點奇怪,不能有意義地表示多模態分佈。我們正在研究一個真正的直方圖替換。
官方英文介紹地址:https://github.com/tensorflow/tensorflow/tree/r0.9/tensorflow/tensorboard
注意,本文是在Windows下運行的。
首先我們先寫一個Python代碼,用來運行顯示圖形層級結構。
"""
Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
"""
from __future__ import print_function
import tensorflow as tf
def add_layer(inputs, in_size, out_size, activation_function=None):
# add one more layer and return the output of this layer
with tf.name_scope('layer'):
with tf.name_scope('weights'):
Weights = tf.Variable(tf.random_normal([in_size, out_size]), name='W')
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='b')
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.add(tf.matmul(inputs, Weights), biases)
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b, )
return outputs
# define placeholder for inputs to network
with tf.name_scope('inputs'):
xs = tf.placeholder(tf.float32, [None, 1], name='x_input')
ys = tf.placeholder(tf.float32, [None, 1], name='y_input')
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)
# the error between prediciton and real data
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
reduction_indices=[1]))
with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
sess = tf.Session()
# tf.train.SummaryWriter soon be deprecated, use following
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1: # tensorflow version < 0.12
writer = tf.train.SummaryWriter('C:/logs/', sess.graph)
else: # tensorflow version >= 0.12
writer = tf.summary.FileWriter("C:/logs/", sess.graph)
# tf.initialize_all_variables() no long valid from
# 2017-03-02 if using tensorflow >= 0.12
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
init = tf.initialize_all_variables()
else:
init = tf.global_variables_initializer()
sess.run(init)
# direct to the local dir and run this in terminal:
# $ tensorboard --logdir=logs
然後點擊Run運行Python代碼。
然後CMD打開命令行終端輸入:
tensorboard --logdir=C:/logs
一切正常的話,我們用chrome瀏覽器打開這個地址:
http://localhost:6006/
因爲我們代碼只是展示了網絡層結構,並沒有數據。所以我們點擊GRAPHS進行查看網絡層結構就可以了。
我們的C盤logs下也有相應的日誌文件。
版權所有,尊重版權。
微信公衆號: