TensorBoard使用

TensorBoard使用

為了更方便TensorFlow程序的理解、調試與優化,Google發布了一套叫做TensorBoard的可視化工具,可以用TensorBoard來展現TensorFlow的圖像,繪制圖像生成的定量指標圖以及附加數據。

TensorBoard設置完成之后的樣子應該如下圖:


image

其基本原理見TensorBoard中文手冊,內有詳細的介紹。

本文參考了放羊的水瓶的博文

下面通過三個例程,來講解其使用:

例程1 矩陣相乘 tfboard1.py

import tensorflow as tf
with tf.name_scope('graph') as scope:
    matrix1 = tf.constant([[3., 3.]], name = 'matrix')   # 一行兩列
    matrix2 = tf.constant([[2.], [2.]], name = 'matrix2') # 兩行一列
    product = tf.matmul(matrix1, matrix2, name = 'product')


sess = tf.Session()

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

init = tf.global_variables_initializer()

sess.run(init)

tf.name_scope函數是作用域名,上述代碼斯即在graph作用域op下,又有三個op(分別是matrix1,matrix2,product),用tf函數內部的name參數命名,這樣會在tensorboard中顯示。

運行上述代碼后,在項目所在目錄會生成"logs1"目錄(可以自定義名字),然后在命令行運行:

tensorboard --logdir logs1

即可在本機6006端口調用TensorBoard。可以通過瀏覽器打開使用。

例程2 線性擬合(一) tfboard2.py

例程1中沒有任何訓練過程,比較簡單,下面通過這個例子來畫出它的張量流動圖。


import tensorflow as tf
import numpy as np

# 準備原始數據
with tf.name_scope('data'):
    x_data = np.random.rand(100).astype(np.float32)
    y_data = 0.3*x_data + 0.1

# 參數設置
with tf.name_scope('parameters'):
    weight = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
    bias = tf.Variable(tf.zeros([1]))

# 得到 y_prediction
with tf.name_scope('y_prediction'):
    y_prediction = weight*x_data + bias

# 計算損失率compute the loss
with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.square(y_data - y_prediction))

#
optimizer = tf.train.GradientDescentOptimizer(0.5)

with tf.name_scope('train'):
    train = optimizer.minimize(loss)

with tf.name_scope('init'):
    init = tf.global_variables_initializer()

sess = tf.Session()
writer = tf.summary.FileWriter("logs2/",sess.graph)
sess.run(init)

for step in range(101):
    sess.run(train)
    if step%10 == 0:
        print(step, 'weight', sess.run(weight), 'bias:', sess.run(bias))

例程3 線性擬合(二) tfboard3.py

對例程二代碼進行修改,嘗試tensorboard的其他功能,例如scalars,distributions,histograms,這些功能對于分析學習算法的性能有很大幫助。

import tensorflow as tf
import numpy as np

with tf.name_scope('data'):
    x_data = np.random.rand(100).astype(np.float32)
    y_data = 0.3*x_data + 0.1

with tf.name_scope('paremeters'):
    with tf.name_scope('weights'):
        weight = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
        tf.summary.histogram('weight', weight)
    with tf.name_scope('biases'):
        bias = tf.Variable(tf.zeros([1]))
        tf.summary.histogram('bias', bias)

with tf.name_scope('y_prediction'):
    y_prediction = weight*x_data + bias

with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.square(y_data - y_prediction))
    tf.summary.scalar('loss', loss)

optimizer = tf.train.GradientDescentOptimizer(0.5)

with tf.name_scope('train'):
    train = optimizer.minimize(loss)

with tf.name_scope('init'):
    init = tf.global_variables_initializer()

sess = tf.Session()
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("logs3/", sess.graph)
sess.run(init)

for step in range(101):
    sess.run(train)
    rs = sess.run(merged)
    writer.add_summary(rs, step)


本文首發于個人網頁Yao Blog,知乎專欄談技術 不能潦草,CSDN博客:手握靈珠常奮筆

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1c8b: 概述 機器學習如此復雜,訓練模型的時候,摸不清背后到底是如何運行的。自己設置的參數和關鍵變量,如果能看...
    darkie閱讀 7,367評論 5 5
  • 最近在寫tensorflow的程序,想看看訓練過程中損失函數等變化,打算用用tensorboard。 tensor...
    TsundNLP閱讀 2,052評論 0 0
  • 與 TensorFlow 的初次相遇 https://jorditorres.org/wp-content/upl...
    布客飛龍閱讀 3,962評論 2 89
  • 昨天下午突然收到一封加薪的通知郵件,卻沒有欣喜的感覺。 趕緊微信Boss連書4條撤銷加薪的理由,請求維持原有薪資水...
    柳夜刀閱讀 188評論 0 0
  • 時間總不眷念我 才在夜風中教我回想起 六月將盡 才想起還未看過任意一朵 荷花凋零 那天海上曾細雨浪涌 沙洲漫長無人...
    愁雨齋閱讀 203評論 0 0