用 TensorBoard 來展現 TensorFlow 圖,繪製圖像生成的定量指標圖以及顯示附加數據(如其中傳遞的圖像)。
tensorflow.summary
scalar一般用於數值的顯示
如
tf.summary.scalar(softmax_cross_entropy)
tf.summary.scalar(loss)
histogram一般用於向量的分佈顯示
tf.summary.histogram('histogram', var)
多個histogram合併到一起
variance_shrinking_normal = tf.random_normal(shape=[1000], mean=0, stddev=1-(k)) 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)[TensorBoard Histogram Dashboard]
tensorflow封裝好的rnn/lstm等層weight/bias的summary
通過層參數.weight或者.variables查看,兩者區別暫時不清楚。
lstm_cell_fw = rnn.LSTMBlockFusedCell(params['lstm_size'])
output_fw, _ = lstm_cell_fw(embeddings_t, dtype=tf.float32, sequence_length=nwords)
lstm_cell_fw_kernel, lstm_cell_fw_bias = lstm_cell_fw.weights
tf.summary.histogram('lstm_cell_fw_kernel', lstm_cell_fw_kernel)
tf.summary.histogram('lstm_cell_fw_bias', lstm_cell_fw_bias)
[Tensorboard - visualize weights of LSTM]
[How to get summary information on tensorflow RNN]
tensorflow獲取tf.layers.dense中的參數權重weight
l=tf.layers.dense(input_tf_xxx,300,name='layer_name')
with tf.variable_scope("layer_name", reuse=True):
weights = tf.get_variable("kernel")
[How to get weights in tf.layers.dense?]
啓動 TensorBoard
要運行 TensorBoard,請使用以下命令
python3 -m tensorboard.main --logdir=path/to/log-directory
tensorboard --logdir=path/to/log-directory
Note: 1
logdir
指向 FileWriter
將數據序列化的目錄(如果使用的是estimator則是MODELDIR:estimator = tf.estimator.Estimator(model_fn, MODELDIR, cfg, params))。如果此 logdir
目錄下有子目錄,而子目錄包含基於各個運行的序列化數據,則 TensorBoard 會將所有這些運行涉及的數據都可視化。
2 指定端口 --port=8008。
3 命令出錯:ImportError: No module named _multiarray_umath...F...Check failed: PyBfloat16_Type.tp_base != nullptr ...[1] 1889 abort python -m tensorboard.main --logdir=results/model,可能是因爲python版本不對試試:python3 -m tensorboard.main --logdir=results/model
TensorBoard 運行後,請在您的網絡瀏覽器中轉到 localhost:6006
以查看 TensorBoard。
tensorboard圖解析
Estimator中使用TensorBoard
所有預創建的 Estimator 都會自動將大量信息記錄到 TensorBoard 上。不過,對於自定義 Estimator,TensorBoard 只提供一個默認日誌(損失圖)以及您明確告知 TensorBoard 要記錄的信息(如將所有的metrics寫入summary繪圖
metrics = {'accuracy': accuracy}
for metric_name, op in metrics.items():
tf.summary.scalar(metric_name, op[1]))。
Note: estimator不需要顯式地將logdir傳給tf.summary.FileWriter,它自動默認爲MODELDIR。其中MODELDIR爲train的,而MODELDIR/eval爲eval的信息。
圖說明如下:
- global_step/sec:一個性能指示器,展示了隨着模型訓練,每秒處理多少batches(gradient updates)
- loss:上報的loss。當你的loss呈下降的趨勢,說明你的神經網絡訓練是有效果的。
- accuracy: 通過以下兩行所記錄的accuracy
- eval_metric_ops={‘my_accuracy’: accuracy}),evaluation期
- tf.summary.scalar(‘accuracy’, accuracy[1]),training期
對於傳輸一個global_step到你的optimizer的minimize方法中很重要,如果沒有它,該模型不能爲這些圖記錄x座標。在訓練期,summaries(橙色線)會隨着batches的處理被週期性記錄,這就是爲什麼它會變成一個x軸。相反的,對於evaluate的每次調要,evaluation過程只會在圖中有一個點。該點包含了整個evaluation調用的平均。在該圖中沒有width,因爲它會被在特定training step(單個checkpoint)的某個模型態下整個進行評估。你可以使用左側的控制面板來選擇性地disable/enable。
在my_accuracy和loss圖中,要注意以下事項:
- 橙色線表示training,其中淺色線代表Smoothing=0時的值
- 藍色點表示evaluation
[官網:在 TensorBoard 中查看自定義 Estimator 的訓練結果]
DISTRIBUTIONS
DISTRIBUTIONS
主要用來展示網絡中各參數隨訓練步數的增加的變化情況,可以說是 多分位數折線圖 的堆疊。下面我就下面這張圖來解釋下。
權重分佈
這張圖表示的是第二個卷積層的權重變化。橫軸表示訓練步數,縱軸表示權重值。而從上到下的折現分別表示權重分佈的不同分位數:[maximum, 93%, 84%, 69%, 50%, 31%, 16%, 7%, minimum]
。對應於我的代碼,部分如下:
with tf.name_scope(name):
W = tf.Variable(tf.truncated_normal(
[k, k, channels_in, channels_out], stddev=0.1), name='W')
b = tf.Variable(tf.constant(0.1, shape=[channels_out]), name='b')
conv = tf.nn.conv2d(inpt, W, strides=[1, s, s, 1], padding='SAME')
act = tf.nn.relu(conv)
tf.summary.histogram('weights', W)
tf.summary.histogram('biases', b)
tf.summary.histogram('activations', act)
HISTOGRAMS
HISTOGRAMS
和 DISTRIBUTIONS
是對同一數據不同方式的展現。與 DISTRIBUTIONS
不同的是,HISTOGRAMS
可以說是 頻數分佈直方圖 的堆疊。
權重分佈
橫軸表示權重值,縱軸表示訓練步數。顏色越深表示時間越早,越淺表示時間越晚(越接近訓練結束)。除此之外,HISTOGRAMS
還有個 Histogram mode
,有兩個選項:OVERLAY
和 OFFSET
。選擇 OVERLAY
時橫軸爲權重值,縱軸爲頻數,每一條折線爲訓練步數。顏色深淺與上面同理。默認爲 OFFSET
模式。
Projector
使用T-SNE方式顯示高維向量,這是一個動態的過程,其中隨着iteration的增加,會發現結果向量會逐漸分開。相同類別的會聚攏在一起,我們可以選擇不同顏色作爲區分,發現不同顏色的預測結果的區分度逐漸拉大。
使用tensorflow.summary時報錯
1 運行
tf.summary.histogram('histogram', var)
Nan in summary histogram for
原因可能有好多種
lz的原因是:在神經網絡前向傳播時使用了傳入參數的轉換就出這個錯誤了:beta = tf.Variable(beta0, name='beta')
其它原因有:
First, I was working with float16 type data which has been documented to cause problems in tensorflow (can't remember where I found this ...I think some stackoverflow post from 2016)
Second (and probably more importantly) my training data was not properly normalized and included a whole batch worth of images without any data. If you're getting Nan's this means training has diverged, likely indicating a problem with training data.
還有可能是學習沒有收斂而是發散了。
2 運行
summary_op = tf.summary.merge_all()
...
summary_str = sess.run([summary_op], feed_dict=feed_dict)
TypeError: Fetch argument None has invalid type <class 'NoneType'>
1)re-assigning the train_step
variable to the second element of the result of sess.run()
(which happens to be None
).
numpy_state, train_step = sess.run([final_state, train_step], feed_dict={})
numpy_state, _ = sess.run([final_state, train_step], feed_dict={...})
2)主要原因是run中某個op爲none,這裏summary_op爲none ,原因是沒有任何summaries 被收集merge_all就會返回none.
3 運行
summary_str = sess.run([summary_op], feed_dict=feed_dict)
TypeError: Parameter to MergeFrom() must be instance of same class: expected Summary got list. for field Event.summary
去掉[]就可以了,居然。。。
summary_str = sess.run(summary_op, feed_dict=feed_dict)
還有可能是沒有run summary_op,而直接運行了writer.add_summary(summary_str, step)
from: -柚子皮-
ref: [TensorBoard:可視化學習]
官方學習視頻[TensorBoard實踐介紹(2017年TensorFlow開發大會)]視頻文檔[詳解 TensorBoard-如何調參]
[理解 TensorBoard][理解 TensorBoard]