tensorboard merge报错_什么是TensorBoard?
前言
只有光頭才能變強。
文本已收錄至我的GitHub倉庫,歡迎Star:https://github.com/ZhongFuCheng3y/3y
回顧前面:
從零開始學TensorFlow【01-搭建環境、HelloWorld篇】
什么是TensorFlow?
TensorFlow讀寫數據
如何理解axis?
這篇文章主要講講TensorBoard的基本使用以及name_scope和variable_scope的區別
一、入門TensorBoard
首先來講講TensorBoard是什么吧,我當時是在官方文檔里學習的,官網也放出了介紹TensorBoard的視頻。我在b站搜了一把,發現也有,大家可以先去看看視頻了解一下(其實已經說得很好了):
https://www.bilibili.com/video/av35203293?from=search&seid=6605552834229124959
為了更方便 TensorFlow 程序的理解、調試與優化,于是就有了TensorBoard 這樣的的可視化工具
因為我們編寫出來的TensorFlow程序,建好一個神經網絡,其實我們也不知道神經網絡里頭具體細節到底做了什么,要人工調試十分困難(就好比你無法想象出遞歸的所有步驟一樣)。有了TensorBoard,可以將TensorFlow程序的執行步驟都顯示出來,非常直觀。并且,我們可以對訓練的參數(比如loss值)進行統計,用圖的方式來查看變化的趨勢。
1.1 name_scope和variable_scope
在視頻中其實也有提到,我們想要TensorBoard的圖能夠更好地展示(更加有條理),那一般我們需要對其用name_scope取名。
那除了name_scope,還有一個叫做variable_scope。那他們有什么區別呢?顧名思義,name_scope是一個名稱作用域,variable_scope是變量作用域。
在前面文章中,創建變量有兩種方式,一種是用tf.get_variable()來創建,一種是用tf.Variable()來創建。這兩種創建方式也是有區別的。
在
tf.name_scope下時,tf.get_variable()創建的變量名不受name_scope的影響,而且在未指定共享變量時,如果重名就會報錯。tf.Variable()會自動檢測有沒有變量重名,如果有則會自行處理(自動創建一個)
比如下面的代碼:
with?tf.name_scope('name_sp1')?as?scp1:
????with?tf.variable_scope('var_scp2')?as?scp2:
????????with?tf.name_scope('name_scp3')?as?scp3:
????????????a?=?tf.Variable('a')
????????????b?=?tf.get_variable('b')
等同于:
with?tf.name_scope('name_sp1')?as?scp1:
????with?tf.name_scope('name_sp2')?as?scp2:
????????with?tf.name_scope('name_scp3')?as?scp3:
????????????a?=?tf.Variable('a')
with?tf.variable_scope('var_scp2')?as?scp2:
????????b?=?tf.get_variable('b')
這里體現的是如果用get_variable的方式來創建對象,是不受name_scope所影響的。
要注意的是,下面的代碼會報錯。因為在scp作用域下壓根就沒有a這個變量,同時又設置成reuse=True。這里因為的是找不到共享變量而出錯!
with?tf.variable_scope('scp',?reuse=True)?as?scp:
????a?=?tf.get_varialbe('a')?#報錯
同樣地,下面的代碼也會報錯,因為明明已經有共享變量了,但設置成reuse=false。所以就會報錯。
with?tf.variable_scope('scp',?reuse=False)?as?scp:????
?????a?=?tf.get_varialbe('a')
????a?=?tf.get_varialbe('a')?#報錯
最后,我們再來看這個例子,應該就可以看懂了。
with?tf.variable_scope('variable_scope_y')?as?scope:
????var1?=?tf.get_variable(name='var1',?shape=[1],?dtype=tf.float32)
????scope.reuse_variables()??#?設置共享變量
????var1_reuse?=?tf.get_variable(name='var1')
????var2?=?tf.Variable(initial_value=[2.],?name='var2',?dtype=tf.float32)
????var2_reuse?=?tf.Variable(initial_value=[2.],?name='var2',?dtype=tf.float32)
with?tf.Session()?as?sess:
????sess.run(tf.global_variables_initializer())
????print(var1.name,?sess.run(var1))
????print(var1_reuse.name,?sess.run(var1_reuse))
????print(var2.name,?sess.run(var2))
????print(var2_reuse.name,?sess.run(var2_reuse))
# 輸出結果:
#?variable_scope_y/var1:0?[-1.59682846]
#?variable_scope_y/var1:0?[-1.59682846]???可以看到變量var1_reuse重復使用了var1
#?variable_scope_y/var2:0?[?2.]
#?variable_scope_y/var2_1:0?[?2.]
參考資料:
https://www.zhihu.com/question/54513728
1.2 TensorBoard例子
下面我們來看一個TensorBoard簡單的入門例子,感受一下:
def?learn_tensor_board_2():
????#?prepare?the?original?data
????with?tf.name_scope('data'):
????????x_data?=?np.random.rand(100).astype(np.float32)
????????y_data?=?0.3?*?x_data?+?0.1
????##creat?parameters
????with?tf.name_scope('parameters'):
????????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)
????##get?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))
????????tf.summary.scalar('loss',?loss)
????##creat?optimizer
????optimizer?=?tf.train.GradientDescentOptimizer(0.5)
????#?creat?train?,minimize?the?loss
????with?tf.name_scope('train'):
????????train?=?optimizer.minimize(loss)
????#?creat?init
????with?tf.name_scope('init'):
????????init?=?tf.global_variables_initializer()
????##creat?a?Session
????sess?=?tf.Session()
????#?merged
????merged?=?tf.summary.merge_all()
????##initialize
????writer?=?tf.summary.FileWriter("/Users/zhongfucheng/tensorboard/loss-2",?sess.graph)
????sess.run(init)
????##?Loop
????for?step?in?range(201):
????????sess.run(train)
????????rs?=?sess.run(merged)
????????writer.add_summary(rs,?step)
if?__name__?==?'__main__':
????learn_tensor_board_2()
????# 啟動完了之后,要在命令行上運行tensor_board的命令,指定其目錄,最后我們就可以通過6006的默認端口訪問我們的圖。
(例子來源網絡,我改動了一下,出處我忘了,侵刪~)
接下來,我們啟動一下TensorBoard,看看圖是怎么樣的,啟動命令如下:
tensorboard?--logdir=/Users/zhongfucheng/tensorboard/loss-2
啟動成功的圖:
通過6006端口我們去訪問一下,首先我們可以檢測到loss值的變更:
我們也可以查看TensorFlow程序大概的執行步驟:
參數w和b的直方圖:
總之,TensorBoard可以方便地查看我們參數的變化,以便更好理解我們寫的代碼。
參考資料:
https://www.cnblogs.com/tengge/p/6376073.html
https://www.cnblogs.com/fydeblog/p/7429344.html
最后
樂于輸出干貨的Java技術公眾號:Java3y。公眾號內有200多篇原創技術文章、海量視頻資源、精美腦圖,不妨來關注一下!
總結
以上是生活随笔為你收集整理的tensorboard merge报错_什么是TensorBoard?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 男性不育到哪里
- 下一篇: 我爱你就像飞蛾扑火是哪首歌啊?