Tensorflow 自帶可視化Tensorboard使用方法(附項(xiàng)目代碼)
Tensorboard:
如何更直觀的觀察數(shù)據(jù)在神經(jīng)網(wǎng)絡(luò)中的變化,或是已經(jīng)構(gòu)建的神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)。上一篇文章說到,可以使用matplotlib第三方可視化,來進(jìn)行一定程度上的可視化。然而Tensorflow也自帶了可視化模塊Tensorboard,并且能更直觀的看見整個(gè)神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)。
上面的結(jié)構(gòu)圖甚至可以展開,變成:
使用:
結(jié)構(gòu)圖:
with tensorflow .name_scope(layer_name):
直接使用以上代碼生成一個(gè)帶可展開符號(hào)的一個(gè)域,并且支持嵌套操作:
with tf.name_scope(layer_name): with tf.name_scope('weights'):
節(jié)點(diǎn)一般是變量或常量,需要加一個(gè)“name=‘'”參數(shù),才會(huì)展示和命名,如:
with tf.name_scope('weights'): Weights = tf.Variable(tf.random_normal([in_size,out_size]))
結(jié)構(gòu)圖符號(hào)及意義:
變量:
變量則可使用Tensorflow.histogram_summary()方法:
tf.histogram_summary(layer_name+"/weights",Weights) #name命名,Weights賦值
常量:
常量則可使用Tensorflow.scalar_summary()方法:
tf.scalar_summary('loss',loss) #命名和賦值
展示:
最后需要整合和存儲(chǔ)SummaryWriter:
#合并到Summary中 merged = tf.merge_all_summaries() #選定可視化存儲(chǔ)目錄 writer = tf.train.SummaryWriter("/目錄",sess.graph)
merged也是需要run的,因此還需要:
result = sess.run(merged) #merged也是需要run的 writer.add_summary(result,i)
執(zhí)行:
運(yùn)行后,會(huì)在相應(yīng)的目錄里生成一個(gè)文件,執(zhí)行:
tensorboard --logdir="/目錄"
會(huì)給出一段網(wǎng)址:
瀏覽器中打開這個(gè)網(wǎng)址即可,因?yàn)橛屑嫒輪栴},firefox并不能很好的兼容,建議使用Chrome。
常量在Event中,結(jié)構(gòu)圖在Graphs中,變量在最后兩個(gè)Tag中。
附項(xiàng)目代碼:
項(xiàng)目承接自上一篇文章(已更新至最新Tensorflow版本API r1.2):
import tensorflow as tf import numpy as np def add_layer(inputs,in_size,out_size,n_layer,activation_function=None): #activation_function=None線性函數(shù) layer_name="layer%s" % n_layer with tf.name_scope(layer_name): with tf.name_scope('weights'): Weights = tf.Variable(tf.random_normal([in_size,out_size])) #Weight中都是隨機(jī)變量 tf.summary.histogram(layer_name+"/weights",Weights) #可視化觀看變量 with tf.name_scope('biases'): biases = tf.Variable(tf.zeros([1,out_size])+0.1) #biases推薦初始值不為0 tf.summary.histogram(layer_name+"/biases",biases) #可視化觀看變量 with tf.name_scope('Wx_plus_b'): Wx_plus_b = tf.matmul(inputs,Weights)+biases #inputs*Weight+biases tf.summary.histogram(layer_name+"/Wx_plus_b",Wx_plus_b) #可視化觀看變量 if activation_function is None: outputs = Wx_plus_b else: outputs = activation_function(Wx_plus_b) tf.summary.histogram(layer_name+"/outputs",outputs) #可視化觀看變量 return outputs #創(chuàng)建數(shù)據(jù)x_data,y_data x_data = np.linspace(-1,1,300)[:,np.newaxis] #[-1,1]區(qū)間,300個(gè)單位,np.newaxis增加維度 noise = np.random.normal(0,0.05,x_data.shape) #噪點(diǎn) y_data = np.square(x_data)-0.5+noise with tf.name_scope('inputs'): #結(jié)構(gòu)化 xs = tf.placeholder(tf.float32,[None,1],name='x_input') ys = tf.placeholder(tf.float32,[None,1],name='y_input') #三層神經(jīng),輸入層(1個(gè)神經(jīng)元),隱藏層(10神經(jīng)元),輸出層(1個(gè)神經(jīng)元) l1 = add_layer(xs,1,10,n_layer=1,activation_function=tf.nn.relu) #隱藏層 prediction = add_layer(l1,10,1,n_layer=2,activation_function=None) #輸出層 #predition值與y_data差別 with tf.name_scope('loss'): loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1])) #square()平方,sum()求和,mean()平均值 tf.summary.scalar('loss',loss) #可視化觀看常量 with tf.name_scope('train'): train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) #0.1學(xué)習(xí)效率,minimize(loss)減小loss誤差 init = tf.initialize_all_variables() sess = tf.Session() #合并到Summary中 merged = tf.summary.merge_all() #選定可視化存儲(chǔ)目錄 writer = tf.summary.FileWriter("Desktop/",sess.graph) sess.run(init) #先執(zhí)行init #訓(xùn)練1k次 for i in range(1000): sess.run(train_step,feed_dict={xs:x_data,ys:y_data}) if i%50==0: result = sess.run(merged,feed_dict={xs:x_data,ys:y_data}) #merged也是需要run的 writer.add_summary(result,i) #result是summary類型的,需要放入writer中,i步數(shù)(x軸)
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用rpclib進(jìn)行Python網(wǎng)絡(luò)編程時(shí)的注釋問題
這篇文章主要介紹了使用rpclib進(jìn)行Python網(wǎng)絡(luò)編程時(shí)的注釋問題,作者講到了自己在編寫服務(wù)器時(shí)要用unicode注釋等需要注意的地方,需要的朋友可以參考下2015-05-05python利用xpath爬取網(wǎng)上數(shù)據(jù)并存儲(chǔ)到django模型中
這篇文章主要介紹了python利用xpath爬取網(wǎng)上數(shù)據(jù)并存儲(chǔ)到django模型中,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Python reques接口測(cè)試框架實(shí)現(xiàn)代碼
這篇文章主要介紹了Python reques接口測(cè)試框架實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Python中的startswith和endswith函數(shù)使用實(shí)例
這篇文章主要介紹了Python中的startswith和endswith函數(shù)使用實(shí)例,特別是endswith函數(shù),有了它,判斷文件的擴(kuò)展名、文件的類型在容易不過了,需要的朋友可以參考下2014-08-08利用Opencv實(shí)現(xiàn)圖片的油畫特效實(shí)例
這篇文章主要給大家介紹了關(guān)于利用Opencv實(shí)現(xiàn)圖片的油畫特效的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02完美解決keras 讀取多個(gè)hdf5文件進(jìn)行訓(xùn)練的問題
這篇文章主要介紹了完美解決keras 讀取多個(gè)hdf5文件進(jìn)行訓(xùn)練的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07windows及l(fā)inux環(huán)境下永久修改pip鏡像源的方法
不知道有沒有人跟我一樣,在剛接觸Linux時(shí)被系統(tǒng)更新源問題搞得暈頭轉(zhuǎn)向,不同的Linux更新源配置也是不一樣的,另外由于默認(rèn)安裝時(shí)的源大都是外國的更新源,速度相對(duì)國內(nèi)會(huì)慢很多,接下來本文主要介紹在windows和linux兩種系統(tǒng)環(huán)境中更新系統(tǒng)源的方法。2016-11-11使用Python對(duì)接OpenAi?API實(shí)現(xiàn)智能QQ機(jī)器人的方法
這篇文章主要介紹了使用Python對(duì)接OpenAi?API實(shí)現(xiàn)智能QQ機(jī)器人的方法,主要是提供一個(gè)方法思路,可以根據(jù)實(shí)現(xiàn)代碼延申出更多的解決方法,需要的朋友可以參考下2023-03-03