僅供學習參考,轉載地址:http://blog.csdn.net/mzpmzk/article/details/77914941
一、TensorBoard 簡介及使用流程
TensorBoard 和 TensorFLow 程序跑在不同的進程中,TensorBoard 會自動讀取最新的 TensorFlow 日誌文件,並呈現當前 TensorFLow 程序運行的最新狀態。
- 添加記錄節點:
tf.summary.scalar/image/histogram()
等- 彙總記錄節點:
merged = tf.summary.merge_all()
- 運行彙總節點:
summary = sess.run(merged)
,得到彙總結果- 日誌書寫器實例化:
summary_writer = tf.summary.FileWriter(logdir, graph=sess.graph)
,實例化的同時傳入 graph 將當前計算圖寫入日誌- 調用日誌書寫器實例對象
summary_writer
的add_summary(summary, global_step=i)
方法將所有彙總日誌寫入文件- 調用日誌書寫器實例對象
summary_writer
的close()
方法寫入內存,否則它每隔120s寫入一次
二、TensorFlow 可視化分類
-
...create a graph...
-
# Launch the graph in a session.
-
sess = tf.Session()
-
# Create a summary writer, add the 'graph' to the event file.
-
writer = tf.summary.FileWriter(logdir, sess.graph)
-
writer.close() # 關閉時寫入內存,否則它每隔120s寫入一次
tf.summary.scalar(name, tensor, collections=None, family=None)
可視化訓練過程中隨着迭代次數準確率(val acc)、損失值(train/test loss)、學習率(learning rate)、每一層的權重和偏置的統計量(mean、std、max/min)等的變化曲線
輸入參數:
- name:此操作節點的名字,TensorBoard 中繪製的圖形的縱軸也將使用此名字
- tensor: 需要監控的變量 A real numeric Tensor containing a single value.
輸出:
- A scalar Tensor of type string. Which contains a Summary protobuf.
tf.summary.image(name, tensor, max_outputs=3, collections=None, family=None)
可視化
當前輪
訓練使用的訓練/測試圖片或者 feature maps輸入參數:
- name:此操作節點的名字,TensorBoard 中繪製的圖形的縱軸也將使用此名字
- tensor: A r A 4-D uint8 or float32 Tensor of shape
[batch_size, height, width, channels]
where channels is 1, 3, or 4- max_outputs:Max number of batch elements to generate images for
輸出:
- A scalar Tensor of type string. Which contains a Summary protobuf.
tf.summary.histogram(name, values, collections=None, family=None)
可視化張量的取值分佈
輸入參數:
- name:此操作節點的名字,TensorBoard 中繪製的圖形的縱軸也將使用此名字
- tensor: A real numeric Tensor. Any shape. Values to use to build the histogram
輸出:
- A scalar Tensor of type string. Which contains a Summary protobuf.
tf.summary.merge_all(key=tf.GraphKeys.SUMMARIES)
- Merges all summaries collected in the default graph
- 因爲程序中定義的寫日誌操作比較多,一一調用非常麻煩,所以TensoorFlow 提供了此函數來整理所有的日誌生成操作,eg:
merged = tf.summary.merge_all ()
- 此操作不會立即執行,所以,需要明確的運行這個操作(
summary = sess.run(merged)
)來得到彙總結果- 最後調用日誌書寫器實例對象的
add_summary(summary, global_step=i)
方法將所有彙總日誌寫入文件
- 如果 logdir 目錄的子目錄中包含另一次運行時的數據(多個 event),那麼 TensorBoard 會展示所有運行的數據(主要是scalar),這樣可以用於比較不同參數下模型的效果,調節模型的參數,讓其達到最好的效果!
- 上面那條線是迭代200次的loss曲線圖,下面那條是迭代400次的曲線圖,程序見最後。
三、通過命名空間美化計算圖
- 使用命名空間使可視化效果圖更有層次性,使得神經網絡的整體結構不會被過多的細節所淹沒
- 同一個命名空間下的所有節點會被縮略成一個節點,只有頂層命名空間中的節點纔會被顯示在 TensorBoard 可視化效果圖上
- 可通過
tf.name_scope()
或者tf.variable_scope()
來實現,具體見最後的程序。
四、將所有日誌寫入到文件:tf.summary.FileWriter()
tf.summary.FileWriter(logdir, graph=None, flush_secs=120, max_queue=10)
- 負責將事件日誌(graph、scalar/image/histogram、event)寫入到指定的文件中
初始化參數:
- logdir:事件寫入的目錄
- graph:如果在初始化的時候傳入
sess,graph
的話,相當於調用add_graph()
方法,用於計算圖的可視化- flush_sec:How often, in seconds, to flush the
added summaries and events
to disk.- max_queue:Maximum number of
summaries or events
pending to be written to disk before one of the ‘add’ calls block.其它常用方法:
add_event(event)
:Adds an event to the event fileadd_graph(graph, global_step=None)
:Adds a Graph to the event file,Most users pass a graph in the constructor insteadadd_summary(summary, global_step=None)
:Adds a Summary protocol buffer to the event file,一定注意要傳入 global_stepclose()
:Flushes the event file to disk and close the fileflush()
:Flushes the event file to diskadd_meta_graph(meta_graph_def,global_step=None)
add_run_metadata(run_metadata, tag, global_step=None)
五、啓動 TensorBoard 展示所有日誌圖表
- 運行你的程序,在指定目錄下(
logs
)生成event
文件 - 在
logs
所在目錄,按住shift
鍵,點擊右鍵選擇在此處打開cmd
- 在
cmd
中,輸入以下命令啓動tensorboard --logdir=logs
,注意:logs的目錄並不需要加引號, logs 中有多個event 時,會生成scalar 的對比圖,但 graph 只會展示最新的結果 - 把下面生成的網址(
http://DESKTOP-S2Q1MOS:6006 # 每個人的可能不一樣
) copy 到瀏覽器中打開即可