當前位置: 首頁>>技術教程>>正文


TensorBoard:Tensorflow可視化學習

使用TensorFlow進行計算 – 如訓練大量的深度神經網絡 – 可能會很複雜且令人困惑。為了便於理解,調試和優化TensorFlow程序,我們引入了一套名為TensorBoard的可視化工具。您可以使用TensorBoard來顯示您的TensorFlow圖,繪製關於計算圖執行的量化指標,並顯示其他數據,如通過它的圖像。當完全配置好TensorBoard時,看起來像這樣:

MNIST TensorBoard

本教程旨在讓您開始使用簡單的TensorBoard。也有不少其他Tensorboard的資源可用!如TensorBoard的GitHub就有關於TensorBoard使用情況的更多信息,包括提示&技巧和調試信息。

序列化數據

TensorBoard通過讀取TensorFlow事件文件進行操作,事件文件包含運行TensorFlow時生成的摘要數據。以下是TensorBoard中匯總數據的一般生命周期:

首先,創建您想從中收集摘要數據的TensorFlow圖,並決定要對哪個節點進行Summary操作注釋。

例如,假設您正在訓練用於識別MNIST數字的卷積神經網絡,您想記錄學習率隨時間的變化,以及目標函數如何變化。通過附加tf.summary.scalar操作到輸出學習率和損失的節點,收集這些信息。然後,給每個scalar_summary一個有意義的tag, 比如'learning rate''loss
function'

也許你也想看到一個特定層的激活分布,或者梯度、權重的分布。通過附加tf.summary.histogram操作到梯度輸出和保持你的權重的變量,來收集這些數據。

有關所有可用摘要操作的詳細信息,請查看文檔Summary操作

在運行之前,TensorFlow中的操作不會執行任何操作。我們剛剛創建的摘要節點是計算圖的外圍設備:您當前正在運行的所有操作都不依賴於它們。所以,為了生成摘要,我們需要運行所有這些匯總節點。一個一個手動管理它們很麻煩,所以使用tf.summary.merge_all將它們組合成一個單獨的操作來生成所有的匯總數據。

然後,您可以運行合並後的摘要操作,這將生成一個序列化Summaryprotobuf對象,它包含給定步驟中的所有摘要數據。最後,要將這個摘要數據寫入磁盤,請將summary protobuf傳遞給一個tf.summary.FileWriter

FileWriter在它的構造函數中需要一個logdir – 這個logdir非常重要,它是所有事件將被寫出的目錄。另外,在FileWriter構造函數中可以選擇一個Graph。如果它收到一個Graph對象,然後TensorBoard將可視化您的圖形與張量形狀信息。這會讓你更好地理解圖中流動的東西:見張量形狀信息

現在你已經修改了你的Graph,並有一個FileWriter,你準備好開始運行你的網絡!如果你願意,你可以每一個訓練步都運行合並的摘要操作,並記錄大量的訓練數據。當然,通常是多步合並輸出。

下麵的代碼示例是對簡單的MNIST教程的修改,其中我們添加了一些Summary操作,並且每十步運行一次。如果你運行這個,然後啟動tensorboard --logdir=/tmp/tensorflow/mnist,您將能夠查看統計數據,例如在訓練過程中權重或準確性如何變化。下麵的代碼是摘錄;完整的來源是這裏

def variable_summaries(var):
  """Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""
  with tf.name_scope('summaries'):
    mean = tf.reduce_mean(var)
    tf.summary.scalar('mean', mean)
    with tf.name_scope('stddev'):
      stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
    tf.summary.scalar('stddev', stddev)
    tf.summary.scalar('max', tf.reduce_max(var))
    tf.summary.scalar('min', tf.reduce_min(var))
    tf.summary.histogram('histogram', var)

def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu):
  """Reusable code for making a simple neural net layer.

  It does a matrix multiply, bias add, and then uses relu to nonlinearize.
  It also sets up name scoping so that the resultant graph is easy to read,
  and adds a number of summary ops.
  """
  # Adding a name scope ensures logical grouping of the layers in the graph.
  with tf.name_scope(layer_name):
    # This Variable will hold the state of the weights for the layer
    with tf.name_scope('weights'):
      weights = weight_variable([input_dim, output_dim])
      variable_summaries(weights)
    with tf.name_scope('biases'):
      biases = bias_variable([output_dim])
      variable_summaries(biases)
    with tf.name_scope('Wx_plus_b'):
      preactivate = tf.matmul(input_tensor, weights) + biases
      tf.summary.histogram('pre_activations', preactivate)
    activations = act(preactivate, name='activation')
    tf.summary.histogram('activations', activations)
    return activations

hidden1 = nn_layer(x, 784, 500, 'layer1')

with tf.name_scope('dropout'):
  keep_prob = tf.placeholder(tf.float32)
  tf.summary.scalar('dropout_keep_probability', keep_prob)
  dropped = tf.nn.dropout(hidden1, keep_prob)

# Do not apply softmax activation yet, see below.
y = nn_layer(dropped, 500, 10, 'layer2', act=tf.identity)

with tf.name_scope('cross_entropy'):
  # The raw formulation of cross-entropy,
  #
  # tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.softmax(y)),
  #                               reduction_indices=[1]))
  #
  # can be numerically unstable.
  #
  # So here we use tf.nn.softmax_cross_entropy_with_logits on the
  # raw outputs of the nn_layer above, and then average across
  # the batch.
  diff = tf.nn.softmax_cross_entropy_with_logits(targets=y_, logits=y)
  with tf.name_scope('total'):
    cross_entropy = tf.reduce_mean(diff)
tf.summary.scalar('cross_entropy', cross_entropy)

with tf.name_scope('train'):
  train_step = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(
      cross_entropy)

with tf.name_scope('accuracy'):
  with tf.name_scope('correct_prediction'):
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
  with tf.name_scope('accuracy'):
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.summary.scalar('accuracy', accuracy)

# Merge all the summaries and write them out to /tmp/mnist_logs (by default)
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/train',
                                      sess.graph)
test_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/test')
tf.global_variables_initializer().run()

初始化FileWriters之後,當我們訓練和測試模型時,必須添加summary到FileWriters

# Train the model, and also write summaries.
# Every 10th step, measure test-set accuracy, and write test summaries
# All other steps, run train_step on training data, & add training summaries

def feed_dict(train):
  """Make a TensorFlow feed_dict: maps data onto Tensor placeholders."""
  if train or FLAGS.fake_data:
    xs, ys = mnist.train.next_batch(100, fake_data=FLAGS.fake_data)
    k = FLAGS.dropout
  else:
    xs, ys = mnist.test.images, mnist.test.labels
    k = 1.0
  return {x: xs, y_: ys, keep_prob: k}

for i in range(FLAGS.max_steps):
  if i % 10 == 0:  # Record summaries and test-set accuracy
    summary, acc = sess.run([merged, accuracy], feed_dict=feed_dict(False))
    test_writer.add_summary(summary, i)
    print('Accuracy at step %s: %s' % (i, acc))
  else:  # Record train set summaries, and train
    summary, _ = sess.run([merged, train_step], feed_dict=feed_dict(True))
    train_writer.add_summary(summary, i)

您現在已經開始使用TensorBoard將這些數據可視化了。

啟動TensorBoard

要運行TensorBoard,請使用以下命令(或者python -m
tensorboard.main --logdir=path/to/log-directory
)

tensorboard --logdir=path/to/log-directory

其中logdir指向FileWriter序列化數據所在的目錄。如果logdir目錄包含來自單獨運行的序列化數據的子目錄,那麽TensorBoard將可視化來自所有這些運行的數據。一旦TensorBoard正在運行,通過網頁瀏覽器瀏覽地址localhost:6006即可查看TensorBoard。

特別需要注意的是:運行tensorboard時,可能遇到報錯ImportError: cannot import name 'encodings', 詳細錯誤信息如下:


Traceback (most recent call last):
  File "/usr/local/bin/tensorboard", line 7, in 
    from tensorboard.main import main
  File "/usr/local/lib/python3.6/site-packages/tensorboard/main.py", line 30, in 
    from tensorboard import default
  File "/usr/local/lib/python3.6/site-packages/tensorboard/default.py", line 35, in 
    from tensorboard.plugins.audio import audio_plugin
  File "/usr/local/lib/python3.6/site-packages/tensorboard/plugins/audio/audio_plugin.py", line 27, in 
    from tensorboard import plugin_util
  File "/usr/local/lib/python3.6/site-packages/tensorboard/plugin_util.py", line 21, in 
    import bleach
  File "/usr/local/lib/python3.6/site-packages/bleach/__init__.py", line 14, in 
    from html5lib.sanitizer import HTMLSanitizer
  File "/usr/local/lib/python3.6/site-packages/html5lib/sanitizer.py", line 7, in 
    from .tokenizer import HTMLTokenizer
  File "/usr/local/lib/python3.6/site-packages/html5lib/tokenizer.py", line 17, in 
    from .inputstream import HTMLInputStream
  File "/usr/local/lib/python3.6/site-packages/html5lib/inputstream.py", line 9, in 
    from .constants import encodings, ReparseException
ImportError: cannot import name 'encodings'

解決這個問題的一種方法是:sudo pip install html5lib==1.0b8, 如果是python3, 將pip替換為pip3即可。

在使用TensorBoard時,您會看到右上角的導航標簽。每個選項卡代表一組可以可視化的序列化數據。

關於使用graph選項卡可視化圖的更深入信息,請參閱TensorBoard:圖形可視化

有關TensorBoard的更多其他資料,請參閱TensorBoard的GitHub

參考資料

本文由《純淨天空》出品。文章地址: https://vimsky.com/zh-tw/article/3642.html,未經允許,請勿轉載。