TensorBoard簡介 Tensorflow發佈包中提供了TensorBoard,用於展示Tensorflow任務在計算過程中的Graph、定量指標圖以及附加數據。大致的效果如下所示, TensorBoard工作機制 TensorBoard 通過讀取 TensorFlow 的事件文件來運行。Te ...
TensorBoard簡介
Tensorflow發佈包中提供了TensorBoard,用於展示Tensorflow任務在計算過程中的Graph、定量指標圖以及附加數據。大致的效果如下所示,
TensorBoard工作機制
TensorBoard 通過讀取 TensorFlow 的事件文件來運行。TensorFlow 的事件文件包括了你會在 TensorFlow 運行中涉及到的主要數據。關於TensorBoard的詳細介紹請參考TensorBoard:可視化學習。下麵做個簡單介紹。
Tensorflow的API中提供了一種叫做Summary的操作,用於將Tensorflow計算過程的相關數據序列化成字元串Tensor。例如標量數據的圖表scalar_summary或者梯度權重的分佈histogram_summary。
通過tf.train.SummaryWriter來將序列化後的Summary數據保存到磁碟指定目錄(通過參數logdir指定)。此外,SummaryWriter構造函數還包含了一個可選參數GraphDef,通過指定該參數,可以在TensorBoard中展示Tensorflow中的Graph(如上圖所示)。
大致的代碼框架如下所示:
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的命令如下,
python tensorflow/tensorboard/tensorboard.py --logdir=/tmp/mnist_logs
其中--logdir命令行參數指定的路徑必須跟SummaryWriter的logdir參數值保持一致,TensorBoard才能夠正確讀取到Tensorflow的事件文件。
啟動Tensorflow後,我們在瀏覽器中輸入http://localhost:6006 即可訪問TensorBoard頁面了。
通過MNIST實例來驗證TensorBoard
tensorflow/tensorflow的源代碼目錄tensorflow/examples/tutorials/mnist目錄下提供了手寫數字MNIST識別樣例代碼。該樣例代碼同樣包含了SummaryWriter的相關代碼,我們可以使用該樣例代碼來驗證一下TensorBoard的效果。
首先,克隆一下tensorflow的代碼庫到本地,
$ git clone https://github.com/tensorflow/tensorflow.git $ cd tensorflow/examples/tutorials/mnist/ $ emacs fully_connected_feed.py
對fully_connected_feed.py的代碼做一下下麵兩個地方的修改:
-
將29、30行的import語句修改一下
import input_data import mnist
-
將154行的FLAGS.train_dir修改成'/opt/tensor':
# Instantiate a SummaryWriter to output summaries and the Graph. summary_writer = tf.train.SummaryWriter('/opt/tensor', sess.graph)
樣例代碼準備好了,下麵我們如何啟動TensorBoard。
Tensorflow官方的Docker鏡像tensorflow/tensorflow提供了一個可快速使用Tensorflow的途徑。不過該鏡像預設啟動的是jupyter。我們通過下麵命令通過該鏡像啟動TensorBoard,並且將我們準備好的MNIST樣例代碼通過volume掛載到容器中。
lienhuadeMacBook-Pro:tensorflow lienhua34$ docker run -d -p 6006:6006 --name=tensorboard -v /Users/lienhua34/Programs/python/tensorflow/tensorflow/examples/tutorials/mnist:/tensorflow/mnist tensorflow/tensorflow tensorboard --logdir=/opt/tensor 50eeb7282f60c10ed52d26f34feeb3472cf36d83c546357801c45e14939adf1a lienhuadeMacBook-Pro:tensorflow lienhua34$ lienhuadeMacBook-Pro:tensorflow lienhua34$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 50eeb7282f60 tensorflow/tensorflow "tensorboard --logdir" 49 minutes ago Up 4 seconds 0.0.0.0:6006->6006/tcp, 8888/tcp tensorboard
此時,我們在瀏覽器中輸入http://localhost:6006/ ,得到下麵的效果,
因為我們還沒有運行MNIST的樣例代碼,所以TensorBoard提示沒有數據。下麵我們將進入tensorboard容器中運行MNIST的樣例代碼,
lienhuadeMacBook-Pro:tensorflow lienhua34$ docker exec -ti tensorboard /bin/bash root@50eeb7282f60:/notebooks# cd /tensorflow/mnist/ root@50eeb7282f60:/tensorflow/mnist# python fully_connected_feed.py Extracting data/train-images-idx3-ubyte.gz Extracting data/train-labels-idx1-ubyte.gz Extracting data/t10k-images-idx3-ubyte.gz Extracting data/t10k-labels-idx1-ubyte.gz Step 0: loss = 2.31 (0.010 sec) Step 100: loss = 2.13 (0.007 sec) Step 200: loss = 1.90 (0.008 sec) Step 300: loss = 1.56 (0.008 sec) Step 400: loss = 1.37 (0.007 sec) Step 500: loss = 0.99 (0.005 sec) Step 600: loss = 0.82 (0.004 sec) Step 700: loss = 0.77 (0.004 sec) Step 800: loss = 0.83 (0.004 sec) Step 900: loss = 0.54 (0.004 sec) Training Data Eval: Num examples: 55000 Num correct: 47055 Precision @ 1: 0.8555 Validation Data Eval: Num examples: 5000 Num correct: 4303 Precision @ 1: 0.8606 Test Data Eval: Num examples: 10000 Num correct: 8639 Precision @ 1: 0.8639 Step 1000: loss = 0.52 (0.010 sec) Step 1100: loss = 0.58 (0.444 sec) Step 1200: loss = 0.44 (0.005 sec) Step 1300: loss = 0.42 (0.005 sec) Step 1400: loss = 0.69 (0.005 sec) Step 1500: loss = 0.43 (0.004 sec) Step 1600: loss = 0.43 (0.006 sec) Step 1700: loss = 0.39 (0.004 sec) Step 1800: loss = 0.34 (0.004 sec) Step 1900: loss = 0.34 (0.004 sec) Training Data Eval: Num examples: 55000 Num correct: 49240 Precision @ 1: 0.8953 Validation Data Eval: Num examples: 5000 Num correct: 4506 Precision @ 1: 0.9012 Test Data Eval: Num examples: 10000 Num correct: 8987 Precision @ 1: 0.8987 root@50eeb7282f60:/tensorflow/mnist# ls -l /opt/tensor total 76 -rw-r--r-- 1 root root 77059 Oct 25 14:53 events.out.tfevents.1477407177.50eeb7282f60
通過上面的運行結果,我們看到MNIST樣例代碼正常運行,而且在/opt/tensor目錄下也生成了Tensorflow的事件文件events.out.tfevents.1477407177.50eeb7282f60。此時我們刷新一下TensorBoard的頁面,看到的效果如下,
如果想看到TensorBoard展示的豐富信息,可以使用mnist目錄下的mnist_with_summaries.py文件。
(done)