第一階段-入門詳細圖文講解tensorflow1.4 -(十一)TensorBoard Histogram Dashboard

TensorBoard直方圖儀表板顯示TensorFlow圖中某些張量的分佈隨時間如何變化。 它通過在不同的時間點顯示張量的許多直方圖可視化。

一,看一個基礎例子

a normally-distributed variable,一個正態分佈值。mean隨着時間的變化。
我們直接使用tf.random_normal操作。完美解決。
當然,還要使用TensorBoard,summary操作填充數據。代碼片段。

import tensorflow as tf

k = tf.placeholder(tf.float32)

# 生成一個正態分佈, 並且改變正態分佈的中值mean
mean_moving_normal = tf.random_normal(shape=[1000], mean=(5*k), stddev=1)
# 記錄這個分佈爲直方圖摘要
tf.summary.histogram("normal/moving_mean", mean_moving_normal)

# 設置一個session,並且寫出summary 的events files
sess = tf.Session()
writer = tf.summary.FileWriter("/tmp/histogram_example")

summaries = tf.summary.merge_all()

# 設置一個400循環,並將這些summary寫入硬盤
N = 400
for step in range(N):
  k_val = step/float(N)
  summ = sess.run(summaries, feed_dict={k: k_val})
  writer.add_summary(summ, global_step=step)

使用一下命令啓動一個TensorBoard實例。

tensorboard --logdir=D:\tmp\histogram_example

這裏寫圖片描述

此外,您可能會注意到,直方圖切片並不總是以步驟計數或時間間隔均勻分佈。 這是因爲TensorBoard使用池採樣來保留所有直方圖的一個子集,以節省內存。(Reservoir sampling guarantees) 池採樣保證每個樣本都有相同的被包含的可能性,但是因爲它是一個隨機算法,選擇的樣本不會在偶數步中發生。

二,Overlay Mode(覆蓋模式)

這裏寫圖片描述
Histogram Mode中選擇overly mode

在“offset”模式下,可視化旋轉45度,以便各個直方圖切片不再及時展開,而是全部繪製在相同的y軸上。
這裏寫圖片描述

現在,每個切片在圖表上都是單獨的一行,y軸顯示每個存儲桶內的項目數。 較深的線條較舊,較早的步驟較輕,較淺的線條較近。 再次,您可以將鼠標懸停在圖表上以查看一些其他信息。

這裏寫圖片描述

In general, the overlay visualization is useful if you want to directly compare the counts of different histograms.
通常情況下,覆蓋模式應用於直接對比不同的直方圖。

三,Multimodal Distributions(多模型分佈)

直方圖儀表板非常適合可視化多模式分佈。 讓我們通過連接兩個不同的正態分佈的輸出來構造一個簡單的雙峯分佈。 代碼將如下所示。

import tensorflow as tf

k = tf.placeholder(tf.float32)

# Make a normal distribution, with a shifting mean
mean_moving_normal = tf.random_normal(shape=[1000], mean=(5*k), stddev=1)
# Record that distribution into a histogram summary
tf.summary.histogram("normal/moving_mean", mean_moving_normal)

# Make a normal distribution with shrinking variance
variance_shrinking_normal = tf.random_normal(shape=[1000], mean=0, stddev=1-(k))
# Record that distribution too
tf.summary.histogram("normal/shrinking_variance", variance_shrinking_normal)

# Let's combine both of those distributions into one dataset
normal_combined = tf.concat([mean_moving_normal, variance_shrinking_normal], 0)
# We add another histogram summary to record the combined distribution
tf.summary.histogram("normal/bimodal", normal_combined)

summaries = tf.summary.merge_all()

# Setup a session and summary writer
sess = tf.Session()
writer = tf.summary.FileWriter("/tmp/histogram_example")

# Setup a loop and write the summaries to disk
N = 400
for step in range(N):
  k_val = step/float(N)
  summ = sess.run(summaries, feed_dict={k: k_val})
  writer.add_summary(summ, global_step=step)

你已經記得上面例子中的“移動均值”正態分佈。 現在我們也有一個“收縮差異”的分佈。 並排,他們看起來像這樣:
這裏寫圖片描述

當我們把它們連接起來的時候,我們得到一個清楚地顯示出不同的雙峯結構的圖表:

這裏寫圖片描述

四,Some more distributions(更多的分佈)

很有趣,讓我們生成和可視化更多的分佈,然後將它們合併成一個圖表。 以下是我們將使用的代碼:

import tensorflow as tf

k = tf.placeholder(tf.float32)

# Make a normal distribution, with a shifting mean
mean_moving_normal = tf.random_normal(shape=[1000], mean=(5*k), stddev=1)
# Record that distribution into a histogram summary
tf.summary.histogram("normal/moving_mean", mean_moving_normal)

# Make a normal distribution with shrinking variance
variance_shrinking_normal = tf.random_normal(shape=[1000], mean=0, stddev=1-(k))
# Record that distribution too
tf.summary.histogram("normal/shrinking_variance", variance_shrinking_normal)

# Let's combine both of those distributions into one dataset
normal_combined = tf.concat([mean_moving_normal, variance_shrinking_normal], 0)
# We add another histogram summary to record the combined distribution
tf.summary.histogram("normal/bimodal", normal_combined)

# Add a gamma distribution
gamma = tf.random_gamma(shape=[1000], alpha=k)
tf.summary.histogram("gamma", gamma)

# And a poisson distribution
poisson = tf.random_poisson(shape=[1000], lam=k)
tf.summary.histogram("poisson", poisson)

# And a uniform distribution
uniform = tf.random_uniform(shape=[1000], maxval=k*10)
tf.summary.histogram("uniform", uniform)

# Finally, combine everything together!
all_distributions = [mean_moving_normal, variance_shrinking_normal,
                     gamma, poisson, uniform]
all_combined = tf.concat(all_distributions, 0)
tf.summary.histogram("all_combined", all_combined)

summaries = tf.summary.merge_all()

# Setup a session and summary writer
sess = tf.Session()
writer = tf.summary.FileWriter("/tmp/histogram_example")

# Setup a loop and write the summaries to disk
N = 400
for step in range(N):
  k_val = step/float(N)
  summ = sess.run(summaries, feed_dict={k: k_val})
  writer.add_summary(summ, global_step=step)

Gamma Distribution
這裏寫圖片描述

Uniform Distribution
這裏寫圖片描述

Poisson Distribution

這裏寫圖片描述

泊松分佈是在整數上定義的。 所以,所有生成的值都是完美的整數。 直方圖壓縮將數據移動到浮點數據庫,導致可視化文件在整數值上顯示出很小的顛簸,而不是完美的尖峯。

All Together Now

最後,我們可以將所有的數據連接成一個有趣的曲線。
這裏寫圖片描述

blog結束。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章