Tensorflow:Tensorboard使用
目錄
- 目錄
- 前言
- 可視化第一步
- 實(shí)現(xiàn)graphs
- 后記
前言
TensorFlow是以流動(dòng)的張量為名的神經(jīng)網(wǎng)絡(luò)開發(fā)庫,所以Google為了讓人們更直觀的了解流動(dòng)的張量的含義,他們做了個(gè)TensorBoard讓我們直觀的看到我們寫的框架是怎么個(gè)流動(dòng)法的(純屬YY)。好了玩笑話不說,Google是怎么定義TensorBoard的呢?
The computations you'll use TensorFlow for - like training a massive deep neural network - can be complex and confusing. To make it easier to understand, debug, and optimize TensorFlow programs, we've included a suite of visualization tools called TensorBoard. You can use TensorBoard to visualize your TensorFlow graph,plot quantitative metrics about the execution of your graph,and show additional data like images that pass through it.大意其實(shí)就是我剛剛說的,我們使用TensorFlow寫的神經(jīng)網(wǎng)絡(luò)直接看上去會(huì)顯得十分的復(fù)雜和混亂,為了更加直觀的去調(diào)試、優(yōu)化我們的神經(jīng)網(wǎng)絡(luò),他們開發(fā)了TensorBoard–一套可視化工具,深度學(xué)習(xí)的可視化對于理解它究竟學(xué)到了什么有很大的幫助。
Tips:TensorBoard官方支持的瀏覽器是Chrome和Firefox,所以最好使用這兩個(gè)瀏覽器去開TensorBoard吧。
可視化第一步
效果圖
我們可以對其進(jìn)行展開,比如我們展開一個(gè)first_layer
可以看出來一個(gè)layer里面是多么復(fù)雜的運(yùn)算過程,其實(shí)我們代碼里也就幾行代碼,TensorFlow幫我們完成了很多事情。
看了效果之后有沒有非常想實(shí)現(xiàn)它?那么接下來我們就來學(xué)習(xí)下怎么去用TensorBoard去畫出這么一幅圖~
實(shí)現(xiàn)graphs
我們這里用的代碼是之前講TensorFlow入門里最后的擬合曲線的代碼,代碼如下:
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt def add_layer(layoutname,inputs,in_size,out_size,activatuib_funaction=None):Weights=tf.Variable(tf.random_normal([in_size,out_size]))biases=tf.Variable(tf.zeros([1,out_size]))+0.1Wx_plus_b=tf.matmul(inputs,Weights)+biasesif activatuib_funaction is None:outputs=Wx_plus_belse :outputs=activatuib_funaction(Wx_plus_b)return outputsx_data=np.linspace(-1,1,300)[:,np.newaxis] noise=np.random.normal(0,0.05,x_data.shape) y_data=np.square(x_data)-0.5+noisexs=tf.placeholder(tf.float32,[None,1]) ys=tf.placeholder(tf.float32,[None,1])l1=add_layer('first_layer',xs,1,10,activatuib_funaction=tf.nn.relu) prediction =add_layer('secend_layer',l1,10,1,activatuib_funaction=None)loss=tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1]))train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss)init=tf.global_variables_initializer()with tf.Session() as sess:fig=plt.figure()ax=fig.add_subplot(1,1,1)ax.scatter(x_data,y_data)plt.show(block=False)sess.run(init)for train in range(1000):sess.run(train_step,feed_dict={xs:x_data,ys:y_data})if train%50==0:try:ax.lines.remove(lines[0])except Exception:passprint train,sess.run(loss,feed_dict={xs:x_data,ys:y_data})prediction_value=sess.run(prediction,feed_dict={xs:x_data})lines=ax.plot(x_data, prediction_value,'r-',lw=5)plt.pause(0.1)我們需要對代碼進(jìn)行寫改動(dòng),讓其生成Graphs。
從最開始的縮略圖,我們可以看到我們有input、兩個(gè)layer、一個(gè)loss、一個(gè)train,那我們第一步先來構(gòu)建這幾個(gè)東西。
我們想要構(gòu)建這些大的框架,只需要我們在想要顯示的地方加上這句話with tf.name_scope(‘顯示的名字’):然后縮進(jìn)想要放在里面的代碼。如input
我們想要顯示的效果如下圖
我們需要用with tf.name_scope(‘input’):把兩個(gè)輸入值縮進(jìn)進(jìn)去:
with tf.name_scope('inputs'):xs=tf.placeholder(tf.float32,[None,1],name="x_input")ys=tf.placeholder(tf.float32,[None,1],name="y_input")x_input、y_input這兩個(gè)顯示值我們只需要在聲明的時(shí)候在最后加一個(gè)name=’顯示名’就可以顯示出上面的效果了。
然后兩個(gè)layer也是類似,由于我們定義了一個(gè)方法去添加層,可是如果我們不給他名字,生成graphs的時(shí)候layer就是layer、layer_1、layer_2這樣子,有點(diǎn)不好看,所以我們傳入一個(gè)layername。
def add_layer(layoutname,inputs,in_size,out_size,activatuib_funaction=None,):with tf.name_scope(layoutname):with tf.name_scope('weights'):Weights=tf.Variable(tf.random_normal([in_size,out_size]),name='W')with tf.name_scope('biases'):biases=tf.Variable(tf.zeros([1,out_size])+0.1,name='b')with tf.name_scope('Wx_plus_b'):Wx_plus_b=tf.add(tf.matmul(inputs,Weights),biases)if activatuib_funaction is None:outputs=Wx_plus_belse :outputs=activatuib_funaction(Wx_plus_b)return outputslayer圖層里面還有weights和biases、Wx_plus_b等,所以他們也是小圖層,所以我們對他們也要單獨(dú)定義,另外給weights,biases給個(gè)name值。 激勵(lì)函數(shù)這些會(huì)自動(dòng)生成,我們不需要去管。
loss和train也是類似,寫完tf.name_scope(”):之后,我們還需要寫文件,寫文件的代碼TensorFlow也幫我們封裝好了,我們只需要調(diào)用writer=tf.summary.FileWriter(“文件保存路徑如:logs/”,sess.graph),完整代碼:
import tensorflow as tf import numpy as np def add_layer(layoutname,inputs,in_size,out_size,activatuib_funaction=None,):with tf.name_scope(layoutname):with tf.name_scope('weights'):Weights=tf.Variable(tf.random_normal([in_size,out_size]),name='W')with tf.name_scope('biases'):biases=tf.Variable(tf.zeros([1,out_size])+0.1,name='b')with tf.name_scope('Wx_plus_b'):Wx_plus_b=tf.add(tf.matmul(inputs,Weights),biases)if activatuib_funaction is None:outputs=Wx_plus_belse :outputs=activatuib_funaction(Wx_plus_b)return outputs x_data=np.linspace(-1,1,300)[:,np.newaxis] noise=np.random.normal(0,0.09,x_data.shape) y_data=np.square(x_data)-0.05+noise with tf.name_scope('inputs'):xs=tf.placeholder(tf.float32,[None,1],name="x_in")ys=tf.placeholder(tf.float32,[None,1],name="y_in") l1=add_layer("first_layer",xs,1,10,activatuib_funaction=tf.nn.relu) prediction =add_layer('second_layout',l1,10,1,activatuib_funaction=None) with tf.name_scope('loss'):loss=tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1])) with tf.name_scope('train'):train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss)init=tf.global_variables_initializer()with tf.Session() as sess:writer=tf.summary.FileWriter("logs/",sess.graph)sess.run(init)由于Graphs展示的是整個(gè)神經(jīng)網(wǎng)絡(luò)的框架,所以我們可以刪去測試數(shù)據(jù)。
到這里代碼已經(jīng)寫好了,運(yùn)行代碼,生成文件,我們會(huì)發(fā)現(xiàn)項(xiàng)目的logs目錄下會(huì)多一個(gè)文件
生成文件之后,首先是進(jìn)入cmd,輸入下面的指令來啟動(dòng)TensorBoard
python tensorflow/tensorboard/tensorboard.py –logdir=path/to/log-directory
這里的參數(shù) logdir 指向 SummaryWriter 序列化數(shù)據(jù)的存儲(chǔ)路徑。如果logdir目錄的子目錄中包含另一次運(yùn)行時(shí)的數(shù)據(jù),那么 TensorBoard 會(huì)展示所有運(yùn)行的數(shù)據(jù)。一旦 TensorBoard 開始運(yùn)行,你可以通過在瀏覽器中輸入 localhost:6006 來查看 TensorBoard。
如果你已經(jīng)通過pip安裝了 TensorBoard,你可以通過執(zhí)行更為簡單地命令來訪問 TensorBoard
tensorboard –logdir=/path/to/log-directory
進(jìn)入 TensorBoard 的界面時(shí),你會(huì)在右上角看到導(dǎo)航選項(xiàng)卡,每一個(gè)選項(xiàng)卡將展現(xiàn)一組可視化的序列化數(shù)據(jù)集 。對于你查看的每一個(gè)選項(xiàng)卡,如果 TensorBoard 中沒有數(shù)據(jù)與這個(gè)選項(xiàng)卡相關(guān)的話,則會(huì)顯示一條提示信息指示你如何序列化相關(guān)數(shù)據(jù)。
注意命令:tensorboard –logdir=E:/tensorFlow/Mycode/logs/
然后我們就可以訪問127.0.0.1:6006去查看TensorBoard,如果沒有數(shù)據(jù)的話就刷新一下。到此,我們就成功的使用可視化的方式查看我們的神經(jīng)網(wǎng)絡(luò)框架了。
后記
TensorFlow配合TensorBoard的Graphs來構(gòu)建我們的框架確實(shí)非常好用,牛逼的Google肯定不會(huì)只賦予TensorBoard這一個(gè)功能,他能監(jiān)控我們的訓(xùn)練過程,讓我們可視化的看到訓(xùn)練過程中參數(shù)的變化,甚至是圖像、音頻的變化,還能展示訓(xùn)練過程的數(shù)據(jù)分布圖,所以tensorboard、TensorFlow博大精深,慢慢學(xué)習(xí)吧!
PS:感謝周莫煩大神出的[機(jī)器學(xué)習(xí)系列]
需要翻墻觀看
總結(jié)
以上是生活随笔為你收集整理的Tensorflow:Tensorboard使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 读书笔记《单核工作法》_6:颠倒you'
- 下一篇: ibm v3700添加硬盘_机 · 科普