Hike News
Hike News

深度學習-tensorflow基礎-tensorboard可視化

Introduction

tensorboard是可視化的web介面tensorflow後台監控程序

  • 將程序的garph結構 顯示在web界面
    • 將程序的graph結構,經過序列化文件的動作,生成events事件文件
    • tensorboard會將events讀取出來顯示到web界面

將程序序列化成events文件

tf.summary.FileWriter(logdir,graph=)

  • summary模塊用於tensorboard後台管理使用
  • logdir:寫入events文件到何者路徑(不包含檔名),最好使用絕對路徑,以提供給tensorflow使用
  • graph:把指定的graph寫入events文件
  • 返回一個filewriter
  • 注意:一般於Session中去執行此函數

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import tensorflow as tf

const = tf.constant([1,2,3,4,5])
variable = tf.Variable(tf.random_normal([2,3], mean=0.0, stddev=1.0))

variable_initialize = tf.global_variables_initializer()


with tf.Session() as sess:
sess.run(variable_initialize)

# 把程序的圖結構寫入事件文件
filewriter = tf.summary.FileWriter("./summary",graph=sess.graph)

print(sess.run([const,variable]))
  • 執行完後會在參數填入的文件夾中生成events文件
  • 其名稱格式如下:
    • events.out.tfevents.{timestamp}.{hotstname}

使用tensorboard開啟events文件

1
tensorboard  --logdir="events存放的文件夾"
  • 路徑記得加上雙引號
  • 使用瀏覽器打開localhost:6006
    tensor board
  • 再修改程序後,在使用FileWriter保存一遍時會有新的事件文件,於tensorboard查看為最新的文件

選項(Inactive)

在Inactive下拉可找到多個tensorboard的選單

  • SCALARS:顯示0維度之值(單純數值)用,例如準確率、損失
  • GRAPHS:顯示程序的結構
  • HISTOGRAMS:顯示高維度之值,例如權重($w$)、偏置(bias)等
  • 我們在GRAPH中並沒有看到先前定義的名為const的張量,原因是要是我們沒有使用到該張量,則不會在GRAPH中顯示

name

任何的op都可以在參數中指定name參數,會在tensorboard的GRAPH中顯示名字

  • 亦可以讓相同op名字進行區分
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    import tensorflow as tf

    a = tf.constant(1.0,name="a_const")
    b = tf.constant(2.0,name="b_const")
    result = tf.add(a,b,name="result")
    variable = tf.Variable(tf.random_normal([2,3], mean=0.0, stddev=1.0),name="my_variable")

    variable_initialize = tf.global_variables_initializer()


    with tf.Session() as sess:
    sess.run(variable_initialize)

    filewriter = tf.summary.FileWriter("./summary",graph=sess.graph)

    print(sess.run([result,variable]))
    name

圖中符號的意義

label
來源https://www.tensorflow.org/programmers_guide/graph_viz?hl=zh-cn

增加變量顯示(後期補充)

目的:觀察模型的參數,損失值等變量值的變化

  • SCALARS及HISTOGRAMS的選項是觀察變量的變化